Skip to content

Commit bee1cd2

Browse files
authored
Add support for mixed ESM and CJS via mixedModules flag (#26)
* Mixded ESM and CJS * Use 2 spaces * Remove unused test * Ignore all `isESM` checks * Add a flag for `mixedModules`
1 parent e707318 commit bee1cd2

File tree

11 files changed

+24
-12
lines changed

11 files changed

+24
-12
lines changed

node-file-trace.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ interface NodeFileTraceOptions {
33
ignore?: string | string[] | ((path: string) => boolean);
44
ts?: boolean;
55
log?: boolean;
6+
mixedModules?: boolean;
67
readFile?: (path: string) => Buffer | string | null;
78
stat?: (path: string) => Object | null;
89
readlink?: (path: string) => string | null;

src/analyze.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ module.exports = async function (id, code, job) {
222222
}
223223
});
224224

225-
if (!isESM) {
225+
if (!isESM || job.mixedModules) {
226226
knownBindings.require = {
227227
shadowDepth: 0,
228228
value: {
@@ -261,7 +261,7 @@ module.exports = async function (id, code, job) {
261261
return binding && binding.shadowDepth === 0;
262262
}
263263

264-
if (isESM) {
264+
if (isESM || job.mixedModules) {
265265
for (const decl of ast.body) {
266266
if (decl.type === 'ImportDeclaration') {
267267
const source = decl.source.value;
@@ -424,13 +424,13 @@ module.exports = async function (id, code, job) {
424424
// - nodegyp()
425425
// - etc.
426426
else if (node.type === 'CallExpression') {
427-
if (!isESM && node.callee.type === 'Identifier' && node.arguments.length) {
427+
if ((!isESM || job.mixedModules) && node.callee.type === 'Identifier' && node.arguments.length) {
428428
if (node.callee.name === 'require' && knownBindings.require.shadowDepth === 0) {
429429
processRequireArg(node.arguments[0]);
430430
return;
431431
}
432432
}
433-
else if (!isESM &&
433+
else if ((!isESM || job.mixedModules) &&
434434
node.callee.type === 'MemberExpression' &&
435435
node.callee.object.type === 'Identifier' &&
436436
node.callee.object.name === 'module' &&
@@ -615,7 +615,7 @@ module.exports = async function (id, code, job) {
615615
}
616616
}
617617
// Support require wrappers like function p (x) { ...; var y = require(x); ...; return y; }
618-
else if (!isESM &&
618+
else if ((!isESM || job.mixedModules) &&
619619
(node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') &&
620620
(node.arguments || node.params)[0] && (node.arguments || node.params)[0].type === 'Identifier') {
621621
let fnName, args;

src/node-file-trace.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class Job {
4343
constructor ({
4444
base = process.cwd(),
4545
ignore,
46-
log = false
46+
log = false,
47+
mixedModules = false,
4748
}) {
4849
base = resolve(base);
4950
this.ignoreFn = path => {
@@ -68,6 +69,7 @@ class Job {
6869
}
6970
this.base = base;
7071
this.log = log;
72+
this.mixedModules = mixedModules;
7173
this.reasons = Object.create(null);
7274

7375
this.fileCache = new Map();

test/unit.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
2929
base: `${__dirname}/../`,
3030
ts: true,
3131
log: true,
32+
mixedModules: true,
3233
ignore: '**/actual.js'
3334
});
3435
let expected;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { dep1: 'dep1' };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const dep2 = 'dep2';

test/unit/mixed-esm-cjs/input.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { dep1 } = require('./commonjs-module');
2+
import { dep2 } from './ecmascript-module';
3+
4+
if (dep1 && dep2) {
5+
console.log(dep1);
6+
console.log(dep2);
7+
}

test/unit/mixed-esm-cjs/output.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"test/unit/mixed-esm-cjs/commonjs-module.js",
3+
"test/unit/mixed-esm-cjs/ecmascript-module.js",
4+
"test/unit/mixed-esm-cjs/input.js"
5+
]

test/unit/require-esm/dep.js

Whitespace-only changes.

test/unit/require-esm/input.js

-3
This file was deleted.

test/unit/require-esm/output.js

-3
This file was deleted.

0 commit comments

Comments
 (0)