Skip to content

Commit

Permalink
Statically analyse parentURL
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jun 27, 2024
1 parent db7751d commit d20d3c1
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 29 deletions.
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"graphql": "^14.4.2",
"highlights": "^3.1.6",
"hot-shots": "^6.3.0",
"import-in-the-middle": "^1.8.1",
"ioredis": "^4.11.1",
"isomorphic-unfetch": "^3.0.0",
"jest": "^27.4.5",
Expand Down
48 changes: 42 additions & 6 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const acorn = Parser.extend(
);

import os from 'os';
import url from 'url';
import { handleWrappers } from './utils/wrappers';
import resolveFrom from 'resolve-from';
import {
Expand Down Expand Up @@ -67,7 +68,6 @@ const FS_FN = Symbol();
const FS_DIR_FN = Symbol();
const BINDINGS = Symbol();
const NODE_GYP_BUILD = Symbol();
const MODULE_FN = Symbol();
const fsSymbols = {
access: FS_FN,
accessSync: FS_FN,
Expand Down Expand Up @@ -95,6 +95,7 @@ const fsExtraSymbols = {
readJsonSync: FS_FN,
readJSONSync: FS_FN,
};
const MODULE_FN = Symbol();
const moduleSymbols = {
register: MODULE_FN,
};
Expand Down Expand Up @@ -139,6 +140,10 @@ const staticModules = Object.assign(Object.create(null), {
default: os,
...os,
},
url: {
default: url,
...url,
},
'@mapbox/node-pre-gyp': {
default: mapboxPregyp,
...mapboxPregyp,
Expand Down Expand Up @@ -538,15 +543,19 @@ export default async function analyze(
let computed = await computePureStaticValue(expression, true);
if (!computed) return;

function add(value: string) {
(isImport ? imports : deps).add(value);
}

if ('value' in computed && typeof computed.value === 'string') {
if (!computed.wildcards) (isImport ? imports : deps).add(computed.value);
if (!computed.wildcards) add(computed.value);
else if (computed.wildcards.length >= 1)
emitWildcardRequire(computed.value);
} else {
if ('then' in computed && typeof computed.then === 'string')
(isImport ? imports : deps).add(computed.then);
add(computed.then);
if ('else' in computed && typeof computed.else === 'string')
(isImport ? imports : deps).add(computed.else);
add(computed.else);
}
}

Expand Down Expand Up @@ -886,10 +895,37 @@ export default async function analyze(
break;
case MODULE_FN:
if (
node.arguments.length &&
node.arguments.length > 1 &&
node.arguments[0].type === 'Literal'
) {
await processRequireArg(node.arguments[0]);
const pathOrSpecifier = node.arguments[0].value;
// It's a relative URL
if (pathOrSpecifier.startsWith('.')) {
// Compute the parentURL if it's statically analyzable
const computedParentURL = await computePureStaticValue(
node.arguments[1],
);

if (computedParentURL && 'value' in computedParentURL) {
const parentURL =
computedParentURL.value instanceof URL
? computedParentURL.value.href
: computedParentURL.value;
// Resolve the path from the parentURL
const srcURL = new URL(pathOrSpecifier, parentURL).href;

const cwd = path.dirname(parentURL);
const srcPath = path.relative(cwd, srcURL);
const relativeSrcPath = srcPath.startsWith('.')
? srcPath
: './' + srcPath;

imports.add(relativeSrcPath);
}
} else {
// It's a bare specifier
imports.add(pathOrSpecifier);
}
}
break;
}
Expand Down
3 changes: 0 additions & 3 deletions test/integration/module-register.mjs

This file was deleted.

1 change: 1 addition & 0 deletions test/unit/module-register/hook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hook.mjs');
1 change: 1 addition & 0 deletions test/unit/module-register/hook2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hook2.mjs');
1 change: 1 addition & 0 deletions test/unit/module-register/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./register.mjs')
7 changes: 7 additions & 0 deletions test/unit/module-register/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"package.json",
"test/unit/module-register/hook.mjs",
"test/unit/module-register/hook2.mjs",
"test/unit/module-register/input.js",
"test/unit/module-register/register.mjs"
]
5 changes: 5 additions & 0 deletions test/unit/module-register/register.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { register } from 'module';
import { pathToFileURL } from 'url';

register('./hook.mjs', import.meta.url);
register('./hook2.mjs', pathToFileURL(__filename));

0 comments on commit d20d3c1

Please sign in to comment.