Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
This repository was 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

@callumlocke

Description

@callumlocke

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions