@serveryyswys/opm 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/build.js +19 -8
- package/package.json +1 -1
package/dist/lib/build.js
CHANGED
|
@@ -120,6 +120,23 @@ function buildProject(options) {
|
|
|
120
120
|
const locate = (0, typescript_1.createLocator)(projectDir, plan.outDir);
|
|
121
121
|
const env = { locate, contract, nodeOperitArchiveDir: contract_1.NODE_OPERIT_DIR };
|
|
122
122
|
const seeds = collectSeeds(manifest, errors);
|
|
123
|
+
// 入口要注入 process / Buffer 全局(沙盒没有这两个全局)。
|
|
124
|
+
// 关键是:**注入必须在可达性遍历之前登记**,把被注入引用的这两个文件
|
|
125
|
+
// 也当作种子。否则注入代码指向的 dist/process.js 不会进包,
|
|
126
|
+
// 产物一加载就 "Cannot resolve module ./node_modules/.../dist/process.js" ——
|
|
127
|
+
// 而且是每个用户代码自己没 require process 的包都这样。
|
|
128
|
+
const entryPath = typeof manifest.main === 'string' ? toPosix(manifest.main) : '';
|
|
129
|
+
let prelude = '';
|
|
130
|
+
if (entryPath !== '') {
|
|
131
|
+
const joined = toPosix(joinRelative((0, node_path_1.dirname)(entryPath), contract_1.NODE_OPERIT_DIR));
|
|
132
|
+
const relative = joined.startsWith('.') ? joined : './' + joined;
|
|
133
|
+
prelude =
|
|
134
|
+
'/* opm: Operit 沙盒没有 process / Buffer 全局,入口先挂上(见 node.operit 的 BUILTINS.json) */\n' +
|
|
135
|
+
"globalThis.process = require('" + relative + "/dist/process.js');\n" +
|
|
136
|
+
"globalThis.Buffer = require('" + relative + "/dist/buffer.js').Buffer;\n";
|
|
137
|
+
seeds.push(joined + '/dist/process.js');
|
|
138
|
+
seeds.push(joined + '/dist/buffer.js');
|
|
139
|
+
}
|
|
123
140
|
const visited = new Set();
|
|
124
141
|
const rewritten = new Map();
|
|
125
142
|
const queue = [];
|
|
@@ -152,14 +169,8 @@ function buildProject(options) {
|
|
|
152
169
|
}
|
|
153
170
|
}
|
|
154
171
|
}
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
if (entryPath !== '' && rewritten.has(entryPath)) {
|
|
158
|
-
const joined = toPosix(joinRelative((0, node_path_1.dirname)(entryPath), contract_1.NODE_OPERIT_DIR));
|
|
159
|
-
const relative = joined.startsWith('.') ? joined : './' + joined;
|
|
160
|
-
const prelude = '/* opm: Operit 沙盒没有 process / Buffer 全局,入口先挂上(见 node.operit 的 BUILTINS.json) */\n' +
|
|
161
|
-
"globalThis.process = require('" + relative + "/dist/process.js');\n" +
|
|
162
|
-
"globalThis.Buffer = require('" + relative + "/dist/buffer.js').Buffer;\n";
|
|
172
|
+
// 注入正文(文件已在遍历阶段按种子收好,这里只贴前置代码)
|
|
173
|
+
if (entryPath !== '' && prelude !== '' && rewritten.has(entryPath)) {
|
|
163
174
|
rewritten.set(entryPath, prelude + rewritten.get(entryPath));
|
|
164
175
|
notes.push('已向入口 ' + entryPath + ' 注入 process / Buffer 全局');
|
|
165
176
|
}
|