Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

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

Closed
bterlson opened this issue Jun 11, 2019 · 0 comments · Fixed by #223
Closed

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

bterlson opened this issue Jun 11, 2019 · 0 comments · Fixed by #223

Comments

@bterlson
Copy link
Contributor

bterlson commented Jun 11, 2019

  • 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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant