Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 535e811

Browse files
opichalsshellscape
authored andcommitted
fix: avoid ES6 export for entry files w/o exports (#351)
No ES6 export is added to _entry_ files containing no CommonJS export. E.g. this: ``` var dummy = require('./dummy'); var input = 42; ``` Is now transformed into the following containing no ES6 export: ``` import './dummy'; import dummy from 'commonjs-proxy:./dummy'; var input = 42; ```
1 parent aac6e5a commit 535e811

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

src/transform.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export function transformCommonjs(
459459
}
460460
});
461461

462-
if (!hasDefaultExport) {
462+
if (!hasDefaultExport && (names.length || !isEntry)) {
463463
wrapperEnd = `\n\nvar ${moduleName} = {\n${names
464464
.map(({ name, deconflicted }) => `\t${name}: ${deconflicted}`)
465465
.join(',\n')}\n};`;
@@ -488,7 +488,11 @@ export function transformCommonjs(
488488
.trim()
489489
.prepend(importBlock + wrapperStart)
490490
.trim()
491-
.append(wrapperEnd + exportBlock);
491+
.append(wrapperEnd);
492+
493+
if (hasDefaultExport || named.length > 0 || shouldWrap || !isEntry) {
494+
magicString.append(exportBlock);
495+
}
492496

493497
code = magicString.toString();
494498
const map = sourceMap ? magicString.generateMap() : null;

test/form/no-exports-entry/_config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
options: {
3+
ignore: [ 'foo' ]
4+
},
5+
entry: './input.js'
6+
};

test/form/no-exports-entry/input.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var dummy = require('./dummy');
2+
3+
var foo = function () {
4+
return;
5+
};
6+
7+
var input = 42;

test/form/no-exports-entry/output.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import './dummy';
2+
import dummy from '_./dummy?commonjs-proxy';
3+
4+
var foo = function () {
5+
return;
6+
};
7+
8+
var input = 42;

test/test.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,22 @@ async function executeBundle(bundle, { context, exports } = {}) {
6969
return execute(code, context);
7070
}
7171

72-
const transformContext = {
73-
getModuleInfo(id) {
74-
return {
75-
isEntry: id === 'main.js'
76-
};
77-
},
78-
parse: (input, options) =>
79-
acorn.parse(
80-
input,
81-
Object.assign(
82-
{
83-
ecmaVersion: 9,
84-
sourceType: 'module'
85-
},
86-
options
87-
)
88-
)
89-
};
90-
9172
describe('rollup-plugin-commonjs', () => {
9273
describe('form', () => {
74+
const transformContext = {
75+
parse: (input, options) =>
76+
acorn.parse(
77+
input,
78+
Object.assign(
79+
{
80+
ecmaVersion: 9,
81+
sourceType: 'module'
82+
},
83+
options
84+
)
85+
)
86+
};
87+
9388
fs.readdirSync('form').forEach(dir => {
9489
let config;
9590

@@ -101,8 +96,13 @@ describe('rollup-plugin-commonjs', () => {
10196

10297
(config.solo ? it.only : it)(dir, () => {
10398
const { transform } = commonjs(config.options);
99+
const id = `./form/${dir}/input.js`;
104100

105-
const input = fs.readFileSync(`form/${dir}/input.js`, 'utf-8');
101+
transformContext.getModuleInfo = moduleId => ({
102+
isEntry: config.entry && moduleId === id
103+
});
104+
105+
const input = fs.readFileSync(id, 'utf-8');
106106

107107
let outputFile = `form/${dir}/output`;
108108
if (fs.existsSync(`${outputFile}.${process.platform}.js`)) {
@@ -112,7 +112,8 @@ describe('rollup-plugin-commonjs', () => {
112112
}
113113

114114
const expected = fs.readFileSync(outputFile, 'utf-8').trim();
115-
const transformed = transform.call(transformContext, input, 'input.js');
115+
116+
const transformed = transform.call(transformContext, input, id);
116117
const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');
117118
assert.equal(actual, expected);
118119
});

0 commit comments

Comments
 (0)