Skip to content

Commit

Permalink
Merge branch 'v2' into wbinnssmith/css-sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Binns-Smith committed Apr 14, 2020
2 parents 108d84a + ae3ca81 commit 0ee8a3c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 102 deletions.
6 changes: 3 additions & 3 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class BundleGraph {

resolveExternalDependency(
dependency: Dependency,
bundle?: Bundle,
bundle: ?Bundle,
): ?(
| {|type: 'bundle_group', value: BundleGroup|}
| {|type: 'asset', value: Asset|}
Expand Down Expand Up @@ -270,7 +270,7 @@ export default class BundleGraph {
});
}

getDependencyResolution(dep: Dependency, bundle?: Bundle): ?Asset {
getDependencyResolution(dep: Dependency, bundle: ?Bundle): ?Asset {
let depNode = this._graph.getNode(dep.id);
if (!depNode) {
return null;
Expand Down Expand Up @@ -539,7 +539,7 @@ export default class BundleGraph {

traverseBundles<TContext>(
visit: GraphVisitor<Bundle, TContext>,
startBundle?: Bundle,
startBundle: ?Bundle,
): ?TContext {
return this._graph.filteredTraverse(
node => (node.type === 'bundle' ? node.value : null),
Expand Down
22 changes: 14 additions & 8 deletions packages/core/core/src/public/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
BundleGraph as IBundleGraph,
BundleGroup,
Dependency as IDependency,
GraphTraversalCallback,
GraphVisitor,
Symbol,
SymbolResolution,
} from '@parcel/types';
Expand All @@ -17,7 +17,7 @@ import invariant from 'assert';
import nullthrows from 'nullthrows';
import {DefaultWeakMap} from '@parcel/utils';

import {assetFromValue, assetToAssetValue, Asset} from './Asset';
import {assetFromValue, assetToAssetValue} from './Asset';
import {Bundle, bundleToInternalBundle} from './Bundle';
import Dependency, {dependencyToInternalDependency} from './Dependency';
import {mapVisitor} from '../Graph';
Expand Down Expand Up @@ -54,10 +54,10 @@ export default class BundleGraph implements IBundleGraph {
internalBundleGraphToBundleGraph.get(options).set(graph, this);
}

getDependencyResolution(dep: IDependency, bundle: IBundle): ?Asset {
getDependencyResolution(dep: IDependency, bundle: ?IBundle): ?IAsset {
let resolution = this.#graph.getDependencyResolution(
dependencyToInternalDependency(dep),
bundleToInternalBundle(bundle),
bundle && bundleToInternalBundle(bundle),
);
if (resolution) {
return assetFromValue(resolution, this.#options);
Expand All @@ -84,14 +84,14 @@ export default class BundleGraph implements IBundleGraph {

resolveExternalDependency(
dependency: IDependency,
bundle: IBundle,
bundle: ?IBundle,
): ?(
| {|type: 'bundle_group', value: BundleGroup|}
| {|type: 'asset', value: IAsset|}
) {
let resolved = this.#graph.resolveExternalDependency(
dependencyToInternalDependency(dependency),
bundleToInternalBundle(bundle),
bundle && bundleToInternalBundle(bundle),
);

if (resolved == null) {
Expand Down Expand Up @@ -190,8 +190,8 @@ export default class BundleGraph implements IBundleGraph {
}

traverseBundles<TContext>(
visit: GraphTraversalCallback<IBundle, TContext>,
startBundle?: IBundle,
visit: GraphVisitor<IBundle, TContext>,
startBundle: ?IBundle,
): ?TContext {
return this.#graph.traverseBundles(
mapVisitor(
Expand All @@ -207,4 +207,10 @@ export default class BundleGraph implements IBundleGraph {
.findBundlesWithAsset(assetToAssetValue(asset))
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}

findBundlesWithDependency(dependency: IDependency): Array<IBundle> {
return this.#graph
.findBundlesWithDependency(dependencyToInternalDependency(dependency))
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}
}
66 changes: 4 additions & 62 deletions packages/core/core/src/public/MutableBundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {ParcelOptions} from '../types';
import invariant from 'assert';
import nullthrows from 'nullthrows';
import {DefaultWeakMap, md5FromString} from '@parcel/utils';
import BundleGraph from './BundleGraph';
import InternalBundleGraph from '../BundleGraph';
import {Bundle, bundleToInternalBundle} from './Bundle';
import {mapVisitor, ALL_EDGE_TYPES} from '../Graph';
Expand All @@ -31,11 +32,13 @@ const internalMutableBundleGraphToMutableBundleGraph: DefaultWeakMap<
WeakMap<InternalBundleGraph, MutableBundleGraph>,
> = new DefaultWeakMap(() => new WeakMap());

export default class MutableBundleGraph implements IMutableBundleGraph {
export default class MutableBundleGraph extends BundleGraph
implements IMutableBundleGraph {
#graph; // InternalBundleGraph
#options; // ParcelOptions

constructor(graph: InternalBundleGraph, options: ParcelOptions) {
super(graph, options);
let existing = internalMutableBundleGraphToMutableBundleGraph
.get(options)
.get(graph);
Expand Down Expand Up @@ -121,30 +124,6 @@ export default class MutableBundleGraph implements IMutableBundleGraph {
this.#graph._graph.removeById(getBundleGroupId(bundleGroup));
}

resolveExternalDependency(
dependency: IDependency,
bundle?: IBundle,
): ?(
| {|type: 'bundle_group', value: BundleGroup|}
| {|type: 'asset', value: IAsset|}
) {
let resolved = this.#graph.resolveExternalDependency(
dependencyToInternalDependency(dependency),
bundle && bundleToInternalBundle(bundle),
);

if (resolved == null) {
return;
} else if (resolved.type === 'bundle_group') {
return resolved;
}

return {
type: 'asset',
value: assetFromValue(resolved.value, this.#options),
};
}

internalizeAsyncDependency(bundle: IBundle, dependency: IDependency): void {
this.#graph.internalizeAsyncDependency(
bundleToInternalBundle(bundle),
Expand Down Expand Up @@ -219,22 +198,6 @@ export default class MutableBundleGraph implements IMutableBundleGraph {
.map(asset => assetFromValue(asset, this.#options));
}

getDependencyResolution(dependency: IDependency): ?IAsset {
let resolved = this.#graph.getDependencyResolution(
dependencyToInternalDependency(dependency),
);

if (resolved) {
return assetFromValue(resolved, this.#options);
}
}

getSiblingBundles(bundle: IBundle): Array<IBundle> {
return this.#graph
.getSiblingBundles(bundleToInternalBundle(bundle))
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}

traverse<TContext>(
visit: GraphVisitor<BundlerBundleGraphTraversable, TContext>,
): ?TContext {
Expand All @@ -256,18 +219,6 @@ export default class MutableBundleGraph implements IMutableBundleGraph {
);
}

findBundlesWithAsset(asset: IAsset): Array<IBundle> {
return this.#graph
.findBundlesWithAsset(assetToAssetValue(asset))
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}

findBundlesWithDependency(dependency: IDependency): Array<IBundle> {
return this.#graph
.findBundlesWithDependency(dependencyToInternalDependency(dependency))
.map(bundle => new Bundle(bundle, this.#graph, this.#options));
}

getBundleGroupsContainingBundle(bundle: IBundle): Array<BundleGroup> {
return this.#graph.getBundleGroupsContainingBundle(
bundleToInternalBundle(bundle),
Expand Down Expand Up @@ -304,15 +255,6 @@ export default class MutableBundleGraph implements IMutableBundleGraph {
);
}

traverseBundles<TContext>(visit: GraphVisitor<IBundle, TContext>): ?TContext {
return this.#graph.traverseBundles(
mapVisitor(
bundle => new Bundle(bundle, this.#graph, this.#options),
visit,
),
);
}

traverseContents<TContext>(
visit: GraphVisitor<BundlerBundleGraphTraversable, TContext>,
): ?TContext {
Expand Down
28 changes: 7 additions & 21 deletions packages/core/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,36 +613,21 @@ export type BundleGroup = {|
entryAssetId: string,
|};

export interface MutableBundleGraph {
export interface MutableBundleGraph extends BundleGraph {
addAssetGraphToBundle(Asset, Bundle): void;
addBundleToBundleGroup(Bundle, BundleGroup): void;
createAssetReference(Dependency, Asset): void;
createBundle(CreateBundleOpts): Bundle;
createBundleGroup(Dependency, Target): BundleGroup;
findBundlesWithAsset(Asset): Array<Bundle>;
findBundlesWithDependency(Dependency): Array<Bundle>;
getDependencyAssets(Dependency): Array<Asset>;
getDependencyResolution(Dependency): ?Asset;
getParentBundlesOfBundleGroup(BundleGroup): Array<Bundle>;
getBundleGroupsContainingBundle(Bundle): Array<BundleGroup>;
getBundlesInBundleGroup(BundleGroup): Array<Bundle>;
getSiblingBundles(bundle: Bundle): Array<Bundle>;
getTotalSize(Asset): number;
isAssetInAncestorBundles(Bundle, Asset): boolean;
removeAssetGraphFromBundle(Asset, Bundle): void;
removeBundleGroup(bundleGroup: BundleGroup): void;
resolveExternalDependency(
dependency: Dependency,
bundle?: Bundle,
): ?(
| {|type: 'bundle_group', value: BundleGroup|}
| {|type: 'asset', value: Asset|}
);
internalizeAsyncDependency(bundle: Bundle, dependency: Dependency): void;
traverse<TContext>(
GraphVisitor<BundlerBundleGraphTraversable, TContext>,
): ?TContext;
traverseBundles<TContext>(GraphVisitor<Bundle, TContext>): ?TContext;
traverseContents<TContext>(
GraphVisitor<BundlerBundleGraphTraversable, TContext>,
): ?TContext;
Expand All @@ -659,12 +644,14 @@ export interface BundleGraph {
getIncomingDependencies(asset: Asset): Array<Dependency>;
resolveExternalDependency(
dependency: Dependency,
bundle: Bundle,
bundle: ?Bundle,
): ?(
| {|type: 'bundle_group', value: BundleGroup|}
| {|type: 'asset', value: Asset|}
);
getDependencyResolution(dependency: Dependency, bundle: Bundle): ?Asset;
getDependencyResolution(dependency: Dependency, bundle: ?Bundle): ?Asset;
findBundlesWithAsset(Asset): Array<Bundle>;
findBundlesWithDependency(Dependency): Array<Bundle>;
isAssetInAncestorBundles(bundle: Bundle, asset: Asset): boolean;
isAssetReferenced(asset: Asset): boolean;
isAssetReferencedByDependant(bundle: Bundle, asset: Asset): boolean;
Expand All @@ -676,10 +663,9 @@ export interface BundleGraph {
): SymbolResolution;
getExportedSymbols(asset: Asset): Array<SymbolResolution>;
traverseBundles<TContext>(
visit: GraphTraversalCallback<Bundle, TContext>,
startBundle?: Bundle,
visit: GraphVisitor<Bundle, TContext>,
startBundle: ?Bundle,
): ?TContext;
findBundlesWithAsset(Asset): Array<Bundle>;
}

export type BundleResult = {|
Expand Down
4 changes: 3 additions & 1 deletion packages/packagers/js/src/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ export default new Packager({

// If scope hoisting is enabled, we use a different code path.
if (bundle.env.scopeHoist) {
let wrappedAssets = new Set<string>();
let {ast, referencedAssets} = link({
bundle,
bundleGraph,
ast: await concat(bundle, bundleGraph),
ast: await concat(bundle, bundleGraph, wrappedAssets),
options,
wrappedAssets,
});

let {contents, map} = generate({
Expand Down
18 changes: 13 additions & 5 deletions packages/shared/scope-hoisting/src/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ type TraversalContext = {|
children: AssetASTMap,
|};

export async function concat(bundle: Bundle, bundleGraph: BundleGraph) {
export async function concat(
bundle: Bundle,
bundleGraph: BundleGraph,
wrappedAssets: Set<string>,
) {
let queue = new PromiseQueue({maxConcurrent: 32});
bundle.traverse((node, shouldWrap) => {
switch (node.type) {
Expand All @@ -62,13 +66,13 @@ export async function concat(bundle: Bundle, bundleGraph: BundleGraph) {
bundle,
);
if (resolved) {
resolved.meta.shouldWrap = true;
wrappedAssets.add(resolved.id);
}
return true;
}
break;
case 'asset':
queue.add(() => processAsset(bundle, node.value));
queue.add(() => processAsset(bundle, node.value, wrappedAssets));
}
});

Expand Down Expand Up @@ -140,7 +144,11 @@ export async function concat(bundle: Bundle, bundleGraph: BundleGraph) {
return t.file(t.program(result));
}

async function processAsset(bundle: Bundle, asset: Asset) {
async function processAsset(
bundle: Bundle,
asset: Asset,
wrappedAssets: Set<string>,
) {
let statements: Array<Statement>;
if (asset.astGenerator && asset.astGenerator.type === 'babel') {
let ast = await asset.getAST();
Expand All @@ -150,7 +158,7 @@ async function processAsset(bundle: Bundle, asset: Asset) {
statements = parse(code, asset.filePath);
}

if (asset.meta.shouldWrap) {
if (wrappedAssets.has(asset.id)) {
statements = wrapModule(asset, statements);
}

Expand Down
1 change: 1 addition & 0 deletions packages/shared/scope-hoisting/src/formats/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function generateExports(
let entry = bundle.getMainEntry();
if (
entry &&
!referencedAssets.has(entry) &&
(!isEntry(bundle, bundleGraph) || isReferenced(bundle, bundleGraph))
) {
let exportsId = assertString(entry.meta.exportsIdentifier);
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/scope-hoisting/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ export function link({
bundleGraph,
ast,
options,
wrappedAssets,
}: {|
bundle: Bundle,
bundleGraph: BundleGraph,
ast: File,
options: PluginOptions,
wrappedAssets: Set<string>,
|}): {|ast: File, referencedAssets: Set<Asset>|} {
let format = OutputFormats[bundle.env.outputFormat];
let replacements: Map<Symbol, Symbol> = new Map();
Expand Down Expand Up @@ -448,7 +450,7 @@ export function link({
// We need to wrap the module in a function when a require
// call happens inside a non top-level scope, e.g. in a
// function, if statement, or conditional expression.
if (mod.meta.shouldWrap) {
if (wrappedAssets.has(mod.id)) {
node = t.callExpression(getIdentifier(mod, 'init'), []);
}
// Replace with nothing if the require call's result is not used.
Expand Down Expand Up @@ -660,7 +662,7 @@ export function link({
let decls = path.pushContainer(
'body',
([...referencedAssets]: Array<Asset>)
.filter(a => !a.meta.shouldWrap)
.filter(a => !wrappedAssets.has(a.id))
.map(a => {
return FAKE_INIT_TEMPLATE({
INIT: getIdentifier(a, 'init'),
Expand Down

0 comments on commit 0ee8a3c

Please sign in to comment.