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(commonjs): __moduleExports in multi entry + inter dependencies #415

Merged
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 packages/commonjs/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures/form/output*.js
5 changes: 4 additions & 1 deletion packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ export default function commonjs(options = {}) {
return null;
}

const moduleInfo = this.getModuleInfo(id);

const transformed = transformCommonjs(
this.parse,
code,
id,
this.getModuleInfo(id).isEntry,
moduleInfo.isEntry,
moduleInfo.importers && moduleInfo.importers.length > 0,
isEsModule,
ignoreGlobal || isEsModule,
ignoreRequire,
Expand Down
8 changes: 5 additions & 3 deletions packages/commonjs/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function transformCommonjs(
code,
id,
isEntry,
hasImporters,
isEsModule,
ignoreGlobal,
ignoreRequire,
Expand Down Expand Up @@ -564,7 +565,7 @@ export function transformCommonjs(
let wrapperEnd = '';

const moduleName = deconflict(scope, globals, getName(id));
if (!isEntry && !isEsModule) {
if ((!isEntry || hasImporters) && !isEsModule) {
const exportModuleExports = {
str: `export { ${moduleName} as __moduleExports };`,
name: '__moduleExports'
Expand Down Expand Up @@ -634,7 +635,7 @@ export function transformCommonjs(
}
}

if (!hasDefaultExport && (names.length || (!isEntry && !isEsModule))) {
if (!hasDefaultExport && (names.length || ((!isEntry || hasImporters) && !isEsModule))) {
wrapperEnd = `\n\nvar ${moduleName} = {\n${names
.map(({ name, deconflicted }) => `\t${name}: ${deconflicted}`)
.join(',\n')}\n};`;
Expand All @@ -660,7 +661,8 @@ export function transformCommonjs(
.trim()
.append(wrapperEnd);

const injectExportBlock = hasDefaultExport || named.length > 0 || shouldWrap || !isEntry;
const injectExportBlock =
hasDefaultExport || named.length > 0 || shouldWrap || !isEntry || hasImporters;
if (injectExportBlock) {
magicString.append(exportBlock);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
multi: {
output1: 'input1.js',
output2: 'input2.js'
},
importers: {
output2: ['input1.js']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const t2 = require('./input2.js');

console.log(t2);
module.exports = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
a: 2
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import './input2.js';
import t2 from '_./input2.js?commonjs-proxy';

console.log(t2);
var input1 = 1;

export default input1;
export { input1 as __moduleExports };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you said we cannot get rid of __moduleExports because it will trigger interop handlers when using the default export? The thing is, this will now taint entry points in a way that users will notice there is suddenly a new named export that does not belong here. So one could wonder if for entry points, we might rather use the default even though it triggers interop? But I am undecided, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this only happens for entry points that are being imported by other code paths in the bundle.
So in most use cases - it will not be exported.
And in the use cases where it does - it's due to its use by one of the other entry points.

Unfortunately I don't see any way around it without breaking code... But if I think of some other trick to get rid of this, I'll make a PR :-)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var input2 = {
a: 2
};

export default input2;
export { input2 as __moduleExports };
65 changes: 40 additions & 25 deletions packages/commonjs/test/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,50 @@ readdirSync('./fixtures/form').forEach((dir) => {
config = {};
}

const inputEntries = [];

if (typeof config.multi === 'object') {
for (const [key, entry] of Object.entries(config.multi)) {
inputEntries.push([key, `fixtures/form/${dir}/${entry}`]);
}
} else {
inputEntries.push(['output', `fixtures/form/${dir}/input.js`]);
}

(config.solo ? test.only : test)(dir, async (t) => {
const { transform } = commonjs(config.options);
const id = `./fixtures/form/${dir}/input.js`;
for (const [outputName, id] of inputEntries) {
const { transform } = commonjs(config.options);

transformContext.getModuleInfo = (moduleId) => {
return {
isEntry: config.entry && moduleId === id
transformContext.getModuleInfo = (moduleId) => {
return {
isEntry: config.entry && moduleId === id,
importers:
config.importers && config.importers[outputName]
? config.importers[outputName].map((x) => `fixtures/form/${dir}/${x}`)
: []
};
};
};
transformContext.error = (base, props) => {
let error = base;
if (!(base instanceof Error)) error = Object.assign(new Error(base.message), base);
if (props) Object.assign(error, props);
throw error;
};

const input = readFileSync(id, 'utf-8');

let outputFile = `fixtures/form/${dir}/output`;
if (existsSync(`${outputFile}.${process.platform}.js`)) {
outputFile += `.${process.platform}.js`;
} else {
outputFile += '.js';
}
transformContext.error = (base, props) => {
let error = base;
if (!(base instanceof Error)) error = Object.assign(new Error(base.message), base);
if (props) Object.assign(error, props);
throw error;
};

const input = readFileSync(id, 'utf-8');

const expected = readFileSync(outputFile, 'utf-8').trim();
const transformed = transform.call(transformContext, input, id);
const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');
let outputFile = `fixtures/form/${dir}/${outputName}`;
if (existsSync(`${outputFile}.${process.platform}.js`)) {
outputFile += `.${process.platform}.js`;
} else {
outputFile += '.js';
}

t.is(actual, expected);
const expected = readFileSync(outputFile, 'utf-8').trim();
const transformed = transform.call(transformContext, input, id);
const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');

t.is(actual, expected);
}
});
});
2 changes: 1 addition & 1 deletion packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The actual snapshot is saved in `function.js.snap`.

Generated by [AVA](https://ava.li).

## \_\_esModule
## __esModule

> Snapshot 1

Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.