Skip to content

Commit

Permalink
feat(commonjs): add kill-switch for mixed es-cjs modules (rollup#358)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Reverts default behavior of mixed es and cjs modules.
Marked as a "breaking change", but the PR that raised this need was actually a breaking change for people who did not utilize include/exclude correctly and have variety of dependencies with mixed UMD mechanisms.

* Implemented kill-switch for mixed commonjs in mixed es-cjs (Closes rollup#348, rollup#342)

`transformMixedEsModules = false`

* Update packages/commonjs/README.md

Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>

* Update index.d.ts

Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>
  • Loading branch information
2 people authored and LarsDenBakker committed Sep 12, 2020
1 parent f6e3825 commit b3acf74
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/commonjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ Default: `true`

If false, skips source map generation for CommonJS modules.

### `transformMixedEsModules`

Type: `Boolean`<br>
Default: `false`

Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation.

### `ignore`

Type: `Array[...String | (String) => Boolean]`<br>
Expand Down
8 changes: 6 additions & 2 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ export default function commonjs(options = {}) {

const isDynamicRequireModule = dynamicRequireModuleSet.has(normalizePathSlashes(id));

if (isEsModule && !isDynamicRequireModule) {
if (isEsModule && (!isDynamicRequireModule || !options.transformMixedEsModules)) {
(hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport).add(id);
if (!options.transformMixedEsModules) {
setIsCjsPromise(id, false);
return null;
}
}
// it is not an ES module but it does not have CJS-specific elements.
// it is not an ES module AND it does not have CJS-specific elements.
else if (!hasCjsKeywords(code, ignoreGlobal)) {
esModulesWithoutDefaultExport.add(id);
setIsCjsPromise(id, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
transformMixedEsModules: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
transformMixedEsModules: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
transformMixedEsModules: true
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
]
},
pluginOptions: {
dynamicRequireTargets: ['fixtures/function/dynamic-require-code-splitting/target?.js']
dynamicRequireTargets: ['fixtures/function/dynamic-require-code-splitting/target?.js'],
transformMixedEsModules: true
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
description: 'works when the entry point is an es module',
pluginOptions: {
dynamicRequireTargets: ['fixtures/function/dynamic-require-es-entry/submodule.js']
dynamicRequireTargets: ['fixtures/function/dynamic-require-es-entry/submodule.js'],
transformMixedEsModules: true
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ module.exports = {
options: {
plugins: [nodeResolve()]
},
pluginOptions: {}
pluginOptions: {
transformMixedEsModules: true
}
};
1 change: 1 addition & 0 deletions packages/commonjs/test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const config: import('rollup').RollupOptions = {
extensions: ['.js', '.coffee'],
ignoreGlobal: false,
sourceMap: false,
transformMixedEsModules: false,
ignore: ['conditional-runtime-dependency'],
dynamicRequireTargets: ['node_modules/logform/*.js']
})
Expand Down
5 changes: 5 additions & 0 deletions packages/commonjs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ interface RollupCommonJSOptions {
* @default true
*/
sourceMap?: boolean;
/**
* Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation.
* @default false
*/
transformMixedEsModules?: boolean;
/**
* explicitly specify unresolvable named exports
* ([see below for more details](https://github.com/rollup/plugins/tree/master/packages/commonjs#named-exports))
Expand Down

0 comments on commit b3acf74

Please sign in to comment.