Skip to content

Commit

Permalink
Fix ES6 re-export of CommonJS modules (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored and devongovett committed Jun 17, 2018
1 parent f536e8b commit 9e2f9ab
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/scope-hoisting/concat.js
Expand Up @@ -59,7 +59,7 @@ module.exports = (packager, ast) => {
if (wildcards && name !== 'default') {
for (let source of wildcards) {
let m = findExportModule(resolveModule(id, source).id, name);
if (m) {
if (m.identifier) {
return m;
}
}
Expand All @@ -74,28 +74,32 @@ module.exports = (packager, ast) => {
exp = replacements.get(exp);
}

return exp;
return {
identifier: exp,
name,
id
};
}

function replaceExportNode(mod, originalName, path) {
let id = mod.id;
let res = findExportModule(id, originalName);
function replaceExportNode(module, originalName, path) {
let {identifier, name, id} = findExportModule(module.id, originalName);
let mod = assets[id];
let node;

if (res) {
node = findSymbol(path, res);
if (identifier) {
node = findSymbol(path, identifier);
}

// If the module is not in this bundle, create a `require` call for it.
if (!node && !assets[id]) {
if (!node && !mod) {
node = REQUIRE_TEMPLATE({ID: t.numericLiteral(id)}).expression;
return interop(mod, originalName, path, node);
return interop(module, name, path, node);
}

// If this is an ES6 module, throw an error if we cannot resolve the module
if (!node && !mod.cacheData.isCommonJS && mod.cacheData.isES6Module) {
let relativePath = relative(packager.options.rootDir, mod.name);
throw new Error(`${relativePath} does not export '${originalName}'`);
throw new Error(`${relativePath} does not export '${name}'`);
}

// If it is CommonJS, look for an exports object.
Expand All @@ -105,7 +109,7 @@ module.exports = (packager, ast) => {
return null;
}

return interop(mod, originalName, path, node);
return interop(mod, name, path, node);
}

return node;
Expand Down Expand Up @@ -282,9 +286,9 @@ module.exports = (packager, ast) => {
continue;
}

let exp = findExportModule(match[1], key.name, path);
if (exp) {
replace(value.name, exp, p);
let {identifier} = findExportModule(match[1], key.name, path);
if (identifier) {
replace(value.name, identifier, p);
}
}

Expand Down Expand Up @@ -332,11 +336,11 @@ module.exports = (packager, ast) => {
// If it's a $id$exports.name expression.
if (match) {
let name = t.isIdentifier(property) ? property.name : property.value;
let exp = findExportModule(match[1], name, path);
let {identifier} = findExportModule(match[1], name, path);

// Check if $id$export$name exists and if so, replace the node by it.
if (exp) {
path.replaceWith(t.identifier(exp));
if (identifier) {
path.replaceWith(t.identifier(identifier));
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/scope-hoisting/es6/re-export-commonjs/a.js
@@ -0,0 +1,3 @@
import {foo} from './b'

output = foo()
@@ -0,0 +1 @@
export {default as foo} from './c'
6 changes: 6 additions & 0 deletions test/integration/scope-hoisting/es6/re-export-commonjs/c.js
@@ -0,0 +1,6 @@
Object.defineProperty(exports, '__esModule', {
value: true
})
Object.defineProperty(exports, 'default', {
value: () => 'foo'
})
9 changes: 9 additions & 0 deletions test/scope-hoisting.js
Expand Up @@ -292,6 +292,15 @@ describe('scope hoisting', function() {
assert(contents.includes('foo'));
assert(!contents.includes('bar'));
});

it('support exporting a ES6 module exported as CommonJS', async function() {
let b = await bundle(
__dirname + '/integration/scope-hoisting/es6/re-export-commonjs/a.js'
);

let output = await run(b);
assert.deepEqual(output, 'foo');
});
});

describe('commonjs', function() {
Expand Down

0 comments on commit 9e2f9ab

Please sign in to comment.