Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exports whose names aren't explicit in the file being transformed #18

Closed
callumlocke opened this issue Dec 3, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@callumlocke
Copy link

commented Dec 3, 2015

Input:

import {promisify} from 'bluebird';
const foo = promisify(bar);

Using the npm and commonjs plugins, I get the error:

Module ~/code/foo/node_modules/bluebird/js/release/bluebird.js does not export promisify (imported by ~/code/foo/foo.js)

Here's the contents of that bluebird.js file:

"use strict";
var old;
if (typeof Promise !== "undefined") old = Promise;
function noConflict() {
    try { if (Promise === bluebird) Promise = old; }
    catch (e) {}
    return bluebird;
}
var bluebird = require("./promise")();
bluebird.noConflict = noConflict;
module.exports = bluebird;

...and here it is after commonjs transformed it:

import require$$0 from './promise';

var bluebird = (function (module) {
var exports = module.exports;
"use strict";
var old;
if (typeof Promise !== "undefined") old = Promise;
function noConflict() {
    try { if (Promise === bluebird) Promise = old; }
    catch (e) {}
    return bluebird;
}
var bluebird = require$$0();
bluebird.noConflict = noConflict;
module.exports = bluebird;
return module.exports;
})({exports:{}});

export default bluebird;

I guess the problem is the file doesn't actually mention the name promisify so there's no way for this plugin to know that the module actually does (in commonjs terms) export the name promisify.

A workaround is to change the importing file like this:

import _bluebird from 'bluebird';
const {promisify} = _bluebird;
const foo = promisify(bar);

But it would be nicer if there was some way rollup could handle this scenario. For example, could rollup try to recover from the "Module X does not export [name]" error like this: check if the module has a default export, and if so, include that whole default in the bundle and then add some code that just grabs the relevant property off it (var promisify = _ref.promsify;)? Behind a config option maybe?

@callumlocke

This comment has been minimized.

Copy link
Author

commented Dec 3, 2015

Actually, this might be a better solution (as it might also be a good workaround for #9): Add a new config option for this plugin, allowing me to manually specify, per file, any extra names that the file should export.

  {
    npm({jsnext: true, main: true}),

    commonjs({
      include: ['node_modules/**'],

      addExports: {
        'node_modules/bluebird/js/release/bluebird.js': ['promisify', 'coroutine']
      }
    })
  }

The addExports option would instruct the plugin to add some extra export lines at the end of the transformed code, e.g. export const promisify = _ref.promisify; (where _ref is whatever the default export is).

Would that work?

@Rich-Harris Rich-Harris referenced this issue Dec 27, 2015

Merged

allow custom named exports #24

1 of 1 task complete
@Rich-Harris

This comment has been minimized.

Copy link
Contributor

commented Dec 27, 2015

Sorry for delay. This is a brilliant idea! And obvious when you mention it. Have implemented it in #18, with just a minor tweak (addExports -> namedExports)

Rich-Harris added a commit that referenced this issue Dec 28, 2015

Merge pull request #24 from rollup/gh-18
allow custom named exports
@Rich-Harris

This comment has been minimized.

Copy link
Contributor

commented Dec 28, 2015

Released in 2.1.0

@DanielJoyce

This comment has been minimized.

Copy link

commented Jan 28, 2017

namedExports is sometimes ignored or doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.