Skip to content

Commit

Permalink
Ensure named exports are prioritized over wildcard re-exports (#7016)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Oct 6, 2021
1 parent 4904f20 commit 2ebed00
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 7 deletions.
@@ -0,0 +1,2 @@
export const foo = 2;
export * from './foo.mjs'
@@ -0,0 +1,2 @@
export * from './foo.mjs'
export const foo = 2;
@@ -0,0 +1,2 @@
import {foo} from './a.mjs';
output = foo;
@@ -0,0 +1,2 @@
import {foo} from './b.mjs';
output = foo;
@@ -0,0 +1 @@
export const foo = 3;
@@ -0,0 +1,2 @@
import * as a from './a.mjs';
output = a;
@@ -0,0 +1,2 @@
import * as b from './b.mjs';
output = b;
24 changes: 24 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -4843,6 +4843,30 @@ describe('javascript', function() {
assert.equal(typeof res.c, 'function');
});

it('should prioritize named exports before re-exports withput scope hoisting (before)', async () => {
let b = await bundle(
path.join(
__dirname,
'integration/scope-hoisting/es6/re-export-priority/entry-a.mjs',
),
);

let res = await run(b, null, {require: false});
assert.equal(res.output, 2);
});

it('should prioritize named exports before re-exports without scope hoisting (after)', async () => {
let b = await bundle(
path.join(
__dirname,
'integration/scope-hoisting/es6/re-export-priority/entry-b.mjs',
),
);

let res = await run(b, null, {require: false});
assert.equal(res.output, 2);
});

it('should exclude default from export all declaration', async function() {
let b = await bundle(
path.join(__dirname, 'integration/js-export-all/index.js'),
Expand Down
48 changes: 48 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -3774,6 +3774,54 @@ describe('scope hoisting', function() {
test: 'foo',
});
});

it('should prioritize named exports before re-exports (before)', async () => {
let b = await bundle(
path.join(
__dirname,
'integration/scope-hoisting/es6/re-export-priority/entry-a.mjs',
),
);

let res = await run(b);
assert.equal(res, 2);
});

it('should prioritize named exports before re-exports (after)', async () => {
let b = await bundle(
path.join(
__dirname,
'integration/scope-hoisting/es6/re-export-priority/entry-b.mjs',
),
);

let res = await run(b);
assert.equal(res, 2);
});

it('should prioritize named exports before re-exports in namespace (before)', async () => {
let b = await bundle(
path.join(
__dirname,
'integration/scope-hoisting/es6/re-export-priority/namespace-a.mjs',
),
);

let res = await run(b);
assert.deepEqual(res, {foo: 2});
});

it('should prioritize named exports before re-exports in namespace (after)', async () => {
let b = await bundle(
path.join(
__dirname,
'integration/scope-hoisting/es6/re-export-priority/namespace-b.mjs',
),
);

let res = await run(b);
assert.deepEqual(res, {foo: 2});
});
});

describe('commonjs', function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/packagers/js/src/helpers.js
Expand Up @@ -38,7 +38,7 @@ export const helpers = {
`,
$parcel$exportWildcard: `function $parcel$exportWildcard(dest, source) {
Object.keys(source).forEach(function(key) {
if (key === 'default' || key === '__esModule') {
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
return;
}
Expand Down
7 changes: 1 addition & 6 deletions packages/transformers/js/src/esmodule-helpers.js
Expand Up @@ -8,12 +8,7 @@ exports.defineInteropFlag = function(a) {

exports.exportAll = function(source, dest) {
Object.keys(source).forEach(function(key) {
if (key === 'default' || key === '__esModule') {
return;
}

// Skip duplicate re-exports when they have the same value.
if (key in dest && dest[key] === source[key]) {
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
return;
}

Expand Down

0 comments on commit 2ebed00

Please sign in to comment.