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

Most upvoted comments

@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 require should resolve to undefined) or for a node env, or whether they actually want to keep the typeof require to keep the code isomorphic to some extend. So manual control over this can’t be avoided, until everyone stops using these hacks of typeof 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