Skip to content

Commit 5d2dcf4

Browse files
feat(commonjs): set syntheticNamedExports for commonjs modules (#149)
BREAKING CHANGE: The namedExports option has been removed, now requires rollup >= 2.3.4. Instead of manually defining named exports, rollup now handles this automatically for you. * feat(commonjs): set syntheticNamedExports for commonjs modules * feat(commonjs): remove namedExports option * chore: merge with upstream * chore(commonjs): restrict rollup version * chore(commonjs): remove unused fixtures
1 parent c09509f commit 5d2dcf4

File tree

38 files changed

+265
-292
lines changed

38 files changed

+265
-292
lines changed

β€Žpackages/commonjs/README.md

-41
Original file line numberDiff line numberDiff line change
@@ -106,47 +106,6 @@ Default: `true`
106106

107107
If false, skips source map generation for CommonJS modules.
108108

109-
### `namedExports`
110-
111-
Type: `Object`<br>
112-
Default: `null`
113-
114-
Explicitly specify unresolvable named exports.
115-
116-
This plugin will attempt to create named exports, where appropriate, so you can do this...
117-
118-
```js
119-
// importer.js
120-
import { named } from './exporter.js';
121-
122-
// exporter.js
123-
module.exports = { named: 42 }; // or `exports.named = 42;`
124-
```
125-
126-
...but that's not always possible:
127-
128-
```js
129-
// importer.js
130-
import { named } from 'my-lib';
131-
132-
// my-lib.js
133-
var myLib = exports;
134-
myLib.named = "you can't see me";
135-
```
136-
137-
In those cases, you can specify custom named exports:
138-
139-
```js
140-
commonjs({
141-
namedExports: {
142-
// left-hand side can be an absolute path, a path
143-
// relative to the current directory, or the name
144-
// of a module in node_modules
145-
'my-lib': ['named']
146-
}
147-
});
148-
```
149-
150109
### `ignore`
151110

152111
Type: `Array[...String | (String) => Boolean]`<br>

β€Žpackages/commonjs/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"require"
4747
],
4848
"peerDependencies": {
49-
"rollup": "^1.20.0||^2.0.0"
49+
"rollup": "^2.3.4"
5050
},
5151
"dependencies": {
5252
"@rollup/pluginutils": "^3.0.8",
@@ -67,7 +67,7 @@
6767
"locate-character": "^2.0.5",
6868
"prettier": "^1.19.1",
6969
"require-relative": "^0.8.7",
70-
"rollup": "^2.0.0",
70+
"rollup": "^2.3.4",
7171
"rollup-plugin-babel": "^4.3.3",
7272
"shx": "^0.3.2",
7373
"source-map": "^0.6.1",

β€Žpackages/commonjs/src/index.js

+8-40
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { realpathSync, existsSync, readFileSync } from 'fs';
2-
import { extname, resolve, normalize, join } from 'path';
1+
import { existsSync, readFileSync } from 'fs';
2+
import { extname, join } from 'path';
33

4-
import { sync as nodeResolveSync, isCore } from 'resolve';
54
import { createFilter } from '@rollup/pluginutils';
65

76
import getCommonDir from 'commondir';
@@ -48,40 +47,6 @@ export default function commonjs(options = {}) {
4847
? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd()))
4948
: null;
5049

51-
const customNamedExports = {};
52-
if (options.namedExports) {
53-
Object.keys(options.namedExports).forEach((id) => {
54-
let resolveId = id;
55-
let resolvedId;
56-
57-
if (isCore(id)) {
58-
// resolve will not find npm modules with the same name as
59-
// core modules without a trailing slash. Since core modules
60-
// must be external, we can assume any core modules defined
61-
// here are npm modules by that name.
62-
resolveId += '/';
63-
}
64-
65-
try {
66-
resolvedId = nodeResolveSync(resolveId, { basedir: process.cwd() });
67-
} catch (err) {
68-
resolvedId = resolve(id);
69-
}
70-
71-
// Note: customNamedExport's keys must be normalized file paths.
72-
// resolve and nodeResolveSync both return normalized file paths
73-
// so no additional normalization is necessary.
74-
customNamedExports[resolvedId] = options.namedExports[id];
75-
76-
if (existsSync(resolvedId)) {
77-
const realpath = realpathSync(resolvedId);
78-
if (realpath !== resolvedId) {
79-
customNamedExports[realpath] = options.namedExports[id];
80-
}
81-
}
82-
});
83-
}
84-
8550
const esModulesWithoutDefaultExport = new Set();
8651
const esModulesWithDefaultExport = new Set();
8752

