Skip to content

Commit

Permalink
Merge branch 'v2' into wbinnssmith/merge-master-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
wbinnssmith committed Apr 23, 2019
2 parents 169a9d9 + 5689a54 commit 4313f71
Show file tree
Hide file tree
Showing 34 changed files with 1,202 additions and 841 deletions.
4 changes: 3 additions & 1 deletion azure-pipelines-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ jobs:
cargo -V
displayName: Install Rust
- script: yarn
# use `--frozen-lockfile` to fail immediately if the committed yarn.lock needs updates
# https://yarnpkg.com/lang/en/docs/cli/install/#toc-yarn-install-frozen-lockfile
- script: yarn --frozen-lockfile
displayName: 'Install dependencies'
- script: yarn test-ci
displayName: 'Run tests'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"flow-bin": "0.97.0",
"lerna": "^3.3.2",
"lint-staged": "^7.2.2",
"mocha": "^6.1.3",
"mocha-junit-reporter": "^1.21.0",
"mocha-multi-reporters": "^1.1.7",
"prettier": "^1.14.2",
"prettier": "1.14.2",
"sinon": "^7.3.1"
},
"engines": {
Expand Down
35 changes: 13 additions & 22 deletions packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import type {Dependency, BundleGroup, Bundle} from '@parcel/types';
import type {BundleGroup, Bundle, MutableBundle} from '@parcel/types';
import {Bundler} from '@parcel/plugin';

const OPTIONS = {
Expand Down Expand Up @@ -27,7 +27,7 @@ export default new Bundler({
assetGraph.traverse(
(node, context: ?BundleContext): ?BundleContext => {
if (node.type === 'dependency') {
let dep: Dependency = node.value;
let dep = node.value;

// Start a new bundle if this is an async dependency, or entry point.
if (dep.isAsync || dep.isEntry) {
Expand Down Expand Up @@ -76,7 +76,7 @@ export default new Bundler({
// If there is an existing bundle of the asset's type, combine with that.
// Otherwise, a new bundle will be created.
if (existingBundle) {
existingBundle.assetGraph.merge(bundle.assetGraph);
existingBundle.merge(bundle);
return {
bundleGroup: context.bundleGroup,
bundle: existingBundle
Expand All @@ -93,33 +93,35 @@ export default new Bundler({

// Step 2: remove assets that are duplicated in a parent bundle
bundleGraph.traverseBundles(bundle => {
let assetGraph = bundle.assetGraph;
assetGraph.traverseAssets(asset => {
bundle.traverseAssets(asset => {
if (bundleGraph.isAssetInAncestorBundle(bundle, asset)) {
assetGraph.removeAsset(asset);
bundle.removeAsset(asset);
}
});
});

// Step 3: Find duplicated assets in different bundle groups, and separate them into their own parallel bundles.
// If multiple assets are always seen together in the same bundles, combine them together.

let candidateBundles = new Map();
let candidateBundles: Map<
string,
{|bundle: MutableBundle, bundles: Array<MutableBundle>, size: number|}
> = new Map();

assetGraph.traverseAssets((asset, context, traversal) => {
// If this asset is duplicated in the minimum number of bundles, it is a candidate to be separated into its own bundle.
let bundles = bundleGraph.findBundlesWithAsset(asset);
if (bundles.length > OPTIONS.minBundles) {
let bundle = assetGraph.createBundle(asset);
let size = bundle.assetGraph.getTotalSize();
let size = bundle.getTotalSize();

let id = bundles.map(b => b.id).join(':');
let candidate = candidateBundles.get(id);
if (!candidate) {
candidateBundles.set(id, {bundles, bundle, size});
} else {
candidate.size += size;
candidate.bundle.assetGraph.merge(bundle.assetGraph);
candidate.bundle.merge(bundle);
}

// Skip children from consideration since we added a parent already.
Expand Down Expand Up @@ -150,9 +152,9 @@ export default new Bundler({
}

// Remove all of the root assets from each of the original bundles
for (let asset of bundle.assetGraph.getEntryAssets()) {
for (let asset of bundle.getEntryAssets()) {
for (let bundle of bundles) {
bundle.assetGraph.removeAsset(asset);
bundle.removeAsset(asset);
}
}

Expand All @@ -161,16 +163,5 @@ export default new Bundler({
bundleGraph.addBundle(bundleGroup, bundle);
}
}

if (process.env.PARCEL_DUMP_GRAPH != null) {
const dumpGraphToGraphViz = require('@parcel/utils/src/dumpGraphToGraphViz')
.default;
dumpGraphToGraphViz(assetGraph, 'BundlerInputAssetGraph');
// $FlowFixMe
dumpGraphToGraphViz(bundleGraph, 'BundleGraph');
bundleGraph.traverseBundles(bundle => {
dumpGraphToGraphViz(bundle.assetGraph, `${bundle.id}`);
});
}
}
});
68 changes: 23 additions & 45 deletions packages/core/core/src/AssetGraph.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// @flow
// @flow strict-local

import type {
Asset,
AssetGraph as IAssetGraph,
AssetGraphNode,
Bundle,
DependencyNode,
FileNode,
NodeId,
RootNode
} from './types';

import type {
Asset,
CacheEntry,
Dependency as IDependency,
DependencyNode,
File,
FilePath,
FileNode,
Graph as IGraph,
GraphTraversalCallback,
NodeId,
Target,
TransformerRequest
} from '@parcel/types';
Expand All @@ -23,19 +24,19 @@ import Graph from './Graph';
import {md5FromString} from '@parcel/utils/src/md5';
import Dependency from './Dependency';

export const nodeFromRootDir = (rootDir: string) => ({
export const nodeFromRootDir = (rootDir: string): RootNode => ({
id: rootDir,
type: 'root',
value: rootDir
});

export const nodeFromDep = (dep: IDependency) => ({
export const nodeFromDep = (dep: IDependency): DependencyNode => ({
id: dep.id,
type: 'dependency',
value: dep
});

export const nodeFromFile = (file: File) => ({
export const nodeFromFile = (file: File): FileNode => ({
id: file.filePath,
type: 'file',
value: file
Expand All @@ -54,18 +55,18 @@ export const nodeFromAsset = (asset: Asset) => ({
});

const getFileNodesFromGraph = (
graph: IGraph<AssetGraphNode>
graph: Graph<AssetGraphNode>
): Array<FileNode> => {
// $FlowFixMe Flow can't refine on filter https://github.com/facebook/flow/issues/1414
return Array.from(graph.nodes.values()).filter(node => node.type === 'file');
};

const getFilesFromGraph = (graph: IGraph<AssetGraphNode>): Array<File> => {
const getFilesFromGraph = (graph: Graph<AssetGraphNode>): Array<File> => {
return getFileNodesFromGraph(graph).map(node => node.value);
};

const getDepNodesFromGraph = (
graph: IGraph<AssetGraphNode>
graph: Graph<AssetGraphNode>
): Array<DependencyNode> => {
// $FlowFixMe Flow can't refine on filter https://github.com/facebook/flow/issues/1414
return Array.from(graph.nodes.values()).filter(
Expand Down Expand Up @@ -99,8 +100,7 @@ type AssetGraphOpts = {|
* * A dependency node should have an edge to exactly one file node
* * A file node can have one to many edges to asset nodes which can have zero to many edges dependency nodes
*/
export default class AssetGraph extends Graph<AssetGraphNode>
implements IAssetGraph {
export default class AssetGraph extends Graph<AssetGraphNode> {
incompleteNodes: Map<NodeId, AssetGraphNode> = new Map();
invalidNodes: Map<NodeId, AssetGraphNode> = new Map();

Expand Down Expand Up @@ -194,7 +194,11 @@ export default class AssetGraph extends Graph<AssetGraphNode>
}

// Add a file node for the file that the transformer request resolved to
fileNodes.push(nodeFromFile({filePath: req.filePath}));
fileNodes.push(
nodeFromFile({
filePath: req.filePath
})
);

let assetNodes = cacheEntry.assets.map(asset => nodeFromAsset(asset));
let {added, removed} = this.replaceNodesConnectedTo(requestNode, [
Expand Down Expand Up @@ -281,33 +285,7 @@ export default class AssetGraph extends Graph<AssetGraphNode>
}, startNode);
}

createBundle(asset: Asset): Bundle {
let assetNode = this.getNode(asset.id);
if (!assetNode) {
throw new Error('Cannot get bundle for non-existant asset');
}

let graph = this.getSubGraph(assetNode);
graph.setRootNode({
type: 'root',
id: 'root',
value: null
});

graph.addEdge({from: 'root', to: assetNode.id});
return {
id: 'bundle:' + asset.id,
type: asset.type,
assetGraph: graph,
env: asset.env,
stats: {
size: 0,
time: 0
}
};
}

getTotalSize(asset?: Asset): number {
getTotalSize(asset?: ?Asset): number {
let size = 0;
let assetNode = asset ? this.getNode(asset.id) : null;
this.traverseAssets(asset => {
Expand All @@ -327,7 +305,7 @@ export default class AssetGraph extends Graph<AssetGraphNode>
return entries;
}

removeAsset(asset: Asset) {
removeAsset(asset: Asset): void {
let assetNode = this.getNode(asset.id);
if (!assetNode) {
return;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/AssetGraphBuilder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// @flow

import type {Node} from './types';

import type {
ParcelOptions,
Dependency,
FilePath,
Node,
Target,
TransformerRequest
} from '@parcel/types';
Expand Down
Loading

0 comments on commit 4313f71

Please sign in to comment.