Skip to content

Commit

Permalink
Merged in wbinnssmith/live-reexports (pull request #5)
Browse files Browse the repository at this point in the history
Live reexports fix

Approved-by: Maia Teegarden
  • Loading branch information
Will Binns-Smith committed Oct 21, 2020
2 parents f2dc121 + 884d29d commit 4089cf6
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="a.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./library/a.js";
import { a } from "./library/index.js";

output = a;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="b.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./library/a.js";
import { a } from "./library/index.js";

output = a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { b } from "./index.js";
export var a = "aaa";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var b = "bbb";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { a } from "./a.js";
export { b } from "./b.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@parcel/bundler-default": {
"minBundleSize": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as x from "./b.js";

function get(obj, prop) {
return obj[prop];
}

let a = get(x, "v");
get(x, "update")();
let b = get(x, "v");

output = [a, b];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {v, update} from "./c.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export let v = 1;
export function update() {
v++;
}
30 changes: 30 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,36 @@ describe('scope hoisting', function() {
assert.equal(output, 8);
});

it('supports live bindings in namespaces of reexporting assets', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/live-bindings-reexports-namespace/a.js',
),
);

let output = await run(b);
assert.deepEqual(output, [1, 2]);
});

it('supports live bindings across bundles', async function() {
let b = await bundle(
['a.html', 'b.html'].map(f =>
path.join(
__dirname,
'/integration/scope-hoisting/es6/live-bindings-cross-bundle',
f,
),
),
);

let output = await runBundle(
b,
b.getBundles().find(b => b.type === 'html'),
);
assert.strictEqual(output, 'aaa');
});

it('supports dynamic import syntax for code splitting', async function() {
let b = await bundle(
path.join(
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/scope-hoisting/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function $parcel$defineInteropFlag(a) {
Object.defineProperty(a, '__esModule', {value: true});
}

function $parcel$reexport(e, n, v) {
Object.defineProperty(e, n, {get: v, enumerable: true});
}

function $parcel$exportWildcard(dest, source) {
Object.keys(source).forEach(function(key) {
if (key === 'default' || key === '__esModule') {
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/scope-hoisting/src/hoist.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const EXPORT_ASSIGN_TEMPLATE = template.statement<
{|EXPORTS: Identifier, NAME: Identifier, LOCAL: Expression|},
Statement,
>('EXPORTS.NAME = LOCAL;');
const REEXPORT_TEMPLATE = template.statement<
{|EXPORTS: Identifier, NAME: StringLiteral, LOCAL: Expression|},
Statement,
>('$parcel$reexport(EXPORTS, NAME, function(){return LOCAL;});');
const EXPORT_ALL_TEMPLATE = template.statement<
{|OLD_NAME: Identifier, ID: StringLiteral, SOURCE: StringLiteral|},
Statement,
Expand Down Expand Up @@ -647,9 +651,9 @@ const VISITOR: Visitor<MutableAsset> = {

id.loc = specifier.loc;
path.insertAfter(
EXPORT_ASSIGN_TEMPLATE({
REEXPORT_TEMPLATE({
EXPORTS: getExportsIdentifier(asset, path.scope),
NAME: exported,
NAME: t.stringLiteral(exported.name),
LOCAL: id,
}),
);
Expand Down
18 changes: 17 additions & 1 deletion packages/shared/scope-hoisting/src/shake.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ function getUnusedBinding(path, name, exportsMap) {

// Is there any references which aren't simple assignments?
let bailout = binding.referencePaths.some(
path => !isExportAssignment(path, exportsMap) && !isWildcardDest(path),
path =>
!isExportAssignment(path, exportsMap) &&
!isWildcardDest(path) &&
!isReexportDest(path),
);

if (!bailout) {
Expand Down Expand Up @@ -154,6 +157,17 @@ function isWildcardDest(path) {
);
}

// check if the argument appears as $parcel$reexport(path, ...)
function isReexportDest(path) {
let parent: Node = path.parent;

return (
isCallExpression(parent) &&
isIdentifier(parent.callee, {name: '$parcel$reexport'}) &&
parent.arguments[0] === path.node
);
}

function remove(
path: NodePath<Node>,
scope: Scope,
Expand Down Expand Up @@ -200,6 +214,8 @@ function remove(
);
remove(path.parentPath, scope, exportsMap);
}
} else if (isReexportDest(path)) {
remove(path.parentPath, scope, exportsMap);
} else if (!path.removed) {
if (isSequenceExpression(parent) && parent.expressions.length === 1) {
// TODO missing test coverage
Expand Down

0 comments on commit 4089cf6

Please sign in to comment.