Skip to content

Commit

Permalink
Fix cache invalidation when shouldOptimize changes (#7030)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Oct 7, 2021
1 parent 7d4d53a commit a6a6fb2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/core/core/src/Environment.js
Expand Up @@ -131,6 +131,7 @@ function getEnvironmentHash(env: Environment): string {
env.outputFormat,
env.sourceType,
env.isLibrary,
env.shouldOptimize,
env.shouldScopeHoist,
env.sourceMap,
]),
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/PackagerRunner.js
Expand Up @@ -563,7 +563,8 @@ export default class PackagerRunner {
invalidationHash +
bundle.target.publicUrl +
bundleGraph.getHash(bundle) +
JSON.stringify(configResults),
JSON.stringify(configResults) +
this.options.mode,
);
}

Expand Down
8 changes: 6 additions & 2 deletions packages/core/core/src/RequestTracker.js
Expand Up @@ -1016,7 +1016,7 @@ export default class RequestTracker {
}

async writeToCache() {
let cacheKey = `${PARCEL_VERSION}:${JSON.stringify(this.options.entries)}`;
let cacheKey = getCacheKey(this.options);
let requestGraphKey = hashString(`${cacheKey}:requestGraph`);
let snapshotKey = hashString(`${cacheKey}:snapshot`);

Expand Down Expand Up @@ -1072,12 +1072,16 @@ export function getWatcherOptions(options: ParcelOptions): WatcherOptions {
return {ignore};
}

function getCacheKey(options) {
return `${PARCEL_VERSION}:${JSON.stringify(options.entries)}:${options.mode}`;
}

async function loadRequestGraph(options): Async<RequestGraph> {
if (options.shouldDisableCache) {
return new RequestGraph();
}

let cacheKey = `${PARCEL_VERSION}:${JSON.stringify(options.entries)}`;
let cacheKey = getCacheKey(options);
let requestGraphKey = hashString(`${cacheKey}:requestGraph`);
let requestGraph = await options.cache.get<RequestGraph>(requestGraphKey);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/requests/AssetGraphRequest.js
Expand Up @@ -132,7 +132,7 @@ export class AssetGraphBuilder {
this.requestedAssetIds = requestedAssetIds ?? new Set();
this.shouldBuildLazily = shouldBuildLazily ?? false;
this.cacheKey = hashString(
`${PARCEL_VERSION}${name}${JSON.stringify(entries) ?? ''}`,
`${PARCEL_VERSION}${name}${JSON.stringify(entries) ?? ''}${options.mode}`,
);

this.queue = new PromiseQueue();
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core/src/requests/BundleGraphRequest.js
Expand Up @@ -345,7 +345,8 @@ class BundlerRunner {
assetGraph.getHash() +
configs +
devDepRequests +
invalidations,
invalidations +
this.options.mode,
);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/core/core/test/Environment.test.js
Expand Up @@ -6,7 +6,7 @@ import {createEnvironment} from '../src/Environment';
describe('Environment', () => {
it('assigns a default environment with nothing passed', () => {
assert.deepEqual(createEnvironment(), {
id: '28ff5688dfa7d45e',
id: 'c242f987e3544367',
context: 'browser',
engines: {
browsers: ['> 0.25%'],
Expand All @@ -24,7 +24,7 @@ describe('Environment', () => {

it('assigns a node context if a node engine is given', () => {
assert.deepEqual(createEnvironment({engines: {node: '>= 10.0.0'}}), {
id: '3bae9fa4de65ce29',
id: '69e0ab7220ee8f7a',
context: 'node',
engines: {
node: '>= 10.0.0',
Expand All @@ -44,7 +44,7 @@ describe('Environment', () => {
assert.deepEqual(
createEnvironment({engines: {browsers: ['last 1 version']}}),
{
id: '0006b4816a385465',
id: '4b5c9005af8c5b19',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand All @@ -63,7 +63,7 @@ describe('Environment', () => {

it('assigns default engines for node', () => {
assert.deepEqual(createEnvironment({context: 'node'}), {
id: '5171f29d65a099c4',
id: 'f7c9644283a8698f',
context: 'node',
engines: {
node: '>= 8.0.0',
Expand All @@ -81,7 +81,7 @@ describe('Environment', () => {

it('assigns default engines for browsers', () => {
assert.deepEqual(createEnvironment({context: 'browser'}), {
id: '28ff5688dfa7d45e',
id: 'c242f987e3544367',
context: 'browser',
engines: {
browsers: ['> 0.25%'],
Expand Down
50 changes: 25 additions & 25 deletions packages/core/core/test/TargetRequest.test.js
Expand Up @@ -120,7 +120,7 @@ describe('TargetResolver', () => {
publicUrl: '/',
distDir: normalizeSeparators(path.resolve('customA')),
env: {
id: '7185aca026a1ba43',
id: '1d40417b63734b32',
context: 'browser',
includeNodeModules: true,
engines: {
Expand All @@ -141,7 +141,7 @@ describe('TargetResolver', () => {
distEntry: 'b.js',
distDir: normalizeSeparators(path.resolve('customB')),
env: {
id: '923e2836f26d91cc',
id: '928f0d1c941b2e57',
context: 'node',
includeNodeModules: false,
engines: {
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: '5b90122270d806a3',
id: 'b552bd32da37fa8b',
context: 'node',
engines: {
node: '>= 8.0.0',
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: 'd569d73dba5024af',
id: '8804e4eb97e2703e',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/assets',
env: {
id: 'f68e756a9f45b317',
id: 'a7ed3e73c53f1923',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand Down Expand Up @@ -288,7 +288,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: '5171f29d65a099c4',
id: 'f7c9644283a8698f',
context: 'node',
engines: {
node: '>= 8.0.0',
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: '5b90122270d806a3',
id: 'b552bd32da37fa8b',
context: 'node',
engines: {
node: '>= 8.0.0',
Expand Down Expand Up @@ -365,7 +365,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: 'a06e3cc9e0541b70',
id: '1f28e9ceaf633d83',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand Down Expand Up @@ -399,7 +399,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: 'd9b90540ac44a9db',
id: '767bf6e6b675c4f3',
context: 'browser',
engines: {
browsers: ['ie11'],
Expand Down Expand Up @@ -450,7 +450,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: '5b90122270d806a3',
id: 'b552bd32da37fa8b',
context: 'node',
engines: {
node: '>= 8.0.0',
Expand Down Expand Up @@ -484,7 +484,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: 'a06e3cc9e0541b70',
id: 'ed7c0e65adee71c9',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand Down Expand Up @@ -518,7 +518,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: 'd9b90540ac44a9db',
id: 'f7692543e59e4c0a',
context: 'browser',
engines: {
browsers: ['ie11'],
Expand Down Expand Up @@ -561,7 +561,7 @@ describe('TargetResolver', () => {
distEntry: undefined,
publicUrl: 'www',
env: {
id: '0d13493649c1b9ee',
id: 'ddb6ac7c9a3a9178',
context: 'browser',
engines: {
browsers: '> 0.25%',
Expand Down Expand Up @@ -604,7 +604,7 @@ describe('TargetResolver', () => {
distDir: normalizeSeparators(path.resolve('customB')),
publicUrl: '/',
env: {
id: '7185aca026a1ba43',
id: '1d40417b63734b32',
context: 'browser',
engines: {
browsers: ['> 0.25%'],
Expand Down Expand Up @@ -646,7 +646,7 @@ describe('TargetResolver', () => {
distDir: normalizeSeparators(path.resolve('customA')),
publicUrl: '/',
env: {
id: '7185aca026a1ba43',
id: '1d40417b63734b32',
context: 'browser',
engines: {
browsers: ['> 0.25%'],
Expand Down Expand Up @@ -675,7 +675,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: 'b58d7c211621cd65',
id: 'bebcf0293c911f03',
context: 'node',
engines: {},
includeNodeModules: false,
Expand Down Expand Up @@ -1046,7 +1046,7 @@ describe('TargetResolver', () => {
distEntry: 'index.mjs',
publicUrl: '/',
env: {
id: '82a58acaebe4fd7c',
id: 'fa77701547623794',
context: 'browser',
engines: {},
includeNodeModules: true,
Expand Down Expand Up @@ -1084,7 +1084,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: '82a58acaebe4fd7c',
id: 'fa77701547623794',
context: 'browser',
engines: {},
includeNodeModules: true,
Expand Down Expand Up @@ -1126,7 +1126,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/',
env: {
id: '5b90122270d806a3',
id: 'b552bd32da37fa8b',
context: 'node',
engines: {
node: '>= 8.0.0',
Expand Down Expand Up @@ -1160,7 +1160,7 @@ describe('TargetResolver', () => {
distEntry: 'index.js',
publicUrl: '/assets',
env: {
id: 'f68e756a9f45b317',
id: 'a7ed3e73c53f1923',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand Down Expand Up @@ -1208,7 +1208,7 @@ describe('TargetResolver', () => {
distDir: '.parcel-cache/dist',
publicUrl: '/',
env: {
id: '69f74e7f31319ffd',
id: '4a236f9275d0a351',
context: 'browser',
engines: {},
includeNodeModules: true,
Expand Down Expand Up @@ -1238,7 +1238,7 @@ describe('TargetResolver', () => {
),
publicUrl: '/',
env: {
id: '04e06037831229c5',
id: 'a9c07d094d038c73',
context: 'browser',
engines: {
browsers: ['Chrome 80'],
Expand Down Expand Up @@ -1271,7 +1271,7 @@ describe('TargetResolver', () => {
distEntry: undefined,
publicUrl: '/',
env: {
id: '04e06037831229c5',
id: 'a9c07d094d038c73',
context: 'browser',
engines: {
browsers: ['Chrome 80'],
Expand Down Expand Up @@ -1309,7 +1309,7 @@ describe('TargetResolver', () => {
distEntry: undefined,
publicUrl: '/',
env: {
id: 'a06e3cc9e0541b70',
id: '1f28e9ceaf633d83',
context: 'browser',
engines: {
browsers: ['last 1 version'],
Expand Down Expand Up @@ -1337,7 +1337,7 @@ describe('TargetResolver', () => {
distEntry: undefined,
publicUrl: '/',
env: {
id: '83bb584641584b67',
id: '824e113c03cab3c8',
context: 'browser',
engines: {
browsers: ['IE 11'],
Expand Down
6 changes: 4 additions & 2 deletions packages/core/integration-tests/test/cache.js
Expand Up @@ -2235,27 +2235,29 @@ describe('cache', function() {

it('should update when minify changes', async function() {
let b = await testCache({
entries: ['src/index.html'],
defaultTargetOptions: {
shouldScopeHoist: true,
shouldOptimize: false,
},
async update(b) {
let contents = await overlayFS.readFile(
b.bundleGraph.getBundles()[0].filePath,
b.bundleGraph.getBundles()[1].filePath,
'utf8',
);
assert(contents.includes('Test'), 'should include Test');

return {
defaultTargetOptions: {
shouldScopeHoist: true,
shouldOptimize: true,
},
};
},
});

let contents = await overlayFS.readFile(
b.bundleGraph.getBundles()[0].filePath,
b.bundleGraph.getBundles()[1].filePath,
'utf8',
);
assert(!contents.includes('Test'), 'should not include Test');
Expand Down

0 comments on commit a6a6fb2

Please sign in to comment.