Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support resolving module.register dependencies #429

Merged
merged 16 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 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 @@ -94,6 +95,10 @@ const fsExtraSymbols = {
readJsonSync: FS_FN,
readJSONSync: FS_FN,
};
const MODULE_FN = Symbol();
const moduleSymbols = {
register: MODULE_FN,
};
const staticModules = Object.assign(Object.create(null), {
bindings: {
default: BINDINGS,
Expand All @@ -111,6 +116,10 @@ const staticModules = Object.assign(Object.create(null), {
default: fsSymbols,
...fsSymbols,
},
module: {
default: moduleSymbols,
...moduleSymbols,
},
'fs-extra': {
default: fsExtraSymbols,
...fsExtraSymbols,
Expand All @@ -131,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 @@ -530,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 @@ -876,6 +893,41 @@ export default async function analyze(
pjsonPath = path.resolve(pjsonPath, '../../package.json');
if (pjsonPath !== rootPjson) assets.add(pjsonPath);
break;
case MODULE_FN:
if (
node.arguments.length > 1 &&
node.arguments[0].type === 'Literal'
) {
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;
timfish marked this conversation as resolved.
Show resolved Hide resolved

imports.add(relativeSrcPath);
}
} else {
// It's a bare specifier
imports.add(pathOrSpecifier);
}
}
break;
}
}
} else if (
Expand Down
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')
timfish marked this conversation as resolved.
Show resolved Hide resolved
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));
timfish marked this conversation as resolved.
Show resolved Hide resolved
Loading