rollup/rollup-plugin-node-resolve

Resolve 1.11.1 breaks importing "shadowed" core modules e.g. buffer

bterlson opened this issue · 0 comments

  • Node-Resolve Plugin Version: 5.0.1
  • Rollup Version: 1.15.1
  • Operating System (or Browser):
  • Node Version: 10.x

How Do We Reproduce?

First install the buffer shim, named "buffer":

npm install buffer

Configure this plugin with preferBuiltins: false:

export default [
  // browser-friendly UMD build
  {
    input: "src/index.js",
    output: {
      name: "test",
      file: pkg.browser["dist/index.cjs.js"],
      format: "umd"
    },
    plugins: [
      resolve({
        mainFields: ["browser", "module", "main"],
        preferBuiltins: false
      }),
      commonjs()
    ]
  }
];

And attempt to create a browser bundle of the following application:

const { Buffer } = require('Buffer');

Expected Behavior

Buffer is bundled.

Actual Behavior

Buffer is not bundled, and warnings are emitted about depending on a built-in module.

I believe this is due to a recent-ish change in resolve in how it handles shadowed modules. Versions of resolve prior to 1.11.1 (including 1.11.0) will happily resolve a module named buffer, but 1.11.1 will simply return buffer back.

If I had to guess at a proper fix, it would be this: when calling resolve, we should first check if the module is a core module1, and if so, and preferBuiltins is false, then a trailing slash should be appended to the module name passed to resolve. If this fails to resolve anything, resolved reverts back to importee.

[1]: I also observe that resolve maintains a list of node builtins, so some duplication could also probably be removed as part of this effort.