@parcel/scope-hoisting
Advanced tools
Comparing version 2.0.0-nightly.258 to 2.0.0-nightly.261
117
lib/link.js
@@ -83,3 +83,3 @@ "use strict"; | ||
if (resolved || dep.isDeferred) { | ||
if (resolved || bundleGraph.isDependencyDeferred(dep)) { | ||
for (let [imported, { | ||
@@ -116,4 +116,15 @@ local, | ||
let identifier = symbol; // If this is a wildcard import, resolve to the exports object. | ||
let identifier = symbol; | ||
if (identifier && imports.get(identifier) === null) { | ||
// a deferred import | ||
return { | ||
asset: asset, | ||
symbol: exportSymbol, | ||
identifier: null, | ||
loc | ||
}; | ||
} // If this is a wildcard import, resolve to the exports object. | ||
if (asset && exportSymbol === '*') { | ||
@@ -133,2 +144,40 @@ identifier = (0, _utils.assertString)(asset.meta.exportsIdentifier); | ||
}; | ||
} | ||
function maybeReplaceIdentifier(path) { | ||
let { | ||
name | ||
} = path.node; | ||
if (typeof name !== 'string') { | ||
return; | ||
} | ||
let replacement = replacements.get(name); | ||
if (replacement) { | ||
path.node.name = replacement; | ||
} | ||
if (imports.has(name)) { | ||
let node; | ||
let imported = imports.get(name); | ||
if (imported == null) { | ||
// import was deferred | ||
node = t.objectExpression([]); | ||
} else { | ||
let [asset, symbol, loc] = imported; | ||
node = replaceImportNode(asset, symbol, path, loc); // If the export does not exist, replace with an empty object. | ||
if (!node) { | ||
node = t.objectExpression([]); | ||
} | ||
} | ||
path.replaceWith(node); | ||
} else if (exportsMap.has(name) && !path.scope.hasBinding(name)) { | ||
// If it's an undefined $id$exports identifier. | ||
path.replaceWith(t.objectExpression([])); | ||
} | ||
} // path is an Identifier like $id$import$foo that directly imports originalName from originalModule | ||
@@ -354,3 +403,3 @@ | ||
})); | ||
} else if (dep.isWeak && dep.isDeferred) { | ||
} else if (dep.isWeak && bundleGraph.isDependencyDeferred(dep)) { | ||
path.remove(); | ||
@@ -496,3 +545,3 @@ } else { | ||
exit(path) { | ||
if (!path.isReferenced()) { | ||
if (path.getData('parcelInserted')) { | ||
return; | ||
@@ -521,50 +570,34 @@ } | ||
identifier | ||
} = resolveSymbol(asset, name); // Check if $id$export$name exists and if so, replace the node by it. | ||
} = resolveSymbol(asset, name); | ||
if (identifier) { | ||
path.replaceWith(t.identifier(identifier)); | ||
if (identifier == null) { | ||
return; | ||
} | ||
} | ||
}, | ||
let { | ||
parent | ||
} = path; // Check if $id$export$name exists and if so, replace the node by it. | ||
ReferencedIdentifier(path) { | ||
let { | ||
name | ||
} = path.node; | ||
if ((0, t.isAssignmentExpression)(parent)) { | ||
if ((0, t.isIdentifier)(parent.right)) { | ||
maybeReplaceIdentifier(path.parentPath.get('right')); // do not modify `$id$exports.foo = $id$export$foo` statements | ||
if (typeof name !== 'string') { | ||
return; | ||
} | ||
if ((0, t.isIdentifier)(parent.right, { | ||
name: identifier | ||
})) { | ||
return; | ||
} | ||
} | ||
let replacement = replacements.get(name); | ||
let [stmt] = path.parentPath.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression('=', t.cloneNode(path.node), t.identifier(identifier)))); | ||
stmt.get('expression.left').setData('parcelInserted', true); | ||
} | ||
if (replacement) { | ||
path.node.name = replacement; | ||
path.replaceWith(t.identifier(identifier)); | ||
} | ||
if (imports.has(name)) { | ||
let node; | ||
let imported = imports.get(name); | ||
}, | ||
if (!imported) { | ||
// import was deferred | ||
node = t.objectExpression([]); | ||
} else { | ||
let [asset, symbol, loc] = imported; | ||
node = replaceImportNode(asset, symbol, path, loc); // If the export does not exist, replace with an empty object. | ||
if (!node) { | ||
node = t.objectExpression([]); | ||
} | ||
} | ||
path.replaceWith(node); | ||
return; | ||
} // If it's an undefined $id$exports identifier. | ||
if (exportsMap.has(name) && !path.scope.hasBinding(name)) { | ||
path.replaceWith(t.objectExpression([])); | ||
} | ||
ReferencedIdentifier(path) { | ||
maybeReplaceIdentifier(path); | ||
}, | ||
@@ -571,0 +604,0 @@ |
{ | ||
"name": "@parcel/scope-hoisting", | ||
"version": "2.0.0-nightly.258+e8d7d944", | ||
"version": "2.0.0-nightly.261+ab50b2f0", | ||
"description": "Blazing fast, zero configuration web application bundler", | ||
@@ -24,9 +24,9 @@ "license": "MIT", | ||
"@babel/types": "^7.3.3", | ||
"@parcel/babylon-walk": "2.0.0-nightly.1880+e8d7d944", | ||
"@parcel/diagnostic": "2.0.0-nightly.258+e8d7d944", | ||
"@parcel/babylon-walk": "2.0.0-nightly.1883+ab50b2f0", | ||
"@parcel/diagnostic": "2.0.0-nightly.261+ab50b2f0", | ||
"@parcel/source-map": "2.0.0-alpha.4.9", | ||
"@parcel/utils": "2.0.0-nightly.258+e8d7d944", | ||
"@parcel/utils": "2.0.0-nightly.261+ab50b2f0", | ||
"nullthrows": "^1.1.1" | ||
}, | ||
"gitHead": "e8d7d94484f887c9f61093fca0c63b499305b933" | ||
"gitHead": "ab50b2f0fdb04244e6365e5d14099cdd79c36e4d" | ||
} |
118
src/link.js
@@ -18,2 +18,3 @@ // @flow | ||
LVal, | ||
MemberExpression, | ||
ObjectProperty, | ||
@@ -32,2 +33,3 @@ Statement, | ||
import { | ||
isAssignmentExpression, | ||
isExpressionStatement, | ||
@@ -91,3 +93,3 @@ isIdentifier, | ||
let replacements: Map<Symbol, Symbol> = new Map(); | ||
let imports: Map<Symbol, ?[Asset, Symbol, ?SourceLocation]> = new Map(); | ||
let imports: Map<Symbol, null | [Asset, Symbol, ?SourceLocation]> = new Map(); | ||
let assets: Map<string, Asset> = new Map(); | ||
@@ -124,3 +126,3 @@ let exportsMap: Map<Symbol, Asset> = new Map(); | ||
// If the dependency was excluded, it will be replaced by the output format at the very end. | ||
if (resolved || dep.isDeferred) { | ||
if (resolved || bundleGraph.isDependencyDeferred(dep)) { | ||
for (let [imported, {local, loc}] of dep.symbols) { | ||
@@ -154,2 +156,12 @@ imports.set(local, resolved ? [resolved, imported, loc] : null); | ||
if (identifier && imports.get(identifier) === null) { | ||
// a deferred import | ||
return { | ||
asset: asset, | ||
symbol: exportSymbol, | ||
identifier: null, | ||
loc, | ||
}; | ||
} | ||
// If this is a wildcard import, resolve to the exports object. | ||
@@ -167,2 +179,35 @@ if (asset && exportSymbol === '*') { | ||
function maybeReplaceIdentifier(path: NodePath<Identifier>) { | ||
let {name} = path.node; | ||
if (typeof name !== 'string') { | ||
return; | ||
} | ||
let replacement = replacements.get(name); | ||
if (replacement) { | ||
path.node.name = replacement; | ||
} | ||
if (imports.has(name)) { | ||
let node; | ||
let imported = imports.get(name); | ||
if (imported == null) { | ||
// import was deferred | ||
node = t.objectExpression([]); | ||
} else { | ||
let [asset, symbol, loc] = imported; | ||
node = replaceImportNode(asset, symbol, path, loc); | ||
// If the export does not exist, replace with an empty object. | ||
if (!node) { | ||
node = t.objectExpression([]); | ||
} | ||
} | ||
path.replaceWith(node); | ||
} else if (exportsMap.has(name) && !path.scope.hasBinding(name)) { | ||
// If it's an undefined $id$exports identifier. | ||
path.replaceWith(t.objectExpression([])); | ||
} | ||
} | ||
// path is an Identifier like $id$import$foo that directly imports originalName from originalModule | ||
@@ -426,3 +471,3 @@ function replaceImportNode(originalModule, originalName, path, depLoc) { | ||
); | ||
} else if (dep.isWeak && dep.isDeferred) { | ||
} else if (dep.isWeak && bundleGraph.isDependencyDeferred(dep)) { | ||
path.remove(); | ||
@@ -568,3 +613,3 @@ } else { | ||
exit(path) { | ||
if (!path.isReferenced()) { | ||
if (path.getData('parcelInserted')) { | ||
return; | ||
@@ -592,43 +637,40 @@ } | ||
// Check if $id$export$name exists and if so, replace the node by it. | ||
if (identifier) { | ||
path.replaceWith(t.identifier(identifier)); | ||
if (identifier == null) { | ||
return; | ||
} | ||
}, | ||
}, | ||
ReferencedIdentifier(path) { | ||
let {name} = path.node; | ||
if (typeof name !== 'string') { | ||
return; | ||
} | ||
let replacement = replacements.get(name); | ||
if (replacement) { | ||
path.node.name = replacement; | ||
} | ||
let {parent} = path; | ||
// Check if $id$export$name exists and if so, replace the node by it. | ||
if (isAssignmentExpression(parent)) { | ||
if (isIdentifier(parent.right)) { | ||
maybeReplaceIdentifier( | ||
path.parentPath.get<NodePath<Identifier>>('right'), | ||
); | ||
if (imports.has(name)) { | ||
let node; | ||
let imported = imports.get(name); | ||
if (!imported) { | ||
// import was deferred | ||
node = t.objectExpression([]); | ||
} else { | ||
let [asset, symbol, loc] = imported; | ||
node = replaceImportNode(asset, symbol, path, loc); | ||
// do not modify `$id$exports.foo = $id$export$foo` statements | ||
if (isIdentifier(parent.right, {name: identifier})) { | ||
return; | ||
} | ||
} | ||
let [stmt] = path.parentPath.parentPath.insertAfter( | ||
t.expressionStatement( | ||
t.assignmentExpression( | ||
'=', | ||
t.cloneNode(path.node), | ||
t.identifier(identifier), | ||
), | ||
), | ||
); | ||
// If the export does not exist, replace with an empty object. | ||
if (!node) { | ||
node = t.objectExpression([]); | ||
} | ||
stmt | ||
.get<NodePath<MemberExpression>>('expression.left') | ||
.setData('parcelInserted', true); | ||
} | ||
path.replaceWith(node); | ||
return; | ||
} | ||
// If it's an undefined $id$exports identifier. | ||
if (exportsMap.has(name) && !path.scope.hasBinding(name)) { | ||
path.replaceWith(t.objectExpression([])); | ||
} | ||
path.replaceWith(t.identifier(identifier)); | ||
}, | ||
}, | ||
ReferencedIdentifier(path) { | ||
maybeReplaceIdentifier(path); | ||
}, | ||
Program: { | ||
@@ -635,0 +677,0 @@ exit(path) { |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
225092
5997