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

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

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

Comments

@callumlocke
Copy link

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
Copy link
Author

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
Copy link
Contributor

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
allow custom named exports
@Rich-Harris
Copy link
Contributor

Released in 2.1.0

@DanielJoyce
Copy link

namedExports is sometimes ignored or doesn't work.

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

No branches or pull requests

3 participants