rollup / rollup-plugin-commonjs

This module has moved and is now available at @rollup/plugin-commonjs / https://github.com/rollup/plugins/blob/master/packages/commonjs
MIT License
502 stars 126 forks source link

Code generated for external re-exports by rollup isn't recognized by the plugin #403

Closed marijnh closed 5 years ago

marijnh commented 5 years ago

If you run rollup (generating CommonJS) on something like export {Foo} from "foo" while marking "foo" as external, you get code like this:

Object.defineProperty(exports, 'Foo', {
    enumerable: true,
    get: function () {
        return foo.Foo;
    }
});

But when you then try to consume the resulting package via rollup-plugin-comonjs, it'll complain

[!] Error: 'Foo' is not exported by node_modules/bar/dist/foo.js

Is there something I'm doing wrong? Is it unreasonable to expect Rollup's CommonJS output to be recognized by this plugin?

marijnh commented 5 years ago

(Found a workaround with the externalLiveBindings: false option to Rollup, so if this is hard to support, I can live with a wontfix answer.)

TrySound commented 5 years ago

You need explicitly specify named exports of this package. This is the way I use in my app for react package

import react from 'react';

commonjs({
  namedExports: {
    'react': Object.keys(react)
  }
})
bterlson commented 5 years ago

Closing as duplicate of #159, please move any discussion over there!

FWIW rollup recognizing its own CJS in a mirror seems like a worthwhile enhancement, but these dynamic patterns are generally difficult to tease out reliably. namedExports is going to be the right solution (and @TrySound's dynamic approach seems really cool, I may adopt it 👍)