This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresolver.js
174 lines (155 loc) · 6.69 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
const fs = require('fs');
const child_process = require('child_process');
const os = require('os');
const pathmod = require('path');
const process = require('process');
function handle_exit() {
try { fs.unlinkSync(pathmod.join(process.env.OWL_TMPDIR, 'load_paths.json')); } catch (err) { }
if (os.platform().indexOf('win') > -1) {
let pipe_name = process.env.OWL_TMPDIR.replace(':', '_').replace(/\\\\/g, '_').replace(/\//g, '_').substr(-100);
let socket_path = '\\\\.\\pipe\\' + pipe_name;
try {
fs.writeFileSync(socket_path, "command:stop\x04");
} catch(e) {}
} else {
let socket_path = pathmod.join(process.env.OWL_TMPDIR, 'owcs_socket');
try {
if (fs.existsSync(socket_path)) {
// this doesnt seem to return, so anything below it is not executed
child_process.spawnSync("bundle", ["exec", "opal-webpack-compile-server", "stop", "-s", socket_path], {timeout: 10000});
}
} catch (err) { }
try { fs.unlinkSync(socket_path); } catch (err) { }
}
try { fs.rmdirSync(process.env.OWL_TMPDIR); } catch (err) { }
}
process.on('exit', function(code) { handle_exit(); });
process.on('SIGTERM', function(signal) { handle_exit(); });
module.exports = class Resolver {
constructor(source, target, filter = []) {
process.env.OWL_TMPDIR = fs.mkdtempSync(pathmod.join(os.tmpdir(), 'opal-webpack-loader-'));
if (!this.owl_cache_fetched) {
const owl_cache_path = pathmod.join(process.env.OWL_TMPDIR, 'load_paths.json');
if (!fs.existsSync(owl_cache_path)) {
let bundle_cmd;
if (os.platform().indexOf('win') > -1) { bundle_cmd = "bundle.cmd"; }
else { bundle_cmd = "bundle"; }
let gen_cache_result = child_process.spawnSync(bundle_cmd, ["exec", "owl-gen-loadpath-cache", owl_cache_path].concat(filter));
console.log(gen_cache_result.stdout.toString());
console.error(gen_cache_result.stderr.toString());
}
let owl_cache_from_file = fs.readFileSync(owl_cache_path);
let owl_cache = JSON.parse(owl_cache_from_file.toString());
this.opal_load_paths = owl_cache.opal_load_paths;
this.opal_load_path_entries = owl_cache.opal_load_path_entries;
this.owl_cache_fetched = true;
}
this.source = source;
this.target = target;
}
apply(resolver) {
const target = resolver.ensureHook(this.target);
resolver.getHook(this.source).tapAsync("OpalWebpackResolverPlugin", (request, resolveContext, callback) => {
if (request.request.endsWith('.rb') || request.request.endsWith('.js')) {
let absolute_path = this.get_absolute_path(request.path, request.request);
if (absolute_path) {
let result = Object.assign({}, request, {path: pathmod.normalize(absolute_path)});
resolver.doResolve(target, result, null, resolveContext, callback);
} else {
// continue pipeline
return callback();
}
} else {
// continue pipeline
return callback();
}
});
}
is_file(path) {
try { return fs.statSync(path).isFile(); }
catch { return false; }
}
get_absolute_path(path, request) {
let logical_filename_rb;
let logical_filename_js;
let absolute_filename;
let module;
// cleanup request, comes like './module.rb', we want '/module.rb'
if (request.startsWith('./')) {
module = request.slice(1);
} else if (request.startsWith('/')) {
module = request;
} else if (request.match(/^[A-Za-z]+\:[\\/]/)) {
module = request.replace(/\\/g,'/');
} else {
module = '/' + request;
}
// opal allows for require of
// .rb, .js, .js.rb, look up all of them
if (module.endsWith('.rb')) {
logical_filename_rb = module;
logical_filename_js = module.slice(0,module.length-2) + 'js';
} else if (module.endsWith('.js')) {
logical_filename_rb = module + '.rb';
logical_filename_js = module;
}
let l = this.opal_load_paths.length;
// check absolute path if in Windows
if (request.match(/^[A-Za-z]+\:[\\/]/)) {
if (this.is_file(logical_filename_rb)) { return logical_filename_rb; }
else if (this.is_file(logical_filename_js)) { return logical_filename_js; }
}
// in general, to avoid conflicts, we need to lookup .rb first, once all .rb
// possibilities are checked, check .js
// try .rb
// look up known entries
for (let i = 0; i < l; i++) {
absolute_filename = this.opal_load_paths[i] + logical_filename_rb;
if (this.opal_load_path_entries.includes(absolute_filename)) {
// check if file exists?
if (this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
// look up file system of app
for (let i = 0; i < l; i++) {
if (this.opal_load_paths[i].startsWith(process.cwd().replace(/\\/g,'/'))) {
absolute_filename = this.opal_load_paths[i] + logical_filename_rb;
if (this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
// check current path
absolute_filename = path + logical_filename_rb;
if (absolute_filename.startsWith(process.cwd().replace(/\\/g,'/'))) {
if (this.is_file(absolute_filename)) {
return absolute_filename;
}
}
// try .js
// look up known entries
for (let i = 0; i < l; i++) {
absolute_filename = this.opal_load_paths[i] + logical_filename_js;
if (this.opal_load_path_entries.includes(absolute_filename)) {
// check if file exists?
if (this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
// look up file system of app
for (let i = 0; i < l; i++) {
if (this.opal_load_paths[i].startsWith(process.cwd().replace(/\\/g,'/'))) {
absolute_filename = this.opal_load_paths[i] + logical_filename_js;
if (this.is_file(absolute_filename)) {
return absolute_filename;
}
}
}
// nothing found
return null;
}
};