@vercel/nft 0.26.4 → 0.27.0
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/out/analyze.js +201 -94
- package/out/analyze.js.map +1 -1
- package/out/cli.js +4 -4
- package/out/cli.js.map +1 -1
- package/out/fs.d.ts +1 -1
- package/out/fs.js +3 -3
- package/out/fs.js.map +1 -1
- package/out/node-file-trace.js +14 -8
- package/out/node-file-trace.js.map +1 -1
- package/out/resolve-dependency.js +62 -26
- package/out/resolve-dependency.js.map +1 -1
- package/out/utils/ast-helpers.js +8 -2
- package/out/utils/ast-helpers.js.map +1 -1
- package/out/utils/binary-locators.js +19 -19
- package/out/utils/binary-locators.js.map +1 -1
- package/out/utils/get-package-base.js +0 -1
- package/out/utils/get-package-base.js.map +1 -1
- package/out/utils/sharedlib-emit.js +2 -3
- package/out/utils/sharedlib-emit.js.map +1 -1
- package/out/utils/special-cases.js +88 -40
- package/out/utils/special-cases.js.map +1 -1
- package/out/utils/static-eval.js +54 -36
- package/out/utils/static-eval.js.map +1 -1
- package/out/utils/wrappers.js +200 -109
- package/out/utils/wrappers.js.map +1 -1
- package/package.json +17 -10
- package/readme.md +26 -22
package/out/analyze.js
CHANGED
|
@@ -26,7 +26,7 @@ const acorn = acorn_1.Parser.extend(
|
|
|
26
26
|
//require("acorn-class-fields"),
|
|
27
27
|
//require("acorn-static-class-features"),
|
|
28
28
|
//require("acorn-private-class-elements")
|
|
29
|
-
require(
|
|
29
|
+
require('acorn-import-attributes').importAttributesOrAssertions);
|
|
30
30
|
const os_1 = __importDefault(require("os"));
|
|
31
31
|
const wrappers_1 = require("./utils/wrappers");
|
|
32
32
|
const resolve_from_1 = __importDefault(require("resolve-from"));
|
|
@@ -36,9 +36,9 @@ const staticProcess = {
|
|
|
36
36
|
},
|
|
37
37
|
env: {
|
|
38
38
|
NODE_ENV: static_eval_1.UNKNOWN,
|
|
39
|
-
[static_eval_1.UNKNOWN]: true
|
|
39
|
+
[static_eval_1.UNKNOWN]: true,
|
|
40
40
|
},
|
|
41
|
-
[static_eval_1.UNKNOWN]: true
|
|
41
|
+
[static_eval_1.UNKNOWN]: true,
|
|
42
42
|
};
|
|
43
43
|
// unique symbol value to identify express instance in static analysis
|
|
44
44
|
const EXPRESS_SET = Symbol();
|
|
@@ -66,7 +66,7 @@ const fsSymbols = {
|
|
|
66
66
|
readFile: FS_FN,
|
|
67
67
|
readFileSync: FS_FN,
|
|
68
68
|
stat: FS_FN,
|
|
69
|
-
statSync: FS_FN
|
|
69
|
+
statSync: FS_FN,
|
|
70
70
|
};
|
|
71
71
|
const fsExtraSymbols = {
|
|
72
72
|
...fsSymbols,
|
|
@@ -79,72 +79,72 @@ const fsExtraSymbols = {
|
|
|
79
79
|
};
|
|
80
80
|
const staticModules = Object.assign(Object.create(null), {
|
|
81
81
|
bindings: {
|
|
82
|
-
default: BINDINGS
|
|
82
|
+
default: BINDINGS,
|
|
83
83
|
},
|
|
84
84
|
express: {
|
|
85
85
|
default: function () {
|
|
86
86
|
return {
|
|
87
87
|
[static_eval_1.UNKNOWN]: true,
|
|
88
88
|
set: EXPRESS_SET,
|
|
89
|
-
engine: EXPRESS_ENGINE
|
|
89
|
+
engine: EXPRESS_ENGINE,
|
|
90
90
|
};
|
|
91
|
-
}
|
|
91
|
+
},
|
|
92
92
|
},
|
|
93
93
|
fs: {
|
|
94
94
|
default: fsSymbols,
|
|
95
|
-
...fsSymbols
|
|
95
|
+
...fsSymbols,
|
|
96
96
|
},
|
|
97
97
|
'fs-extra': {
|
|
98
98
|
default: fsExtraSymbols,
|
|
99
|
-
...fsExtraSymbols
|
|
99
|
+
...fsExtraSymbols,
|
|
100
100
|
},
|
|
101
101
|
'graceful-fs': {
|
|
102
102
|
default: fsSymbols,
|
|
103
|
-
...fsSymbols
|
|
103
|
+
...fsSymbols,
|
|
104
104
|
},
|
|
105
105
|
process: {
|
|
106
106
|
default: staticProcess,
|
|
107
|
-
...staticProcess
|
|
107
|
+
...staticProcess,
|
|
108
108
|
},
|
|
109
109
|
// populated below
|
|
110
110
|
path: {
|
|
111
|
-
default: {}
|
|
111
|
+
default: {},
|
|
112
112
|
},
|
|
113
113
|
os: {
|
|
114
114
|
default: os_1.default,
|
|
115
|
-
...os_1.default
|
|
115
|
+
...os_1.default,
|
|
116
116
|
},
|
|
117
117
|
'@mapbox/node-pre-gyp': {
|
|
118
118
|
default: node_pre_gyp_1.default,
|
|
119
|
-
...node_pre_gyp_1.default
|
|
119
|
+
...node_pre_gyp_1.default,
|
|
120
120
|
},
|
|
121
121
|
'node-pre-gyp': binary_locators_1.pregyp,
|
|
122
122
|
'node-pre-gyp/lib/pre-binding': binary_locators_1.pregyp,
|
|
123
123
|
'node-pre-gyp/lib/pre-binding.js': binary_locators_1.pregyp,
|
|
124
124
|
'node-gyp-build': {
|
|
125
|
-
default: NODE_GYP_BUILD
|
|
125
|
+
default: NODE_GYP_BUILD,
|
|
126
126
|
},
|
|
127
127
|
'@aminya/node-gyp-build': {
|
|
128
|
-
default: NODE_GYP_BUILD
|
|
128
|
+
default: NODE_GYP_BUILD,
|
|
129
129
|
},
|
|
130
|
-
|
|
130
|
+
nbind: {
|
|
131
131
|
init: NBIND_INIT,
|
|
132
132
|
default: {
|
|
133
|
-
init: NBIND_INIT
|
|
134
|
-
}
|
|
133
|
+
init: NBIND_INIT,
|
|
134
|
+
},
|
|
135
135
|
},
|
|
136
136
|
'resolve-from': {
|
|
137
|
-
default: resolve_from_1.default
|
|
137
|
+
default: resolve_from_1.default,
|
|
138
138
|
},
|
|
139
139
|
'strong-globalize': {
|
|
140
140
|
default: {
|
|
141
|
-
SetRootDir: SET_ROOT_DIR
|
|
141
|
+
SetRootDir: SET_ROOT_DIR,
|
|
142
142
|
},
|
|
143
|
-
SetRootDir: SET_ROOT_DIR
|
|
143
|
+
SetRootDir: SET_ROOT_DIR,
|
|
144
|
+
},
|
|
145
|
+
pkginfo: {
|
|
146
|
+
default: PKG_INFO,
|
|
144
147
|
},
|
|
145
|
-
'pkginfo': {
|
|
146
|
-
default: PKG_INFO
|
|
147
|
-
}
|
|
148
148
|
});
|
|
149
149
|
const globalBindings = {
|
|
150
150
|
// Support for require calls generated from `import` statements by babel
|
|
@@ -156,15 +156,18 @@ const globalBindings = {
|
|
|
156
156
|
MONGOOSE_DRIVER_PATH: undefined,
|
|
157
157
|
URL: url_1.URL,
|
|
158
158
|
Object: {
|
|
159
|
-
assign: Object.assign
|
|
160
|
-
}
|
|
159
|
+
assign: Object.assign,
|
|
160
|
+
},
|
|
161
161
|
};
|
|
162
|
-
globalBindings.global =
|
|
162
|
+
globalBindings.global =
|
|
163
|
+
globalBindings.GLOBAL =
|
|
164
|
+
globalBindings.globalThis =
|
|
165
|
+
globalBindings;
|
|
163
166
|
// call expression triggers
|
|
164
167
|
const TRIGGER = Symbol();
|
|
165
168
|
binary_locators_1.pregyp.find[TRIGGER] = true;
|
|
166
169
|
const staticPath = staticModules.path;
|
|
167
|
-
Object.keys(path_1.default).forEach(name => {
|
|
170
|
+
Object.keys(path_1.default).forEach((name) => {
|
|
168
171
|
const pathFn = path_1.default[name];
|
|
169
172
|
if (typeof pathFn === 'function') {
|
|
170
173
|
const fn = function mockPath() {
|
|
@@ -183,7 +186,12 @@ staticPath.resolve = staticPath.default.resolve = function (...args) {
|
|
|
183
186
|
};
|
|
184
187
|
staticPath.resolve[TRIGGER] = true;
|
|
185
188
|
const excludeAssetExtensions = new Set(['.h', '.cmake', '.c', '.cpp']);
|
|
186
|
-
const excludeAssetFiles = new Set([
|
|
189
|
+
const excludeAssetFiles = new Set([
|
|
190
|
+
'CHANGELOG.md',
|
|
191
|
+
'README.md',
|
|
192
|
+
'readme.md',
|
|
193
|
+
'changelog.md',
|
|
194
|
+
]);
|
|
187
195
|
let cwd;
|
|
188
196
|
const absoluteRegEx = /^\/[^\/]+|^[a-z]:[\\/][^\\/]+/i;
|
|
189
197
|
function isAbsolutePathOrUrl(str) {
|
|
@@ -205,7 +213,6 @@ function isAbsolutePathOrUrl(str) {
|
|
|
205
213
|
}
|
|
206
214
|
const BOUND_REQUIRE = Symbol();
|
|
207
215
|
const repeatGlobRegEx = /([\/\\]\*\*[\/\\]\*)+/g;
|
|
208
|
-
;
|
|
209
216
|
async function analyze(id, code, job) {
|
|
210
217
|
const assets = new Set();
|
|
211
218
|
const deps = new Set();
|
|
@@ -219,23 +226,31 @@ async function analyze(id, code, job) {
|
|
|
219
226
|
if (!job.analysis.emitGlobs)
|
|
220
227
|
return;
|
|
221
228
|
const wildcardIndex = wildcardPath.indexOf(static_eval_1.WILDCARD);
|
|
222
|
-
const dirIndex = wildcardIndex === -1
|
|
229
|
+
const dirIndex = wildcardIndex === -1
|
|
230
|
+
? wildcardPath.length
|
|
231
|
+
: wildcardPath.lastIndexOf(path_1.default.sep, wildcardIndex);
|
|
223
232
|
const assetDirPath = wildcardPath.substring(0, dirIndex);
|
|
224
233
|
const patternPath = wildcardPath.slice(dirIndex);
|
|
225
|
-
const wildcardPattern = patternPath
|
|
234
|
+
const wildcardPattern = patternPath
|
|
235
|
+
.replace(static_eval_1.wildcardRegEx, (_match, index) => {
|
|
226
236
|
return patternPath[index - 1] === path_1.default.sep ? '**/*' : '*';
|
|
227
|
-
})
|
|
237
|
+
})
|
|
238
|
+
.replace(repeatGlobRegEx, '/**/*') || '/**/*';
|
|
228
239
|
if (job.ignoreFn(path_1.default.relative(job.base, assetDirPath + wildcardPattern)))
|
|
229
240
|
return;
|
|
230
241
|
assetEmissionPromises = assetEmissionPromises.then(async () => {
|
|
231
242
|
if (job.log)
|
|
232
243
|
console.log('Globbing ' + assetDirPath + wildcardPattern);
|
|
233
|
-
const files =
|
|
244
|
+
const files = await new Promise((resolve, reject) => (0, glob_1.default)(assetDirPath + wildcardPattern, {
|
|
245
|
+
mark: true,
|
|
246
|
+
ignore: assetDirPath + '/**/node_modules/**/*',
|
|
247
|
+
dot: true,
|
|
248
|
+
}, (err, files) => (err ? reject(err) : resolve(files))));
|
|
234
249
|
files
|
|
235
|
-
.filter(name => !excludeAssetExtensions.has(path_1.default.extname(name)) &&
|
|
250
|
+
.filter((name) => !excludeAssetExtensions.has(path_1.default.extname(name)) &&
|
|
236
251
|
!excludeAssetFiles.has(path_1.default.basename(name)) &&
|
|
237
252
|
!name.endsWith('/'))
|
|
238
|
-
.forEach(file => assets.add(file));
|
|
253
|
+
.forEach((file) => assets.add(file));
|
|
239
254
|
});
|
|
240
255
|
};
|
|
241
256
|
let assetEmissionPromises = Promise.resolve();
|
|
@@ -244,7 +259,10 @@ async function analyze(id, code, job) {
|
|
|
244
259
|
let ast;
|
|
245
260
|
let isESM = false;
|
|
246
261
|
try {
|
|
247
|
-
ast = acorn.parse(code, {
|
|
262
|
+
ast = acorn.parse(code, {
|
|
263
|
+
ecmaVersion: 'latest',
|
|
264
|
+
allowReturnOutsideFunction: true,
|
|
265
|
+
});
|
|
248
266
|
isESM = false;
|
|
249
267
|
}
|
|
250
268
|
catch (e) {
|
|
@@ -256,7 +274,11 @@ async function analyze(id, code, job) {
|
|
|
256
274
|
//@ts-ignore
|
|
257
275
|
if (!ast) {
|
|
258
276
|
try {
|
|
259
|
-
ast = acorn.parse(code, {
|
|
277
|
+
ast = acorn.parse(code, {
|
|
278
|
+
ecmaVersion: 'latest',
|
|
279
|
+
sourceType: 'module',
|
|
280
|
+
allowAwaitOutsideFunction: true,
|
|
281
|
+
});
|
|
260
282
|
isESM = true;
|
|
261
283
|
}
|
|
262
284
|
catch (e) {
|
|
@@ -269,16 +291,16 @@ async function analyze(id, code, job) {
|
|
|
269
291
|
const knownBindings = Object.assign(Object.create(null), {
|
|
270
292
|
__dirname: {
|
|
271
293
|
shadowDepth: 0,
|
|
272
|
-
value: { value: path_1.default.resolve(id, '..') }
|
|
294
|
+
value: { value: path_1.default.resolve(id, '..') },
|
|
273
295
|
},
|
|
274
296
|
__filename: {
|
|
275
297
|
shadowDepth: 0,
|
|
276
|
-
value: { value: id }
|
|
298
|
+
value: { value: id },
|
|
277
299
|
},
|
|
278
300
|
process: {
|
|
279
301
|
shadowDepth: 0,
|
|
280
|
-
value: { value: staticProcess }
|
|
281
|
-
}
|
|
302
|
+
value: { value: staticProcess },
|
|
303
|
+
},
|
|
282
304
|
});
|
|
283
305
|
if (!isESM || job.mixedModules) {
|
|
284
306
|
knownBindings.require = {
|
|
@@ -292,9 +314,9 @@ async function analyze(id, code, job) {
|
|
|
292
314
|
},
|
|
293
315
|
resolve(specifier) {
|
|
294
316
|
return (0, resolve_dependency_js_1.default)(specifier, id, job);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
},
|
|
298
320
|
};
|
|
299
321
|
knownBindings.require.value.value.resolve[TRIGGER] = true;
|
|
300
322
|
}
|
|
@@ -305,7 +327,7 @@ async function analyze(id, code, job) {
|
|
|
305
327
|
return;
|
|
306
328
|
knownBindings[name] = {
|
|
307
329
|
shadowDepth: 0,
|
|
308
|
-
value: value
|
|
330
|
+
value: value,
|
|
309
331
|
};
|
|
310
332
|
}
|
|
311
333
|
function getKnownBinding(name) {
|
|
@@ -331,14 +353,19 @@ async function analyze(id, code, job) {
|
|
|
331
353
|
for (const impt of decl.specifiers) {
|
|
332
354
|
if (impt.type === 'ImportNamespaceSpecifier')
|
|
333
355
|
setKnownBinding(impt.local.name, { value: staticModule });
|
|
334
|
-
else if (impt.type === 'ImportDefaultSpecifier' &&
|
|
356
|
+
else if (impt.type === 'ImportDefaultSpecifier' &&
|
|
357
|
+
'default' in staticModule)
|
|
335
358
|
setKnownBinding(impt.local.name, { value: staticModule.default });
|
|
336
|
-
else if (impt.type === 'ImportSpecifier' &&
|
|
337
|
-
|
|
359
|
+
else if (impt.type === 'ImportSpecifier' &&
|
|
360
|
+
impt.imported.name in staticModule)
|
|
361
|
+
setKnownBinding(impt.local.name, {
|
|
362
|
+
value: staticModule[impt.imported.name],
|
|
363
|
+
});
|
|
338
364
|
}
|
|
339
365
|
}
|
|
340
366
|
}
|
|
341
|
-
else if (decl.type === 'ExportNamedDeclaration' ||
|
|
367
|
+
else if (decl.type === 'ExportNamedDeclaration' ||
|
|
368
|
+
decl.type === 'ExportAllDeclaration') {
|
|
342
369
|
if (decl.source)
|
|
343
370
|
deps.add(String(decl.source.value));
|
|
344
371
|
}
|
|
@@ -346,10 +373,10 @@ async function analyze(id, code, job) {
|
|
|
346
373
|
}
|
|
347
374
|
async function computePureStaticValue(expr, computeBranches = true) {
|
|
348
375
|
const vars = Object.create(null);
|
|
349
|
-
Object.keys(globalBindings).forEach(name => {
|
|
376
|
+
Object.keys(globalBindings).forEach((name) => {
|
|
350
377
|
vars[name] = { value: globalBindings[name] };
|
|
351
378
|
});
|
|
352
|
-
Object.keys(knownBindings).forEach(name => {
|
|
379
|
+
Object.keys(knownBindings).forEach((name) => {
|
|
353
380
|
vars[name] = getKnownBinding(name);
|
|
354
381
|
});
|
|
355
382
|
vars['import.meta'] = { url: importMetaUrl };
|
|
@@ -364,29 +391,33 @@ async function analyze(id, code, job) {
|
|
|
364
391
|
// Express engine opt-out
|
|
365
392
|
let definedExpressEngines = false;
|
|
366
393
|
function emitWildcardRequire(wildcardRequire) {
|
|
367
|
-
if (!job.analysis.emitGlobs ||
|
|
394
|
+
if (!job.analysis.emitGlobs ||
|
|
395
|
+
(!wildcardRequire.startsWith('./') && !wildcardRequire.startsWith('../')))
|
|
368
396
|
return;
|
|
369
397
|
wildcardRequire = path_1.default.resolve(dir, wildcardRequire);
|
|
370
398
|
const wildcardIndex = wildcardRequire.indexOf(static_eval_1.WILDCARD);
|
|
371
|
-
const dirIndex = wildcardIndex === -1
|
|
399
|
+
const dirIndex = wildcardIndex === -1
|
|
400
|
+
? wildcardRequire.length
|
|
401
|
+
: wildcardRequire.lastIndexOf(path_1.default.sep, wildcardIndex);
|
|
372
402
|
const wildcardDirPath = wildcardRequire.substring(0, dirIndex);
|
|
373
403
|
const patternPath = wildcardRequire.slice(dirIndex);
|
|
374
404
|
let wildcardPattern = patternPath.replace(static_eval_1.wildcardRegEx, (_match, index) => {
|
|
375
405
|
return patternPath[index - 1] === path_1.default.sep ? '**/*' : '*';
|
|
376
406
|
}) || '/**/*';
|
|
377
407
|
if (!wildcardPattern.endsWith('*'))
|
|
378
|
-
wildcardPattern +=
|
|
408
|
+
wildcardPattern +=
|
|
409
|
+
'?(' + (job.ts ? '.ts|.tsx|' : '') + '.js|.json|.node)';
|
|
379
410
|
if (job.ignoreFn(path_1.default.relative(job.base, wildcardDirPath + wildcardPattern)))
|
|
380
411
|
return;
|
|
381
412
|
assetEmissionPromises = assetEmissionPromises.then(async () => {
|
|
382
413
|
if (job.log)
|
|
383
414
|
console.log('Globbing ' + wildcardDirPath + wildcardPattern);
|
|
384
|
-
const files =
|
|
415
|
+
const files = await new Promise((resolve, reject) => (0, glob_1.default)(wildcardDirPath + wildcardPattern, { mark: true, ignore: wildcardDirPath + '/**/node_modules/**/*' }, (err, files) => (err ? reject(err) : resolve(files))));
|
|
385
416
|
files
|
|
386
|
-
.filter(name => !excludeAssetExtensions.has(path_1.default.extname(name)) &&
|
|
417
|
+
.filter((name) => !excludeAssetExtensions.has(path_1.default.extname(name)) &&
|
|
387
418
|
!excludeAssetFiles.has(path_1.default.basename(name)) &&
|
|
388
419
|
!name.endsWith('/'))
|
|
389
|
-
.forEach(file => deps.add(file));
|
|
420
|
+
.forEach((file) => deps.add(file));
|
|
390
421
|
});
|
|
391
422
|
}
|
|
392
423
|
async function processRequireArg(expression, isImport = false) {
|
|
@@ -419,7 +450,14 @@ async function analyze(id, code, job) {
|
|
|
419
450
|
let scope = (0, pluginutils_1.attachScopes)(ast, 'scope');
|
|
420
451
|
if (isAst(ast)) {
|
|
421
452
|
(0, wrappers_1.handleWrappers)(ast);
|
|
422
|
-
await (0, special_cases_1.default)({
|
|
453
|
+
await (0, special_cases_1.default)({
|
|
454
|
+
id,
|
|
455
|
+
ast,
|
|
456
|
+
emitDependency: (path) => deps.add(path),
|
|
457
|
+
emitAsset: (path) => assets.add(path),
|
|
458
|
+
emitAssetDirectory,
|
|
459
|
+
job,
|
|
460
|
+
});
|
|
423
461
|
}
|
|
424
462
|
async function backtrack(parent, context) {
|
|
425
463
|
// computing a static expression outward
|
|
@@ -429,8 +467,11 @@ async function analyze(id, code, job) {
|
|
|
429
467
|
throw new Error('Internal error: No staticChildNode for backtrack.');
|
|
430
468
|
const curStaticValue = await computePureStaticValue(parent, true);
|
|
431
469
|
if (curStaticValue) {
|
|
432
|
-
if ('value' in curStaticValue &&
|
|
433
|
-
|
|
470
|
+
if (('value' in curStaticValue &&
|
|
471
|
+
typeof curStaticValue.value !== 'symbol') ||
|
|
472
|
+
('then' in curStaticValue &&
|
|
473
|
+
typeof curStaticValue.then !== 'symbol' &&
|
|
474
|
+
typeof curStaticValue.else !== 'symbol')) {
|
|
434
475
|
staticChildValue = curStaticValue;
|
|
435
476
|
staticChildNode = parent;
|
|
436
477
|
if (context)
|
|
@@ -458,19 +499,31 @@ async function analyze(id, code, job) {
|
|
|
458
499
|
if (!parent)
|
|
459
500
|
return;
|
|
460
501
|
if (node.type === 'Identifier') {
|
|
461
|
-
if ((0, ast_helpers_1.isIdentifierRead)(node, parent) &&
|
|
502
|
+
if ((0, ast_helpers_1.isIdentifierRead)(node, parent) &&
|
|
503
|
+
job.analysis.computeFileReferences) {
|
|
462
504
|
let binding;
|
|
463
505
|
// detect asset leaf expression triggers (if not already)
|
|
464
506
|
// __dirname, __filename
|
|
465
|
-
if (typeof (binding = getKnownBinding(node.name)?.value) === 'string' &&
|
|
466
|
-
binding
|
|
467
|
-
|
|
507
|
+
if ((typeof (binding = getKnownBinding(node.name)?.value) === 'string' &&
|
|
508
|
+
binding.match(absoluteRegEx)) ||
|
|
509
|
+
(binding &&
|
|
510
|
+
(typeof binding === 'function' || typeof binding === 'object') &&
|
|
511
|
+
binding[TRIGGER])) {
|
|
512
|
+
staticChildValue = {
|
|
513
|
+
value: typeof binding === 'string' ? binding : undefined,
|
|
514
|
+
};
|
|
468
515
|
staticChildNode = node;
|
|
469
516
|
await backtrack(parent, this);
|
|
470
517
|
}
|
|
471
518
|
}
|
|
472
519
|
}
|
|
473
|
-
else if (job.analysis.computeFileReferences &&
|
|
520
|
+
else if (job.analysis.computeFileReferences &&
|
|
521
|
+
node.type === 'MemberExpression' &&
|
|
522
|
+
node.object.type === 'MetaProperty' &&
|
|
523
|
+
node.object.meta.name === 'import' &&
|
|
524
|
+
node.object.property.name === 'meta' &&
|
|
525
|
+
(node.property.computed ? node.property.value : node.property.name) ===
|
|
526
|
+
'url') {
|
|
474
527
|
// import.meta.url leaf trigger
|
|
475
528
|
staticChildValue = { value: importMetaUrl };
|
|
476
529
|
staticChildNode = node;
|
|
@@ -487,8 +540,11 @@ async function analyze(id, code, job) {
|
|
|
487
540
|
// - nodegyp()
|
|
488
541
|
// - etc.
|
|
489
542
|
else if (node.type === 'CallExpression') {
|
|
490
|
-
if ((!isESM || job.mixedModules) &&
|
|
491
|
-
|
|
543
|
+
if ((!isESM || job.mixedModules) &&
|
|
544
|
+
node.callee.type === 'Identifier' &&
|
|
545
|
+
node.arguments.length) {
|
|
546
|
+
if (node.callee.name === 'require' &&
|
|
547
|
+
knownBindings.require.shadowDepth === 0) {
|
|
492
548
|
await processRequireArg(node.arguments[0]);
|
|
493
549
|
return;
|
|
494
550
|
}
|
|
@@ -517,10 +573,15 @@ async function analyze(id, code, job) {
|
|
|
517
573
|
await processRequireArg(node.arguments[0]);
|
|
518
574
|
return;
|
|
519
575
|
}
|
|
520
|
-
const calleeValue = job.analysis.evaluatePureExpressions &&
|
|
576
|
+
const calleeValue = job.analysis.evaluatePureExpressions &&
|
|
577
|
+
(await computePureStaticValue(node.callee, false));
|
|
521
578
|
// if we have a direct pure static function,
|
|
522
579
|
// and that function has a [TRIGGER] symbol -> trigger asset emission from it
|
|
523
|
-
if (calleeValue &&
|
|
580
|
+
if (calleeValue &&
|
|
581
|
+
'value' in calleeValue &&
|
|
582
|
+
typeof calleeValue.value === 'function' &&
|
|
583
|
+
calleeValue.value[TRIGGER] &&
|
|
584
|
+
job.analysis.computeFileReferences) {
|
|
524
585
|
staticChildValue = await computePureStaticValue(node, true);
|
|
525
586
|
// if it computes, then we start backtracking
|
|
526
587
|
if (staticChildValue && parent) {
|
|
@@ -529,7 +590,9 @@ async function analyze(id, code, job) {
|
|
|
529
590
|
}
|
|
530
591
|
}
|
|
531
592
|
// handle well-known function symbol cases
|
|
532
|
-
else if (calleeValue &&
|
|
593
|
+
else if (calleeValue &&
|
|
594
|
+
'value' in calleeValue &&
|
|
595
|
+
typeof calleeValue.value === 'symbol') {
|
|
533
596
|
switch (calleeValue.value) {
|
|
534
597
|
// customRequireWrapper('...')
|
|
535
598
|
case BOUND_REQUIRE:
|
|
@@ -580,7 +643,8 @@ async function analyze(id, code, job) {
|
|
|
580
643
|
node.arguments[0].arguments[0].type === 'Identifier' &&
|
|
581
644
|
node.arguments[0].arguments[0].name === '__dirname' &&
|
|
582
645
|
node.arguments[0].arguments[1].type === 'Literal';
|
|
583
|
-
if (knownBindings.__dirname.shadowDepth === 0 &&
|
|
646
|
+
if (knownBindings.__dirname.shadowDepth === 0 &&
|
|
647
|
+
(withDirname || withPathJoinDirname)) {
|
|
584
648
|
const pathJoinedDir = withPathJoinDirname
|
|
585
649
|
? path_1.default.join(dir, node.arguments[0].arguments[1].value)
|
|
586
650
|
: dir;
|
|
@@ -610,7 +674,10 @@ async function analyze(id, code, job) {
|
|
|
610
674
|
case NBIND_INIT:
|
|
611
675
|
if (node.arguments.length) {
|
|
612
676
|
const arg = await computePureStaticValue(node.arguments[0], false);
|
|
613
|
-
if (arg &&
|
|
677
|
+
if (arg &&
|
|
678
|
+
'value' in arg &&
|
|
679
|
+
(typeof arg.value === 'string' ||
|
|
680
|
+
typeof arg.value === 'undefined')) {
|
|
614
681
|
const bindingInfo = (0, binary_locators_1.nbind)(arg.value);
|
|
615
682
|
if (bindingInfo && bindingInfo.path) {
|
|
616
683
|
deps.add(path_1.default.relative(dir, bindingInfo.path).replace(/\\/g, '/'));
|
|
@@ -641,7 +708,9 @@ async function analyze(id, code, job) {
|
|
|
641
708
|
// if it computes, then we start backtracking
|
|
642
709
|
if (staticChildValue) {
|
|
643
710
|
staticChildNode = node.arguments[0];
|
|
644
|
-
if (calleeValue.value === FS_DIR_FN &&
|
|
711
|
+
if (calleeValue.value === FS_DIR_FN &&
|
|
712
|
+
node.arguments[0].type === 'Identifier' &&
|
|
713
|
+
node.arguments[0].name === '__dirname') {
|
|
645
714
|
// Special case `fs.readdirSync(__dirname)` to emit right away
|
|
646
715
|
emitAssetDirectory(dir);
|
|
647
716
|
}
|
|
@@ -665,7 +734,8 @@ async function analyze(id, code, job) {
|
|
|
665
734
|
case PKG_INFO:
|
|
666
735
|
let pjsonPath = path_1.default.resolve(id, '../package.json');
|
|
667
736
|
const rootPjson = path_1.default.resolve('/package.json');
|
|
668
|
-
while (pjsonPath !== rootPjson &&
|
|
737
|
+
while (pjsonPath !== rootPjson &&
|
|
738
|
+
(await job.stat(pjsonPath)) === null)
|
|
669
739
|
pjsonPath = path_1.default.resolve(pjsonPath, '../../package.json');
|
|
670
740
|
if (pjsonPath !== rootPjson)
|
|
671
741
|
assets.add(pjsonPath);
|
|
@@ -673,7 +743,10 @@ async function analyze(id, code, job) {
|
|
|
673
743
|
}
|
|
674
744
|
}
|
|
675
745
|
}
|
|
676
|
-
else if (node.type === 'VariableDeclaration' &&
|
|
746
|
+
else if (node.type === 'VariableDeclaration' &&
|
|
747
|
+
parent &&
|
|
748
|
+
!(0, ast_helpers_1.isVarLoop)(parent) &&
|
|
749
|
+
job.analysis.evaluatePureExpressions) {
|
|
677
750
|
for (const decl of node.declarations) {
|
|
678
751
|
if (!decl.init)
|
|
679
752
|
continue;
|
|
@@ -693,10 +766,14 @@ async function analyze(id, code, job) {
|
|
|
693
766
|
computed.value === null ||
|
|
694
767
|
!(prop.key.name in computed.value))
|
|
695
768
|
continue;
|
|
696
|
-
setKnownBinding(prop.value.name, {
|
|
769
|
+
setKnownBinding(prop.value.name, {
|
|
770
|
+
value: computed.value[prop.key.name],
|
|
771
|
+
});
|
|
697
772
|
}
|
|
698
773
|
}
|
|
699
|
-
if (!('value' in computed) &&
|
|
774
|
+
if (!('value' in computed) &&
|
|
775
|
+
isAbsolutePathOrUrl(computed.then) &&
|
|
776
|
+
isAbsolutePathOrUrl(computed.else)) {
|
|
700
777
|
staticChildValue = computed;
|
|
701
778
|
staticChildNode = decl.init;
|
|
702
779
|
await emitStaticChildAsset();
|
|
@@ -704,7 +781,10 @@ async function analyze(id, code, job) {
|
|
|
704
781
|
}
|
|
705
782
|
}
|
|
706
783
|
}
|
|
707
|
-
else if (node.type === 'AssignmentExpression' &&
|
|
784
|
+
else if (node.type === 'AssignmentExpression' &&
|
|
785
|
+
parent &&
|
|
786
|
+
!(0, ast_helpers_1.isLoop)(parent) &&
|
|
787
|
+
job.analysis.evaluatePureExpressions) {
|
|
708
788
|
if (!hasKnownBindingValue(node.left.name)) {
|
|
709
789
|
const computed = await computePureStaticValue(node.right, false);
|
|
710
790
|
if (computed && 'value' in computed) {
|
|
@@ -722,7 +802,9 @@ async function analyze(id, code, job) {
|
|
|
722
802
|
computed.value === null ||
|
|
723
803
|
!(prop.key.name in computed.value))
|
|
724
804
|
continue;
|
|
725
|
-
setKnownBinding(prop.value.name, {
|
|
805
|
+
setKnownBinding(prop.value.name, {
|
|
806
|
+
value: computed.value[prop.key.name],
|
|
807
|
+
});
|
|
726
808
|
}
|
|
727
809
|
}
|
|
728
810
|
if (isAbsolutePathOrUrl(computed.value)) {
|
|
@@ -735,11 +817,15 @@ async function analyze(id, code, job) {
|
|
|
735
817
|
}
|
|
736
818
|
// Support require wrappers like function p (x) { ...; var y = require(x); ...; return y; }
|
|
737
819
|
else if ((!isESM || job.mixedModules) &&
|
|
738
|
-
(node.type === 'FunctionDeclaration' ||
|
|
739
|
-
|
|
820
|
+
(node.type === 'FunctionDeclaration' ||
|
|
821
|
+
node.type === 'FunctionExpression' ||
|
|
822
|
+
node.type === 'ArrowFunctionExpression') &&
|
|
823
|
+
(node.arguments || node.params)[0] &&
|
|
824
|
+
(node.arguments || node.params)[0].type === 'Identifier') {
|
|
740
825
|
let fnName;
|
|
741
826
|
let args;
|
|
742
|
-
if ((node.type === 'ArrowFunctionExpression' ||
|
|
827
|
+
if ((node.type === 'ArrowFunctionExpression' ||
|
|
828
|
+
node.type === 'FunctionExpression') &&
|
|
743
829
|
parent &&
|
|
744
830
|
parent.type === 'VariableDeclarator' &&
|
|
745
831
|
parent.id.type === 'Identifier') {
|
|
@@ -753,7 +839,8 @@ async function analyze(id, code, job) {
|
|
|
753
839
|
if (fnName && node.body.body) {
|
|
754
840
|
let requireDecl, returned = false;
|
|
755
841
|
for (let i = 0; i < node.body.body.length; i++) {
|
|
756
|
-
if (node.body.body[i].type === 'VariableDeclaration' &&
|
|
842
|
+
if (node.body.body[i].type === 'VariableDeclaration' &&
|
|
843
|
+
!requireDecl) {
|
|
757
844
|
requireDecl = node.body.body[i].declarations.find((decl) => decl &&
|
|
758
845
|
decl.id &&
|
|
759
846
|
decl.id.type === 'Identifier' &&
|
|
@@ -798,14 +885,16 @@ async function analyze(id, code, job) {
|
|
|
798
885
|
}
|
|
799
886
|
if (staticChildNode && parent)
|
|
800
887
|
await backtrack(parent, this);
|
|
801
|
-
}
|
|
888
|
+
},
|
|
802
889
|
});
|
|
803
890
|
await assetEmissionPromises;
|
|
804
891
|
return { assets, deps, imports, isESM };
|
|
805
892
|
async function emitAssetPath(assetPath) {
|
|
806
893
|
// verify the asset file / directory exists
|
|
807
894
|
const wildcardIndex = assetPath.indexOf(static_eval_1.WILDCARD);
|
|
808
|
-
const dirIndex = wildcardIndex === -1
|
|
895
|
+
const dirIndex = wildcardIndex === -1
|
|
896
|
+
? assetPath.length
|
|
897
|
+
: assetPath.lastIndexOf(path_1.default.sep, wildcardIndex);
|
|
809
898
|
const basePath = assetPath.substring(0, dirIndex);
|
|
810
899
|
try {
|
|
811
900
|
var stats = await job.stat(basePath);
|
|
@@ -848,30 +937,46 @@ async function analyze(id, code, job) {
|
|
|
848
937
|
return false;
|
|
849
938
|
// do not emit asset directories higher than the node_modules base if a package
|
|
850
939
|
if (pkgBase) {
|
|
851
|
-
const nodeModulesBase = id.substring(0, id.indexOf(path_1.default.sep + 'node_modules')) +
|
|
940
|
+
const nodeModulesBase = id.substring(0, id.indexOf(path_1.default.sep + 'node_modules')) +
|
|
941
|
+
path_1.default.sep +
|
|
942
|
+
'node_modules' +
|
|
943
|
+
path_1.default.sep;
|
|
852
944
|
if (!assetPath.startsWith(nodeModulesBase)) {
|
|
853
945
|
if (job.log)
|
|
854
|
-
console.log('Skipping asset emission of ' +
|
|
946
|
+
console.log('Skipping asset emission of ' +
|
|
947
|
+
assetPath.replace(static_eval_1.wildcardRegEx, '*') +
|
|
948
|
+
' for ' +
|
|
949
|
+
id +
|
|
950
|
+
' as it is outside the package base ' +
|
|
951
|
+
pkgBase);
|
|
855
952
|
return false;
|
|
856
953
|
}
|
|
857
954
|
}
|
|
858
955
|
return true;
|
|
859
956
|
}
|
|
860
957
|
function resolveAbsolutePathOrUrl(value) {
|
|
861
|
-
return value instanceof url_1.URL
|
|
958
|
+
return value instanceof url_1.URL
|
|
959
|
+
? (0, url_1.fileURLToPath)(value)
|
|
960
|
+
: value.startsWith('file:')
|
|
961
|
+
? (0, url_1.fileURLToPath)(new url_1.URL(value))
|
|
962
|
+
: path_1.default.resolve(value);
|
|
862
963
|
}
|
|
863
964
|
async function emitStaticChildAsset() {
|
|
864
965
|
if (!staticChildValue) {
|
|
865
966
|
return;
|
|
866
967
|
}
|
|
867
|
-
if ('value' in staticChildValue &&
|
|
968
|
+
if ('value' in staticChildValue &&
|
|
969
|
+
isAbsolutePathOrUrl(staticChildValue.value)) {
|
|
868
970
|
try {
|
|
869
971
|
const resolved = resolveAbsolutePathOrUrl(staticChildValue.value);
|
|
870
972
|
await emitAssetPath(resolved);
|
|
871
973
|
}
|
|
872
974
|
catch (e) { }
|
|
873
975
|
}
|
|
874
|
-
else if ('then' in staticChildValue &&
|
|
976
|
+
else if ('then' in staticChildValue &&
|
|
977
|
+
'else' in staticChildValue &&
|
|
978
|
+
isAbsolutePathOrUrl(staticChildValue.then) &&
|
|
979
|
+
isAbsolutePathOrUrl(staticChildValue.else)) {
|
|
875
980
|
let resolvedThen;
|
|
876
981
|
try {
|
|
877
982
|
resolvedThen = resolveAbsolutePathOrUrl(staticChildValue.then);
|
|
@@ -887,7 +992,10 @@ async function analyze(id, code, job) {
|
|
|
887
992
|
if (resolvedElse)
|
|
888
993
|
await emitAssetPath(resolvedElse);
|
|
889
994
|
}
|
|
890
|
-
else if (staticChildNode &&
|
|
995
|
+
else if (staticChildNode &&
|
|
996
|
+
staticChildNode.type === 'ArrayExpression' &&
|
|
997
|
+
'value' in staticChildValue &&
|
|
998
|
+
staticChildValue.value instanceof Array) {
|
|
891
999
|
for (const value of staticChildValue.value) {
|
|
892
1000
|
try {
|
|
893
1001
|
const resolved = resolveAbsolutePathOrUrl(value);
|
|
@@ -900,7 +1008,6 @@ async function analyze(id, code, job) {
|
|
|
900
1008
|
}
|
|
901
1009
|
}
|
|
902
1010
|
exports.default = analyze;
|
|
903
|
-
;
|
|
904
1011
|
function isAst(ast) {
|
|
905
1012
|
return 'body' in ast;
|
|
906
1013
|
}
|