rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.64k stars 590 forks source link

`node-resolve` with `browser` under `mainFields` breaks polyfills with its `empty.js` #440

Closed 5310 closed 4 years ago

5310 commented 4 years ago

How Do We Reproduce?

A reproduction with the breaking config as well as a working variant has been uploaded here: https://github.com/5310/issue_rollup_plugins_node-resolve-w-browser

Expected Behavior

Upon running npx rollup -c ./rollup.config.broken.js Rollup should produce a browser bundle for my test-case CJS module that depends on some polyfilled Node builtins.

Actual Behavior

The build process fails with: [!] Error: 'Buffer' is not exported by node-resolve:empty.js, imported by node_modules/node-forge/lib/baseN.js

See also: The readme in the reproduction repo, and https://www.pika.dev/npm/snowpack/discuss/275

/cc @FredKSchott @quasor

FredKSchott commented 4 years ago

I can reproduce this with the xlsx package as well

duke79 commented 4 years ago

I'm facing the same issue.

snowpack failed to load node_modules/xlsx/dist/cpexcel.js 
'Buffer' is not exported by node-resolve:empty.js, imported by node_modules/xlsx/dist/cpexcel.js
duke79 commented 4 years ago

@shellscape Why is my comment marked off-topic? In case my problem is not a related one, please let me know.

stale[bot] commented 4 years ago

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.

wangchongchong1007 commented 3 years ago

@duke79 Is there a solution to this problem?

Rollup: Missing Export 'Buffer' is not exported by node-resolve:empty.js, imported by ./node_modules/xlsx/dist/cpexcel.js

Avnerus commented 3 years ago

@duke79, @wangchongchong1007 I was able to work around this by adding a plugin hook which adds syntheticNamedExports: true to node-resolve:empty.js. This is the hook:

const addSyntheticNamedExportsToSkippedNodeImports = () => ({
  load: (importee) => {
    if (importee === '\u0000node-resolve:empty.js') {
      return {code: 'export default {};', syntheticNamedExports: true};
    } else {
      return null;
    }
  }
});

It needs to be in the plugin chain before nodeResolve.

   ...
    addSyntheticNamedExportsToSkippedNodeImports(),
    nodeResolve({
          browser: true
    }),
    ...

I will work on making a separate issue about this.