Skip to content

Commit

Permalink
feat(commonjs): set syntheticNamedExports for commonjs modules
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsDenBakker committed Jan 8, 2020
1 parent 16f8ba5 commit 6f40b47
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"pnpm": "^4.3.0",
"prettier": "^1.19.1",
"prettier-plugin-package": "^0.3.1",
"rollup": "^1.27.2",
"rollup": "^1.28.0",
"ts-node": "^8.5.2",
"tsconfig-paths": "^3.9.0",
"tslib": "^1.10.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"mocha": "^6.1.4",
"prettier": "^1.18.2",
"require-relative": "^0.8.7",
"rollup": "^1.16.2",
"rollup": "^1.28.0",
"rollup-plugin-babel": "^4.3.3",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-node-resolve": "^6.0.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/commonjs/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,13 @@ export function transformCommonjs(
.trim()
.append(wrapperEnd);

if (hasDefaultExport || named.length > 0 || shouldWrap || !isEntry) {
const injectExportBlock = hasDefaultExport || named.length > 0 || shouldWrap || !isEntry;
if (injectExportBlock) {
magicString.append(exportBlock);
}

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

return { code, map };
return { code, map, syntheticNamedExports: injectExportBlock };
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ import * as x from './answer';

t.truthy('answer' in x);
t.truthy('default' in x);
t.truthy(!('__esModule' in x));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { named } from './x.js';

t.is(named, 'foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (typeof someUnknownGlobal !== 'undefined') {
module.exports = { named: 'bar' };
} else {
module.exports = { named: 'foo' };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
context: {
window: { }
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { named } from './x.js';

t.is(named, undefined);

window.addExport('named', 'foo');

t.is(named, 'foo')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.addExport = (key, value) => {
module.exports[key] = value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { named } from './x.js';

t.is(named, 'foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Object.defineProperty(module.exports, 'named', {
enumerable: true,
get: function get() {
return 'foo';
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.named = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { named } from './reexport.js';

t.is(named, 2);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./export.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.named = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { named } from './reexport.js';

t.is(named, 2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const myModule = require('./export.js');

module.exports.named = myModule.named;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { nonExisting } from './x.js';

t.is(nonExisting, undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.named = 2;
1 change: 1 addition & 0 deletions packages/commonjs/test/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ readdirSync('./fixtures/form').forEach((dir) => {

const transformed = transform.call(transformContext, input, id);
const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');

t.is(actual, expected);
});
});
14 changes: 0 additions & 14 deletions packages/commonjs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,6 @@ test('does not process the entry file when it has a leading "." (issue #63)', as
await t.notThrowsAsync(executeBundle(bundle, t));
});

test('does not reexport named contents', async (t) => {
try {
await rollup({
input: 'fixtures/samples/reexport/main.js',
plugins: [commonjs()]
});
} catch (error) {
t.is(
error.message,
`'named' is not exported by fixtures/samples${path.sep}reexport${path.sep}reexport.js`
);
}
});

test('respects other plugins', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/other-transforms/main.js',
Expand Down
86 changes: 47 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f40b47

Please sign in to comment.