Skip to content

Commit

Permalink
Fix renamed external imports (parcel-bundler#3797)
Browse files Browse the repository at this point in the history
* Add test

* Fix renaming of external imports
  • Loading branch information
mischnic authored Nov 17, 2019
1 parent 051a8ff commit 1fb6c19
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"jsdom": "^15.2.1",
"json5": "^1.0.1",
"kotlin": "^1.3.11",
"lodash": "^4.17.15",
"marked": "^0.6.1",
"ncp": "^2.0.0",
"nyc": "^11.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { assign as a } from "lodash/fp";
import { assign as b } from "lodash";

export const bar = a !== b;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { assign as a } from "lodash/fp";
import { assign as b } from "lodash";

export const bar = a !== b;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { assign as a } from "lodash/fp";
import { assign as b } from "lodash";

export const bar = a !== b;
46 changes: 46 additions & 0 deletions packages/core/integration-tests/test/output-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ describe('output formats', function() {
assert.equal((await run(b)).bar, 3);
});

it('should support commonjs output with external modules (named import with same name)', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/formats/commonjs-external/named-same.js'
)
);

let dist = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(dist.includes('exports.bar'));
assert(/var {\s*assign\s*} = require\("lodash\/fp"\)/.test(dist));
let match = dist.match(
/var {\s*assign:\s*(.*)\s*} = require\("lodash"\)/
);
assert(match);
assert.notEqual(match[1], 'assign');
assert.equal((await run(b)).bar, true);
});

it('should support commonjs output with external modules (namespace import)', async function() {
let b = await bundle(
path.join(
Expand Down Expand Up @@ -133,6 +152,21 @@ describe('output formats', function() {
assert.equal((await run(b)).bar, 3);
});

it('should support commonjs output with old node without destructuring (multiple single with same name)', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/formats/commonjs-destructuring-node/single-same.js'
)
);

let dist = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(dist.includes('exports.bar'));
assert(dist.includes('var assign = require("lodash/fp").assign;'));
assert(dist.includes('var _assign = require("lodash").assign;'));
assert.equal((await run(b)).bar, true);
});

it('should support commonjs output with old node without destructuring (multiple)', async function() {
let b = await bundle(
path.join(
Expand Down Expand Up @@ -320,6 +354,18 @@ describe('output formats', function() {
assert(dist.includes('import { add } from "lodash"'));
});

it('should support esmodule output with external modules (named import with same name)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/esm-external/named-same.js')
);

let dist = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(dist.includes('export const bar'));
assert(dist.includes('import { assign } from "lodash/fp"'));
assert(dist.includes('import { assign as _assign } from "lodash"'));
assert(dist.includes('assign !== _assign'));
});

it('should support esmodule output with external modules (namespace import)', async function() {
let b = await bundle(
path.join(__dirname, '/integration/formats/esm-external/namespace.js')
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/scope-hoisting/src/formats/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ function generateDestructuringAssignment(env, specifiers, value, scope) {
for (let specifier of specifiers) {
statements.push(
ASSIGN_TEMPLATE({
SPECIFIERS: specifier.key,
MODULE: t.memberExpression(value, specifier.value)
SPECIFIERS: specifier.value,
MODULE: t.memberExpression(value, specifier.key)
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/scope-hoisting/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export function link({
programScope.hasBinding(imported) ||
programScope.hasReference(imported)
) {
programScope.generateUid(imported);
renamed = programScope.generateUid(imported);
}

programScope.references[renamed] = true;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7992,6 +7992,11 @@ lodash@^4.16.4, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.4, lodash@^4.17.5
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba"
integrity sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw==

lodash@^4.17.15:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==

[email protected], log-symbols@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a"
Expand Down

0 comments on commit 1fb6c19

Please sign in to comment.