plugins: Isomorphic code is no longer isomorphic
- Rollup Plugin Name: commonjs
- Rollup Plugin Version: 11.1.0
- Rollup Version: 2.7.3
- Operating System (or Browser): Windows 10
- Node Version: 13.9.0
How Do We Reproduce?
Repro repo at https://github.com/SupernaviX/rollup-plugin-commonjs-broken-isomorphic-require
Bundle a module which checks if require is defined, and requires a module if it is.
Expected Behavior
Since the require statement will never be called in the browser, it should ideally be transpiled out. Since the original code worked in the browser, the new code should work too.
Actual Behavior
The require statement is converted into a static import. Even though the module is never actually used, it becomes a hard dependency and the file no longer works in the browser.
Relevant snippet of code:
// Add isomorphic support for TextDecoder
let TextDecoder = undefined;
if (typeof window === "object") {
TextDecoder = window.TextDecoder;
} else if (typeof self === "object") {
TextDecoder = self.TextDecoder;
} else if (typeof require === "function") {
TextDecoder = require("util").TextDecoder;
}
This might be a duplicate of #342, I’m not sure.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (11 by maintainers)
Commits related to this issue
- Implemented kill-switch for mixed commonjs in mixed es-cjs (Closes #348, #342) `transformMixedEsModules = false` — committed to danielgindi/rollup-official-plugins by danielgindi 4 years ago
- feat(commonjs): add kill-switch for mixed es-cjs modules (#358) BREAKING CHANGE: Reverts default behavior of mixed es and cjs modules. Marked as a "breaking change", but the PR that raised this n... — committed to LarsDenBakker/plugins by danielgindi 4 years ago
@lukastaegert Yes you raise a good point. We probably have no good way of knowing whether the user intend for the module to be transpiled for the browser (hence
typeof requireshould resolve toundefined) or for anodeenv, or whether they actually want to keep thetypeof requireto keep the code isomorphic to some extend. So manual control over this can’t be avoided, until everyone stops using these hacks oftypeof require.I can confirm we can’t longer compile stencil with rollup because the commonjs plugin is trying to wrap ESmodule code around the commonjs wrapper, leading to
Error: 'import' and 'export' may only appear at the top level