Skip to content
Open
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
17 changes: 13 additions & 4 deletions packages/commonjs/src/generate-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function rewriteExportsAndGetExportsBlock(
exportMode,
defaultIsModuleExports,
usesRequireWrapper,
requireName
requireName,
usesTopLevelThis
) {
const exports = [];
const exportDeclarations = [];
Expand All @@ -62,7 +63,8 @@ export function rewriteExportsAndGetExportsBlock(
firstTopLevelModuleExportsAssignment,
exportsName,
defaultIsModuleExports,
HELPERS_NAME
HELPERS_NAME,
usesTopLevelThis
);
} else {
if (exportMode === 'module') {
Expand Down Expand Up @@ -149,12 +151,19 @@ function getExportsForReplacedModuleExports(
firstTopLevelModuleExportsAssignment,
exportsName,
defaultIsModuleExports,
HELPERS_NAME
HELPERS_NAME,
usesTopLevelThis
) {
for (const { left } of moduleExportsAssignments) {
magicString.overwrite(left.start, left.end, exportsName);
}
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
if (usesTopLevelThis) {
// Top-level `this` is rewritten to exportsName and may run before the
// assignment, so the variable must already hold Node's initial `{}`.
magicString.prepend(`var ${exportsName} = {};\n`);
} else {
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
}
exports.push(`${exportsName} as __moduleExports`);
exportDeclarations.push(
getDefaultExportDeclaration(exportsName, defaultIsModuleExports, HELPERS_NAME)
Expand Down
10 changes: 8 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ export default async function transformCommonjs(
exportMode,
defaultIsModuleExports,
usesRequireWrapper,
requireName
requireName,
replacedThis.length > 0
);

if (shouldWrap) {
Expand All @@ -568,13 +569,18 @@ export default async function transformCommonjs(
exclude: indentExclusionRanges
});
const exported = exportMode === 'module' ? `${moduleName}.exports` : exportsName;
// A replaced module.exports starts from an empty object like it does in Node,
// so that a circular require or a top-level `this` before the
// `module.exports =` assignment sees `{}`. It is set when the first require
// starts, so a value that a re-entrant require already completed is kept.
const initialExports = exportMode === 'replace' ? `\t${exportsName} = {};\n` : '';
magicString.prepend(
`var ${isRequiredName};

function ${requireName} () {
\tif (${isRequiredName}) return ${exported};
\t${isRequiredName} = 1;
`
${initialExports}`
).append(`
\treturn ${exported};
}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = async function () {
// TODO
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = { __esModule: true, default: { foo: 'bar' }}
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = { default: { foo: 'bar' }}
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = { __esModule: true, default: { foo: 'bar' }};
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = { default: { foo: 'bar' }};
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = { __esModule: true, default: { foo: 'bar' }};
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = { default: { foo: 'bar' }};
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var hasRequiredInput1;
function requireInput1 () {
if (hasRequiredInput1) return input1;
hasRequiredInput1 = 1;
input1 = {};
const t2 = require$$0;

console.log(t2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput2;
function requireInput2 () {
if (hasRequiredInput2) return input2;
hasRequiredInput2 = 1;
input2 = {};
input2 = {
a: 2
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
var foo = function () {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
var bar;
input = bar = function foo () {};
return input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = function foo () {};
return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = (function foo () {
return function fooChild() {};
}());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var hasRequiredInput;
function requireInput () {
if (hasRequiredInput) return input;
hasRequiredInput = 1;
input = {};
input = 42;
return input;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
description:
'keeps a replaced module.exports that a re-entrant require completed through an ES module',
options: {
onwarn(warning) {
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
throw new Error(warning.message);
}
}
},
pluginOptions: {
strictRequires: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./b.js');

module.exports = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import c from './c.js';

export default c;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./a.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
t.is(require('./a.js'), 42);
t.is(require('./a.js'), 42);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
description:
'provides the initial exports object to a circular require before module.exports is replaced',
pluginOptions: {
strictRequires: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const b = require('./b.js');

module.exports = { seenByB: b.seen };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = require('./a.js');

module.exports = { seen: typeof a };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.deepEqual(require('./a.js'), { seenByB: 'object' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
description: 'allows top-level this before module.exports is replaced',
pluginOptions: {
strictRequires: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.deepEqual(require('./other.js'), { y: 2 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this.x = 1;

module.exports = { y: 2 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
description:
'allows top-level this before module.exports is replaced when strictRequires is auto',
pluginOptions: {
strictRequires: 'auto'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.deepEqual(require('./other.js'), { y: 2 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this.x = 1;

module.exports = { y: 2 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
description: 'allows top-level this before module.exports is replaced in an unwrapped module',
pluginOptions: {
strictRequires: false
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t.deepEqual(require('./other.js'), { y: 2 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this.x = 1;

module.exports = { y: 2 };
Loading