@@ -111,8 +76,6 @@ export default function commonjs(options = {}) {
11176
return null;
11277
}
11378

114-
const normalizedId = normalize(id);
115-
11679
const transformed = transformCommonjs(
11780
this.parse,
11881
code,
@@ -121,7 +84,6 @@ export default function commonjs(options = {}) {
12184
isEsModule,
12285
ignoreGlobal || isEsModule,
12386
ignoreRequire,
124-
customNamedExports[normalizedId],
12587
sourceMap,
12688
isDynamicRequireModulesEnabled,
12789
dynamicRequireModuleSet,
@@ -143,6 +105,12 @@ export default function commonjs(options = {}) {
143105
name: 'commonjs',
144106

145107
buildStart() {
108+
if (options.namedExports != null) {
109+
this.warn(
110+
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
111+
);
112+
}
113+
146114
const [major, minor] = this.meta.rollupVersion.split('.').map(Number);
147115
const minVersion = peerDependencies.rollup.slice(2);
148116
const [minMajor, minMinor] = minVersion.split('.').map(Number);

β€Žpackages/commonjs/src/transform.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export function transformCommonjs(
109109
isEsModule,
110110
ignoreGlobal,
111111
ignoreRequire,
112-
customNamedExports,
113112
sourceMap,
114113
isDynamicRequireModulesEnabled,
115114
dynamicRequireModuleSet,
@@ -562,8 +561,6 @@ export function transformCommonjs(
562561
});
563562
}
564563

565-
if (customNamedExports) customNamedExports.forEach(addExport);
566-
567564
const defaultExportPropertyAssignments = [];
568565
let hasDefaultExport = false;
569566

@@ -648,12 +645,13 @@ export function transformCommonjs(
648645
.trim()
649646
.append(wrapperEnd);
650647

651-
if (hasDefaultExport || named.length > 0 || shouldWrap || (!isEntry && !isEsModule)) {
648+
const injectExportBlock = hasDefaultExport || named.length > 0 || shouldWrap || !isEntry;
649+
if (injectExportBlock) {
652650
magicString.append(exportBlock);
653651
}
654652

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

658-
return { code, map };
656+
return { code, map, syntheticNamedExports: injectExportBlock };
659657
}

β€Žpackages/commonjs/test/fixtures/.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"func-names": "off",
77
"no-console": "off",
88
"no-undefined": "off",
9+
"no-undef": "off",
910
"import/prefer-default-export": "off",
1011
"import/extensions": "off",
1112
"import/no-unresolved": "off",

β€Žpackages/commonjs/test/fixtures/function/__esModule/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ import * as x from './answer';
22

33
t.truthy('answer' in x);
44
t.truthy('default' in x);
5-
t.truthy(!('__esModule' in x));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { named } from './x.js';
2+
3+
t.is(named, 'foo');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (typeof someUnknownGlobal !== 'undefined') {
2+
module.exports = { named: 'bar' };
3+
} else {
4+
module.exports = { named: 'foo' };
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
context: {
3+
window: {}
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { named } from './x.js';
2+
3+
t.is(named, undefined);
4+
5+
window.addExport('named', 'foo');
6+
7+
t.is(named, 'foo');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.addExport = (key, value) => {
2+
module.exports[key] = value;
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { named } from './x.js';
2+
3+
t.is(named, 'foo');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Object.defineProperty(module.exports, 'named', {
2+
enumerable: true,
3+
get: function get() {
4+
return 'foo';
5+
}
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.named = 2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { named } from './reexport.js';
2+
3+
t.is(named, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const myModule = require('./export.js');
2+
3+
module.exports.named = myModule.named;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { nonExisting } from './x.js';
2+
3+
t.is(nonExisting, undefined);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.named = 2;

β€Žpackages/commonjs/test/fixtures/function/reexports/_config.js

-9
This file was deleted.

β€Žpackages/commonjs/test/fixtures/function/reexports/bar.js

-1
This file was deleted.

β€Žpackages/commonjs/test/fixtures/function/reexports/foo.js

-1
This file was deleted.

β€Žpackages/commonjs/test/fixtures/function/reexports/main.js

-3
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/custom-named-exports-browser-shims/main.js

-4
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/custom-named-exports-false-positive/main.js

-3
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/custom-named-exports-false-positive/other.js

-3
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/custom-named-exports-warn-builtins/main.js

-3
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/custom-named-exports/main.js

-6
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/custom-named-exports/secret-named-exporter.js

-2
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/define-property/foo.js

-6
This file was deleted.

β€Žpackages/commonjs/test/fixtures/samples/define-property/main.js

-1
This file was deleted.

0 commit comments

Comments
Β (0)