Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packager request #6379

Merged
merged 17 commits into from Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
members = ["packages/transformers/js", "packages/utils/fs-search"]
members = ["packages/transformers/js", "packages/utils/fs-search", "packages/utils/hash"]

[profile.release]
opt-level = 3
Expand Down
1 change: 1 addition & 0 deletions packages/bundlers/default/package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@parcel/diagnostic": "2.0.0-beta.3.1",
"@parcel/hash": "2.0.0-beta.3.1",
"@parcel/plugin": "2.0.0-beta.3.1",
"@parcel/utils": "2.0.0-beta.3.1",
"nullthrows": "^1.1.1"
Expand Down
5 changes: 3 additions & 2 deletions packages/bundlers/default/src/DefaultBundler.js
Expand Up @@ -12,7 +12,8 @@ import type {SchemaEntity} from '@parcel/utils';

import invariant from 'assert';
import {Bundler} from '@parcel/plugin';
import {md5FromString, validateSchema} from '@parcel/utils';
import {validateSchema} from '@parcel/utils';
import {hashString} from '@parcel/hash';
import nullthrows from 'nullthrows';
import {encodeJSONKeyComponent} from '@parcel/diagnostic';

Expand Down Expand Up @@ -397,7 +398,7 @@ export default (new Bundler({

let [firstBundle] = [...sourceBundles];
let sharedBundle = bundleGraph.createBundle({
uniqueKey: md5FromString([...sourceBundles].map(b => b.id).join(':')),
uniqueKey: hashString([...sourceBundles].map(b => b.id).join(':')),
// Allow this bundle to be deduplicated. It shouldn't be further split.
// TODO: Reconsider bundle/asset flags.
isSplittable: true,
Expand Down
1 change: 1 addition & 0 deletions packages/core/core/package.json
Expand Up @@ -27,6 +27,7 @@
"@parcel/diagnostic": "2.0.0-beta.3.1",
"@parcel/events": "2.0.0-beta.3.1",
"@parcel/fs": "2.0.0-beta.3.1",
"@parcel/hash": "2.0.0-beta.3.1",
"@parcel/logger": "2.0.0-beta.3.1",
"@parcel/package-manager": "2.0.0-beta.3.1",
"@parcel/plugin": "2.0.0-beta.3.1",
Expand Down
40 changes: 20 additions & 20 deletions packages/core/core/src/AssetGraph.js
Expand Up @@ -19,12 +19,8 @@ import type {
} from './types';

import invariant from 'assert';
import crypto from 'crypto';
import {
md5FromObject,
md5FromOrderedObject,
objectSortedEntries,
} from '@parcel/utils';
import {hashString, Hash} from '@parcel/hash';
import {hashObject, objectSortedEntries} from '@parcel/utils';
import nullthrows from 'nullthrows';
import ContentGraph, {type SerializedContentGraph} from './ContentGraph';
import {createDependency} from './Dependency';
Expand Down Expand Up @@ -57,15 +53,19 @@ export function nodeFromDep(dep: Dependency): DependencyNode {

export function nodeFromAssetGroup(assetGroup: AssetGroup): AssetGroupNode {
return {
id: md5FromOrderedObject({
filePath: assetGroup.filePath,
env: assetGroup.env.id,
isSource: assetGroup.isSource,
sideEffects: assetGroup.sideEffects,
code: assetGroup.code,
pipeline: assetGroup.pipeline,
query: assetGroup.query ? objectSortedEntries(assetGroup.query) : null,
}),
id: hashString(
assetGroup.filePath +
assetGroup.env.id +
String(assetGroup.isSource) +
String(assetGroup.sideEffects) +
(assetGroup.code ?? '') +
':' +
(assetGroup.pipeline ?? '') +
':' +
(assetGroup.query
? JSON.stringify(objectSortedEntries(assetGroup.query))
: ''),
),
type: 'asset_group',
value: assetGroup,
usedSymbolsDownDirty: true,
Expand Down Expand Up @@ -93,7 +93,7 @@ export function nodeFromEntrySpecifier(entry: string): EntrySpecifierNode {

export function nodeFromEntryFile(entry: Entry): EntryFileNode {
return {
id: 'entry_file:' + md5FromObject(entry),
id: 'entry_file:' + hashObject(entry),
type: 'entry_file',
value: entry,
};
Expand Down Expand Up @@ -569,18 +569,18 @@ export default class AssetGraph extends ContentGraph<AssetGraphNode> {
return this.hash;
}

let hash = crypto.createHash('md5');
let hash = new Hash();
// TODO: sort??
this.traverse(nodeId => {
let node = nullthrows(this.getNode(nodeId));
if (node.type === 'asset') {
hash.update(nullthrows(node.value.outputHash));
hash.writeString(nullthrows(node.value.outputHash));
} else if (node.type === 'dependency' && node.value.target) {
hash.update(JSON.stringify(node.value.target));
hash.writeString(JSON.stringify(node.value.target));
}
});

this.hash = hash.digest('hex');
this.hash = hash.finish();
return this.hash;
}
}
40 changes: 21 additions & 19 deletions packages/core/core/src/BundleGraph.js
Expand Up @@ -22,9 +22,9 @@ import type AssetGraph from './AssetGraph';

import assert from 'assert';
import invariant from 'assert';
import crypto from 'crypto';
import nullthrows from 'nullthrows';
import {objectSortedEntriesDeep} from '@parcel/utils';
import {Hash, hashString} from '@parcel/hash';

import {getBundleGroupId, getPublicId} from './utils';
import {ALL_EDGE_TYPES, mapVisitor} from './Graph';
Expand Down Expand Up @@ -234,10 +234,6 @@ export default class BundleGraph {
return;
}

if (node.type === 'asset' && !this.bundleHasAsset(bundle, node.value)) {
bundle.stats.size += node.value.stats.size;
}

if (node.type === 'asset' || node.type === 'dependency') {
this._graph.addEdge(bundleNodeId, nodeId, 'contains');
}
Expand Down Expand Up @@ -432,10 +428,6 @@ export default class BundleGraph {
// aggregate.
false /* removeOrphans */,
);

if (node.type === 'asset') {
bundle.stats.size -= asset.stats.size;
}
} else {
actions.skipChildren();
}
Expand Down Expand Up @@ -1404,10 +1396,10 @@ export default class BundleGraph {
return existingHash;
}

let hash = crypto.createHash('md5');
let hash = new Hash();
// TODO: sort??
this.traverseAssets(bundle, asset => {
hash.update(
hash.writeString(
[
this.getAssetPublicId(asset),
asset.outputHash,
Expand All @@ -1419,7 +1411,7 @@ export default class BundleGraph {
);
});

let hashHex = hash.digest('hex');
let hashHex = hash.finish();
this._bundleContentHashes.set(bundle.id, hashHex);
return hashHex;
}
Expand Down Expand Up @@ -1457,23 +1449,33 @@ export default class BundleGraph {
}

getHash(bundle: Bundle): string {
let hash = crypto.createHash('md5');
hash.update(bundle.id);
hash.update(this.getContentHash(bundle));
let hash = new Hash();
hash.writeString(
bundle.id + bundle.target.publicUrl + this.getContentHash(bundle),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Target options aren't in the environment hash and aren't tracked in the graph. This is kinda assuming that packagers will use the publicUrl in some way, but currently this is only true of the HTML packager. Perhaps we can make this more granular in the future somehow...

);

let inlineBundles = this.getInlineBundles(bundle);
for (let inlineBundle of inlineBundles) {
hash.update(this.getContentHash(inlineBundle));
hash.writeString(this.getContentHash(inlineBundle));
}

for (let referencedBundle of this.getReferencedBundles(bundle)) {
if (!referencedBundle.isInline) {
hash.update(referencedBundle.id);
hash.writeString(referencedBundle.id);
}
}

hash.update(JSON.stringify(objectSortedEntriesDeep(bundle.env)));
return hash.digest('hex');
hash.writeString(JSON.stringify(objectSortedEntriesDeep(bundle.env)));
return hash.finish();
}

getBundleGraphHash(): string {
let hashes = '';
for (let bundle of this.getBundles()) {
hashes += this.getHash(bundle);
}

return hashString(hashes);
}

addBundleToBundleGroup(bundle: Bundle, bundleGroup: BundleGroup) {
Expand Down
16 changes: 8 additions & 8 deletions packages/core/core/src/Dependency.js
Expand Up @@ -5,8 +5,8 @@ import type {
ModuleSpecifier,
Symbol,
} from '@parcel/types';
import {md5FromOrderedObject} from '@parcel/utils';
import type {Dependency, Environment, Target} from './types';
import {hashString} from '@parcel/hash';

type DependencyOpts = {|
id?: string,
Expand All @@ -33,13 +33,13 @@ type DependencyOpts = {|
export function createDependency(opts: DependencyOpts): Dependency {
let id =
opts.id ||
md5FromOrderedObject({
sourceAssetId: opts.sourceAssetId,
moduleSpecifier: opts.moduleSpecifier,
env: opts.env.id,
target: opts.target,
pipeline: opts.pipeline,
});
hashString(
(opts.sourceAssetId ?? '') +
opts.moduleSpecifier +
opts.env.id +
(opts.target ? JSON.stringify(opts.target) : '') +
(opts.pipeline ?? ''),
);

return {
...opts,
Expand Down
20 changes: 11 additions & 9 deletions packages/core/core/src/Environment.js
@@ -1,7 +1,7 @@
// @flow
import type {EnvironmentOptions} from '@parcel/types';
import type {Environment} from './types';
import {md5FromOrderedObject} from '@parcel/utils';
import {hashString} from '@parcel/hash';

const DEFAULT_ENGINES = {
browsers: ['> 0.25%'],
Expand Down Expand Up @@ -113,12 +113,14 @@ export function mergeEnvironments(
function getEnvironmentHash(env: Environment): string {
// context is excluded from hash so that assets can be shared between e.g. workers and browser.
// Different engines should be sufficient to distinguish multi-target builds.
return md5FromOrderedObject({
engines: env.engines,
includeNodeModules: env.includeNodeModules,
outputFormat: env.outputFormat,
isLibrary: env.isLibrary,
shouldScopeHoist: env.shouldScopeHoist,
sourceMap: env.sourceMap,
});
return hashString(
JSON.stringify([
env.engines,
env.includeNodeModules,
env.outputFormat,
env.isLibrary,
env.shouldScopeHoist,
env.sourceMap,
]),
);
}
4 changes: 2 additions & 2 deletions packages/core/core/src/InternalConfig.js
Expand Up @@ -7,9 +7,9 @@ import type {
ConfigResult,
DevDepOptions,
} from '@parcel/types';
import {md5FromString} from '@parcel/utils';
import type {Config, Environment} from './types';
import {createEnvironment} from './Environment';
import {hashString} from '@parcel/hash';

type ConfigOpts = {|
plugin: PackageName,
Expand Down Expand Up @@ -38,7 +38,7 @@ export function createConfig({
}: ConfigOpts): Config {
let environment = env ?? createEnvironment();
return {
id: md5FromString(plugin + searchPath + environment.id + String(isSource)),
id: hashString(plugin + searchPath + environment.id + String(isSource)),
isSource: isSource ?? false,
searchPath,
env: environment,
Expand Down