Skip to content

Commit

Permalink
fix(commonjs): __moduleExports in multi entry + inter dependencies (#415
Browse files Browse the repository at this point in the history
)

* fix(commonjs): __moduleExports in multi entry + inter dependencies

* Added form tests outputs to .prettierignore
  • Loading branch information
danielgindi authored Jun 1, 2020
1 parent 7c38cb2 commit 7ae5390
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 30 deletions.
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 };
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.

0 comments on commit 7ae5390

Please sign in to comment.