diff --git a/package.json b/package.json index 4371f2d99..a730c8470 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/commonjs/package.json b/packages/commonjs/package.json index 9bacc6437..90fa4c87a 100644 --- a/packages/commonjs/package.json +++ b/packages/commonjs/package.json @@ -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", diff --git a/packages/commonjs/src/transform.js b/packages/commonjs/src/transform.js index 03060b455..c645306c4 100644 --- a/packages/commonjs/src/transform.js +++ b/packages/commonjs/src/transform.js @@ -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 }; } diff --git a/packages/commonjs/test/fixtures/function/__esModule/main.js b/packages/commonjs/test/fixtures/function/__esModule/main.js index be3be5c54..6ef314f3a 100644 --- a/packages/commonjs/test/fixtures/function/__esModule/main.js +++ b/packages/commonjs/test/fixtures/function/__esModule/main.js @@ -2,4 +2,3 @@ import * as x from './answer'; t.truthy('answer' in x); t.truthy('default' in x); -t.truthy(!('__esModule' in x)); diff --git a/packages/commonjs/test/fixtures/function/named-exports-conditional/main.js b/packages/commonjs/test/fixtures/function/named-exports-conditional/main.js new file mode 100644 index 000000000..b469d5837 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-conditional/main.js @@ -0,0 +1,3 @@ +import { named } from './x.js'; + +t.is(named, 'foo'); diff --git a/packages/commonjs/test/fixtures/function/named-exports-conditional/x.js b/packages/commonjs/test/fixtures/function/named-exports-conditional/x.js new file mode 100644 index 000000000..883fb344a --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-conditional/x.js @@ -0,0 +1,5 @@ +if (typeof someUnknownGlobal !== 'undefined') { + module.exports = { named: 'bar' }; +} else { + module.exports = { named: 'foo' }; +} diff --git a/packages/commonjs/test/fixtures/function/named-exports-dynamic/_config.js b/packages/commonjs/test/fixtures/function/named-exports-dynamic/_config.js new file mode 100644 index 000000000..b51a11062 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-dynamic/_config.js @@ -0,0 +1,5 @@ +module.exports = { + context: { + window: { } + } +}; diff --git a/packages/commonjs/test/fixtures/function/named-exports-dynamic/main.js b/packages/commonjs/test/fixtures/function/named-exports-dynamic/main.js new file mode 100644 index 000000000..d8f2b98b4 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-dynamic/main.js @@ -0,0 +1,7 @@ +import { named } from './x.js'; + +t.is(named, undefined); + +window.addExport('named', 'foo'); + +t.is(named, 'foo') diff --git a/packages/commonjs/test/fixtures/function/named-exports-dynamic/x.js b/packages/commonjs/test/fixtures/function/named-exports-dynamic/x.js new file mode 100644 index 000000000..025a40ad8 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-dynamic/x.js @@ -0,0 +1,3 @@ +window.addExport = (key, value) => { + module.exports[key] = value; +} diff --git a/packages/commonjs/test/fixtures/function/named-exports-object-define/main.js b/packages/commonjs/test/fixtures/function/named-exports-object-define/main.js new file mode 100644 index 000000000..b469d5837 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-object-define/main.js @@ -0,0 +1,3 @@ +import { named } from './x.js'; + +t.is(named, 'foo'); diff --git a/packages/commonjs/test/fixtures/function/named-exports-object-define/x.js b/packages/commonjs/test/fixtures/function/named-exports-object-define/x.js new file mode 100644 index 000000000..c8dcd764b --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-object-define/x.js @@ -0,0 +1,6 @@ +Object.defineProperty(module.exports, 'named', { + enumerable: true, + get: function get() { + return 'foo'; + } +}); diff --git a/packages/commonjs/test/fixtures/function/named-exports-reexport-default/export.js b/packages/commonjs/test/fixtures/function/named-exports-reexport-default/export.js new file mode 100644 index 000000000..5cb323723 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-reexport-default/export.js @@ -0,0 +1 @@ +exports.named = 2; diff --git a/packages/commonjs/test/fixtures/function/named-exports-reexport-default/main.js b/packages/commonjs/test/fixtures/function/named-exports-reexport-default/main.js new file mode 100644 index 000000000..28abb9a60 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-reexport-default/main.js @@ -0,0 +1,3 @@ +import { named } from './reexport.js'; + +t.is(named, 2); diff --git a/packages/commonjs/test/fixtures/function/named-exports-reexport-default/reexport.js b/packages/commonjs/test/fixtures/function/named-exports-reexport-default/reexport.js new file mode 100644 index 000000000..fb12c5825 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-reexport-default/reexport.js @@ -0,0 +1 @@ +module.exports = require('./export.js'); diff --git a/packages/commonjs/test/fixtures/function/named-exports-reexport-named/export.js b/packages/commonjs/test/fixtures/function/named-exports-reexport-named/export.js new file mode 100644 index 000000000..5cb323723 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-reexport-named/export.js @@ -0,0 +1 @@ +exports.named = 2; diff --git a/packages/commonjs/test/fixtures/function/named-exports-reexport-named/main.js b/packages/commonjs/test/fixtures/function/named-exports-reexport-named/main.js new file mode 100644 index 000000000..28abb9a60 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-reexport-named/main.js @@ -0,0 +1,3 @@ +import { named } from './reexport.js'; + +t.is(named, 2); diff --git a/packages/commonjs/test/fixtures/function/named-exports-reexport-named/reexport.js b/packages/commonjs/test/fixtures/function/named-exports-reexport-named/reexport.js new file mode 100644 index 000000000..ba56c12b5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-reexport-named/reexport.js @@ -0,0 +1,3 @@ +const myModule = require('./export.js'); + +module.exports.named = myModule.named; diff --git a/packages/commonjs/test/fixtures/function/named-exports-unexported/main.js b/packages/commonjs/test/fixtures/function/named-exports-unexported/main.js new file mode 100644 index 000000000..8b713b667 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-unexported/main.js @@ -0,0 +1,3 @@ +import { nonExisting } from './x.js'; + +t.is(nonExisting, undefined); diff --git a/packages/commonjs/test/fixtures/function/named-exports-unexported/x.js b/packages/commonjs/test/fixtures/function/named-exports-unexported/x.js new file mode 100644 index 000000000..5cb323723 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/named-exports-unexported/x.js @@ -0,0 +1 @@ +exports.named = 2; diff --git a/packages/commonjs/test/form.js b/packages/commonjs/test/form.js index 403d94634..5104f8636 100644 --- a/packages/commonjs/test/form.js +++ b/packages/commonjs/test/form.js @@ -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); }); }); diff --git a/packages/commonjs/test/test.js b/packages/commonjs/test/test.js index 255559db4..3ef10a624 100644 --- a/packages/commonjs/test/test.js +++ b/packages/commonjs/test/test.js @@ -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', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 63e358cae..25c5d7ffb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,7 +16,7 @@ importers: pnpm: 4.5.0 prettier: 1.19.1 prettier-plugin-package: 0.3.1_prettier@1.19.1 - rollup: 1.27.13 + rollup: 1.28.0 ts-node: 8.5.4_typescript@3.7.3 tsconfig-paths: 3.9.0 tslib: 1.10.0 @@ -38,7 +38,7 @@ importers: 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 @@ -101,7 +101,7 @@ importers: typescript: ^3.7.2 packages/commonjs: dependencies: - '@rollup/pluginutils': 3.0.0_rollup@1.27.11 + '@rollup/pluginutils': 3.0.1_rollup@1.28.0 estree-walker: 0.6.1 is-reference: 1.1.4 magic-string: 0.25.4 @@ -110,8 +110,8 @@ importers: '@babel/core': 7.7.5 '@babel/preset-env': 7.7.6_@babel+core@7.7.5 '@babel/register': 7.7.4_@babel+core@7.7.5 - '@rollup/plugin-json': 4.0.0_rollup@1.27.11 - '@rollup/plugin-node-resolve': 6.0.0_rollup@1.27.11 + '@rollup/plugin-json': 4.0.0_rollup@1.28.0 + '@rollup/plugin-node-resolve': 6.0.0_rollup@1.28.0 acorn: 6.4.0 eslint: 6.7.2 eslint-plugin-import: 2.19.1_eslint@6.7.2 @@ -121,8 +121,8 @@ importers: mocha: 6.2.2 prettier: 1.19.1 require-relative: 0.8.7 - rollup: 1.27.11 - rollup-plugin-babel: 4.3.3_@babel+core@7.7.5+rollup@1.27.11 + rollup: 1.28.0 + rollup-plugin-babel: 4.3.3_@babel+core@7.7.5+rollup@1.28.0 shx: 0.3.2 source-map: 0.6.1 source-map-support: 0.5.16 @@ -147,7 +147,7 @@ importers: prettier: ^1.18.2 require-relative: ^0.8.7 resolve: ^1.11.0 - rollup: ^1.16.2 + rollup: ^1.28.0 rollup-plugin-babel: ^4.3.3 shx: ^0.3.2 source-map: ^0.6.1 @@ -245,7 +245,7 @@ importers: rollup-plugin-babel: ^4.0.3 packages/node-resolve: dependencies: - '@rollup/pluginutils': 3.0.0_rollup@1.27.8 + '@rollup/pluginutils': 3.0.1_rollup@1.27.8 '@types/resolve': 0.0.8 builtin-modules: 3.1.0 is-module: 1.0.0 @@ -358,7 +358,7 @@ importers: sucrase: ^3.10.1 packages/typescript: dependencies: - '@rollup/pluginutils': 3.0.0_rollup@1.27.8 + '@rollup/pluginutils': 3.0.1_rollup@1.27.8 resolve: 1.13.1 devDependencies: '@rollup/plugin-buble': 0.20.0_rollup@1.27.8 @@ -1804,32 +1804,32 @@ packages: rollup: ^1.20.0 resolution: integrity: sha512-3Qkoa3n+6NjQggLkN5R6ouVL3/jveyqjJjJXxbk04HEig/97YyOwoimWYIOC5vlQ60Z+xLhnAvGd6mM0gFY2wQ== - /@rollup/plugin-json/4.0.0_rollup@1.27.11: + /@rollup/plugin-json/4.0.0_rollup@1.27.8: dependencies: - rollup: 1.27.11 + rollup: 1.27.8 rollup-pluginutils: 2.8.2 dev: true peerDependencies: rollup: ^1.20.0 resolution: integrity: sha512-Z65CtEVWv40+ri4CvmswyhtuUtki9yP5p0UJN/GyCKKyU4jRuDS9CG0ZuV7/XuS7zGkoajyE7E4XBEaC4GW62A== - /@rollup/plugin-json/4.0.0_rollup@1.27.8: + /@rollup/plugin-json/4.0.0_rollup@1.28.0: dependencies: - rollup: 1.27.8 + rollup: 1.28.0 rollup-pluginutils: 2.8.2 dev: true peerDependencies: rollup: ^1.20.0 resolution: integrity: sha512-Z65CtEVWv40+ri4CvmswyhtuUtki9yP5p0UJN/GyCKKyU4jRuDS9CG0ZuV7/XuS7zGkoajyE7E4XBEaC4GW62A== - /@rollup/plugin-node-resolve/6.0.0_rollup@1.27.11: + /@rollup/plugin-node-resolve/6.0.0_rollup@1.28.0: dependencies: - '@rollup/pluginutils': 3.0.0_rollup@1.27.11 + '@rollup/pluginutils': 3.0.0_rollup@1.28.0 '@types/resolve': 0.0.8 builtin-modules: 3.1.0 is-module: 1.0.0 resolve: 1.13.1 - rollup: 1.27.11 + rollup: 1.28.0 dev: true engines: node: '>= 8.0.0' @@ -1837,17 +1837,29 @@ packages: rollup: ^1.20.0 resolution: integrity: sha512-GqWz1CfXOsqpeVMcoM315+O7zMxpRsmhWyhJoxLFHVSp9S64/u02i7len/FnbTNbmgYs+sZyilasijH8UiuboQ== - /@rollup/pluginutils/3.0.0_rollup@1.27.11: + /@rollup/pluginutils/3.0.0_rollup@1.28.0: dependencies: estree-walker: 0.6.1 - rollup: 1.27.11 + rollup: 1.28.0 + dev: true engines: node: '>= 8.0.0' peerDependencies: rollup: ^1.20.0 resolution: integrity: sha512-qBbGQQaUUiId/lBU9VMeYlVLOoRNvz1fV8HWY5tiGDpI2gdPZHbmOfCjzSdXPhdq3XOfyWvXEBlIPbnM3+9ogQ== - /@rollup/pluginutils/3.0.0_rollup@1.27.8: + /@rollup/pluginutils/3.0.1_rollup@1.27.13: + dependencies: + estree-walker: 0.6.1 + rollup: 1.27.13 + dev: false + engines: + node: '>= 8.0.0' + peerDependencies: + rollup: ^1.20.0 + resolution: + integrity: sha512-PmNurkecagFimv7ZdKCVOfQuqKDPkrcpLFxRBcQ00LYr4HAjJwhCFxBiY2Xoletll2htTIiXBg6g0Yg21h2M3w== + /@rollup/pluginutils/3.0.1_rollup@1.27.8: dependencies: estree-walker: 0.6.1 rollup: 1.27.8 @@ -1857,11 +1869,11 @@ packages: peerDependencies: rollup: ^1.20.0 resolution: - integrity: sha512-qBbGQQaUUiId/lBU9VMeYlVLOoRNvz1fV8HWY5tiGDpI2gdPZHbmOfCjzSdXPhdq3XOfyWvXEBlIPbnM3+9ogQ== - /@rollup/pluginutils/3.0.1_rollup@1.27.13: + integrity: sha512-PmNurkecagFimv7ZdKCVOfQuqKDPkrcpLFxRBcQ00LYr4HAjJwhCFxBiY2Xoletll2htTIiXBg6g0Yg21h2M3w== + /@rollup/pluginutils/3.0.1_rollup@1.28.0: dependencies: estree-walker: 0.6.1 - rollup: 1.27.13 + rollup: 1.28.0 dev: false engines: node: '>= 8.0.0' @@ -1996,9 +2008,6 @@ packages: /@types/node/12.12.14: resolution: integrity: sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA== - /@types/node/12.12.17: - resolution: - integrity: sha512-Is+l3mcHvs47sKy+afn2O1rV4ldZFU7W8101cNlOd+MRbjM4Onida8jSZnJdTe/0Pcf25g9BNIUsuugmE6puHA== /@types/node/12.12.20: dev: true resolution: @@ -2007,7 +2016,6 @@ packages: resolution: integrity: sha512-8sRGhbpU+ck1n0PGAUgVrWrWdjSW2aqNeyC15W88GRsMpSwzv6RJGlLhE7s2RhVSOdyDmxbqlWSeThq4/7xqlA== /@types/node/12.12.22: - dev: true resolution: integrity: sha512-r5i93jqbPWGXYXxianGATOxTelkp6ih/U0WVnvaqAvTqM+0U6J3kw6Xk6uq/dWNRkEVw/0SLcO5ORXbVNz4FMQ== /@types/normalize-package-data/2.4.0: @@ -7590,11 +7598,11 @@ packages: rollup: '>=0.60.0 <2' resolution: integrity: sha512-tKzWOCmIJD/6aKNz0H1GMM+lW1q9KyFubbWzGiOG540zxPPifnEAHTZwjo0g991Y+DyOZcLqBgqOdqazYE5fkw== - /rollup-plugin-babel/4.3.3_@babel+core@7.7.5+rollup@1.27.11: + /rollup-plugin-babel/4.3.3_@babel+core@7.7.5+rollup@1.27.13: dependencies: '@babel/core': 7.7.5 '@babel/helper-module-imports': 7.7.4 - rollup: 1.27.11 + rollup: 1.27.13 rollup-pluginutils: 2.8.2 dev: true peerDependencies: @@ -7602,11 +7610,11 @@ packages: rollup: '>=0.60.0 <2' resolution: integrity: sha512-tKzWOCmIJD/6aKNz0H1GMM+lW1q9KyFubbWzGiOG540zxPPifnEAHTZwjo0g991Y+DyOZcLqBgqOdqazYE5fkw== - /rollup-plugin-babel/4.3.3_@babel+core@7.7.5+rollup@1.27.13: + /rollup-plugin-babel/4.3.3_@babel+core@7.7.5+rollup@1.28.0: dependencies: '@babel/core': 7.7.5 '@babel/helper-module-imports': 7.7.4 - rollup: 1.27.13 + rollup: 1.28.0 rollup-pluginutils: 2.8.2 dev: true peerDependencies: @@ -7734,14 +7742,6 @@ packages: estree-walker: 0.6.1 resolution: integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== - /rollup/1.27.11: - dependencies: - '@types/estree': 0.0.40 - '@types/node': 12.12.17 - acorn: 7.1.0 - hasBin: true - resolution: - integrity: sha512-ENXdvXk8tjtkNTvIvjRzOEu+vv54va7PiDR7VwP8TD+In6J87gKzzFmQMawQixEL2y9rsPEgomUS7ZVkq47Tww== /rollup/1.27.13: dependencies: '@types/estree': 0.0.40 @@ -7758,6 +7758,14 @@ packages: hasBin: true resolution: integrity: sha512-EVoEV5rAWl+5clnGznt1KY8PeVkzVQh/R0d2s3gHEkN7gfoyC4JmvIVuCtPbYE8NM5Ep/g+nAmvKXBjzaqTsHA== + /rollup/1.28.0: + dependencies: + '@types/estree': 0.0.40 + '@types/node': 12.12.22 + acorn: 7.1.0 + hasBin: true + resolution: + integrity: sha512-v2J/DmQi9+Nf6frGqzwZRvbiuTTrqH0yzoUF4Eybf8sONT4UpLZzJYnYzW96Zm9X1+4SJmijfnFBWCzHDAXYnQ== /run-async/2.3.0: dependencies: is-promise: 2.1.0