Skip to content

Commit

Permalink
Invalidate cache for entry, target, and parcel option changes (#5212)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Nov 22, 2020
1 parent 0247770 commit 5510b03
Show file tree
Hide file tree
Showing 37 changed files with 2,165 additions and 706 deletions.
16 changes: 6 additions & 10 deletions packages/core/core/src/AssetGraphBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import type {
Entry,
ParcelOptions,
ValidationOpts,
Target,
} from './types';
import type {ConfigAndCachePath} from './requests/ParcelConfigRequest';
import type {EntryResult} from './requests/EntryRequest';
import type {TargetResolveResult} from './requests/TargetRequest';

import EventEmitter from 'events';
import invariant from 'assert';
Expand Down Expand Up @@ -105,13 +105,9 @@ export default class AssetGraphBuilder extends EventEmitter {
this.workerFarm = workerFarm;
this.assetRequests = [];

// TODO: changing these should not throw away the entire graph.
// We just need to re-run target resolution.
let {hot, publicUrl, distDir, minify, scopeHoist} = options;
this.cacheKey = md5FromObject({
parcelVersion: PARCEL_VERSION,
name,
options: {hot, publicUrl, distDir, minify, scopeHoist},
entries,
});

Expand All @@ -138,6 +134,7 @@ export default class AssetGraphBuilder extends EventEmitter {
if (changes) {
this.requestGraph.invalidateUnpredictableNodes();
this.requestGraph.invalidateEnvNodes(options.env);
this.requestGraph.invalidateOptionNodes(options);
this.requestTracker.respondToFSEvents(changes);
} else {
this.assetGraph.initialize({
Expand Down Expand Up @@ -757,11 +754,10 @@ export default class AssetGraphBuilder extends EventEmitter {

async runTargetRequest(input: Entry) {
let request = createTargetRequest(input);
let result = await this.requestTracker.runRequest<
Entry,
TargetResolveResult,
>(request);
this.assetGraph.resolveTargets(request.input, result.targets, request.id);
let targets = await this.requestTracker.runRequest<Entry, Array<Target>>(
request,
);
this.assetGraph.resolveTargets(request.input, targets, request.id);
}

async runPathRequest(input: Dependency) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core/src/BundlerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export default class BundlerRunner {
version,
hash: assetGraph.getHash(),
config: configResult?.config,
// TODO: remove once bundling is a request and we track options as invalidations.
hot: this.options.hot,
});
}

Expand Down
3 changes: 3 additions & 0 deletions packages/core/core/src/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function createEnvironment({
minify = false,
isLibrary = false,
scopeHoist = false,
sourceMap,
}: EnvironmentOpts = {}): Environment {
if (context == null) {
if (engines?.node) {
Expand Down Expand Up @@ -85,6 +86,7 @@ export function createEnvironment({
isLibrary,
minify,
scopeHoist,
sourceMap,
};
}

Expand Down Expand Up @@ -112,5 +114,6 @@ export function getEnvironmentHash(env: Environment): string {
outputFormat: env.outputFormat,
isLibrary: env.isLibrary,
scopeHoist: env.scopeHoist,
sourceMap: env.sourceMap,
});
}
61 changes: 39 additions & 22 deletions packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ export default class PackagerRunner {
let info = await this.processBundle(bundle, bundleGraph, ref);
bundleInfoMap[bundle.id] = info;
if (!info.hashReferences.length) {
hashRefToNameHash.set(bundle.hashReference, info.hash.slice(-8));
hashRefToNameHash.set(
bundle.hashReference,
this.options.contentHash
? info.hash.slice(-8)
: bundle.id.slice(-8),
);
writeEarlyPromises[bundle.id] = this.writeToDist({
bundle,
info,
Expand All @@ -130,7 +135,12 @@ export default class PackagerRunner {
}
}),
);
assignComplexNameHashes(hashRefToNameHash, bundles, bundleInfoMap);
assignComplexNameHashes(
hashRefToNameHash,
bundles,
bundleInfoMap,
this.options,
);
await Promise.all(
bundles.map(
bundle =>
Expand Down Expand Up @@ -262,8 +272,8 @@ export default class PackagerRunner {
}

getSourceMapReference(bundle: NamedBundle, map: ?SourceMap): Async<?string> {
if (map && this.options.sourceMaps && !bundle.isInline) {
if (bundle.target.sourceMap && bundle.target.sourceMap.inline) {
if (map && bundle.env.sourceMap && !bundle.isInline) {
if (bundle.env.sourceMap && bundle.env.sourceMap.inline) {
return this.generateSourceMap(bundleToInternalBundle(bundle), map);
} else {
return path.basename(bundle.filePath) + '.map';
Expand Down Expand Up @@ -405,10 +415,10 @@ export default class PackagerRunner {

if (bundle.target) {
if (
bundle.target.sourceMap &&
bundle.target.sourceMap.sourceRoot !== undefined
bundle.env.sourceMap &&
bundle.env.sourceMap.sourceRoot !== undefined
) {
sourceRoot = bundle.target.sourceMap.sourceRoot;
sourceRoot = bundle.env.sourceMap.sourceRoot;
} else if (
this.options.serve &&
bundle.target.env.context === 'browser'
Expand All @@ -417,18 +427,18 @@ export default class PackagerRunner {
}

if (
bundle.target.sourceMap &&
bundle.target.sourceMap.inlineSources !== undefined
bundle.env.sourceMap &&
bundle.env.sourceMap.inlineSources !== undefined
) {
inlineSources = bundle.target.sourceMap.inlineSources;
inlineSources = bundle.env.sourceMap.inlineSources;
} else if (bundle.target.env.context !== 'node') {
// inlining should only happen in production for browser targets by default
inlineSources = this.options.mode === 'production';
}
}

let mapFilename = filePath + '.map';
let isInlineMap = bundle.target.sourceMap && bundle.target.sourceMap.inline;
let isInlineMap = bundle.env.sourceMap && bundle.env.sourceMap.inline;

let stringified = await map.stringify({
file: path.basename(mapFilename),
Expand Down Expand Up @@ -464,12 +474,12 @@ export default class PackagerRunner {
}

// TODO: add third party configs to the cache key
let {sourceMaps} = this.options;
let {publicUrl} = bundle.target;
return md5FromObject({
parcelVersion: PARCEL_VERSION,
packager,
optimizers,
opts: {sourceMaps},
target: {publicUrl},
hash: bundleGraph.getHash(bundle),
configResults,
});
Expand Down Expand Up @@ -520,8 +530,7 @@ export default class PackagerRunner {
bundle.type = info.type;
}

// Without content hashing, the hash reference is already the correct id
if (this.options.contentHash && filePath.includes(thisHashReference)) {
if (filePath.includes(thisHashReference)) {
let thisNameHash = nullthrows(hashRefToNameHash.get(thisHashReference));
filePath = filePath.replace(thisHashReference, thisNameHash);
name = name.replace(thisHashReference, thisNameHash);
Expand Down Expand Up @@ -560,9 +569,8 @@ export default class PackagerRunner {

let mapKey = cacheKeys.map;
if (
(typeof bundle.target.sourceMap === 'object'
? !bundle.target.sourceMap.inline
: bundle.target.sourceMap) &&
bundle.env.sourceMap &&
!bundle.env.sourceMap.inline &&
(await this.options.cache.blobExists(mapKey))
) {
let mapStream = this.options.cache.getStream(mapKey);
Expand Down Expand Up @@ -679,7 +687,12 @@ function replaceStream(hashRefToNameHash) {
});
}

function assignComplexNameHashes(hashRefToNameHash, bundles, bundleInfoMap) {
function assignComplexNameHashes(
hashRefToNameHash,
bundles,
bundleInfoMap,
options,
) {
for (let bundle of bundles) {
if (hashRefToNameHash.get(bundle.hashReference) != null) {
continue;
Expand All @@ -691,9 +704,13 @@ function assignComplexNameHashes(hashRefToNameHash, bundles, bundleInfoMap) {

hashRefToNameHash.set(
bundle.hashReference,
md5FromString(
includedBundles.map(bundleId => bundleInfoMap[bundleId].hash).join(':'),
).slice(-8),
options.contentHash
? md5FromString(
includedBundles
.map(bundleId => bundleInfoMap[bundleId].hash)
.join(':'),
).slice(-8)
: bundle.id.slice(-8),
);
}
}
Expand Down

0 comments on commit 5510b03

Please sign in to comment.