Skip to content

Add support for mixed ESM and CJS via mixedModules flag #26

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

Merged
merged 6 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions node-file-trace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface NodeFileTraceOptions {
ignore?: string | string[] | ((path: string) => boolean);
ts?: boolean;
log?: boolean;
mixedModules?: boolean;
readFile?: (path: string) => Buffer | string | null;
stat?: (path: string) => Object | null;
readlink?: (path: string) => string | null;
Expand Down
10 changes: 5 additions & 5 deletions src/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module.exports = async function (id, code, job) {
}
});

if (!isESM) {
if (!isESM || job.mixedModules) {
knownBindings.require = {
shadowDepth: 0,
value: {
Expand Down Expand Up @@ -261,7 +261,7 @@ module.exports = async function (id, code, job) {
return binding && binding.shadowDepth === 0;
}

if (isESM) {
if (isESM || job.mixedModules) {
for (const decl of ast.body) {
if (decl.type === 'ImportDeclaration') {
const source = decl.source.value;
Expand Down Expand Up @@ -424,13 +424,13 @@ module.exports = async function (id, code, job) {
// - nodegyp()
// - etc.
else if (node.type === 'CallExpression') {
if (!isESM && node.callee.type === 'Identifier' && node.arguments.length) {
if ((!isESM || job.mixedModules) && node.callee.type === 'Identifier' && node.arguments.length) {
if (node.callee.name === 'require' && knownBindings.require.shadowDepth === 0) {
processRequireArg(node.arguments[0]);
return;
}
}
else if (!isESM &&
else if ((!isESM || job.mixedModules) &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'module' &&
Expand Down Expand Up @@ -615,7 +615,7 @@ module.exports = async function (id, code, job) {
}
}
// Support require wrappers like function p (x) { ...; var y = require(x); ...; return y; }
else if (!isESM &&
else if ((!isESM || job.mixedModules) &&
(node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') &&
(node.arguments || node.params)[0] && (node.arguments || node.params)[0].type === 'Identifier') {
let fnName, args;
Expand Down
4 changes: 3 additions & 1 deletion src/node-file-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Job {
constructor ({
base = process.cwd(),
ignore,
log = false
log = false,
mixedModules = false,
}) {
base = resolve(base);
this.ignoreFn = path => {
Expand All @@ -68,6 +69,7 @@ class Job {
}
this.base = base;
this.log = log;
this.mixedModules = mixedModules;
this.reasons = Object.create(null);

this.fileCache = new Map();
Expand Down
1 change: 1 addition & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
base: `${__dirname}/../`,
ts: true,
log: true,
mixedModules: true,
ignore: '**/actual.js'
});
let expected;
Expand Down
1 change: 1 addition & 0 deletions test/unit/mixed-esm-cjs/commonjs-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { dep1: 'dep1' };
1 change: 1 addition & 0 deletions test/unit/mixed-esm-cjs/ecmascript-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const dep2 = 'dep2';
7 changes: 7 additions & 0 deletions test/unit/mixed-esm-cjs/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { dep1 } = require('./commonjs-module');
import { dep2 } from './ecmascript-module';

if (dep1 && dep2) {
console.log(dep1);
console.log(dep2);
}
5 changes: 5 additions & 0 deletions test/unit/mixed-esm-cjs/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"test/unit/mixed-esm-cjs/commonjs-module.js",
"test/unit/mixed-esm-cjs/ecmascript-module.js",
"test/unit/mixed-esm-cjs/input.js"
]
Empty file removed test/unit/require-esm/dep.js
Empty file.
3 changes: 0 additions & 3 deletions test/unit/require-esm/input.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/require-esm/output.js

This file was deleted.