Skip to content

Commit

Permalink
Implemented kill-switch for mixed commonjs in mixed es-cjs (Closes ro…
Browse files Browse the repository at this point in the history
…llup#348, rollup#342)

`transformMixedEsModules = false`
  • Loading branch information
danielgindi committed May 1, 2020
1 parent 0ef0adb commit 3bd8d2d
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`

In case of modules that contain commonjs code you can enable/disable the transformer. Set it to `true` if you know that `require` calls should be transformed, or `false` if you know that the code contains env detection and the `require` should survive the transform.

### `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;
/**
* In case of modules that contain commonjs code you can enable/disable the transformer. Set it to `true` if you know that `require` calls should be transformed, or `false` if you know that the code contains env detection and the `require` should survive the transform.
* @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 3bd8d2d

Please sign in to comment.