Skip to content

Commit

Permalink
Add lazy/eager cache key to avoid invalid change when switching modes (
Browse files Browse the repository at this point in the history
…#9518)

* Add lazy/eager cache key to avoid invalid change when switching modes

* Update packages/core/core/src/PackagerRunner.js
  • Loading branch information
JakeLane committed Feb 2, 2024
1 parent 891d105 commit 4debc2e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,8 @@ export default class PackagerRunner {
bundleGraph.getHash(bundle) +
JSON.stringify(configResults) +
JSON.stringify(globalInfoResults) +
this.options.mode,
this.options.mode +
(this.options.shouldBuildLazily ? 'lazy' : 'eager'),
);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/core/core/src/RequestTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,9 @@ export function getWatcherOptions(options: ParcelOptions): WatcherOptions {
}

function getCacheKey(options) {
return `${PARCEL_VERSION}:${JSON.stringify(options.entries)}:${options.mode}`;
return `${PARCEL_VERSION}:${JSON.stringify(options.entries)}:${
options.mode
}:${options.shouldBuildLazily ? 'lazy' : 'eager'}`;
}

async function loadRequestGraph(options): Async<RequestGraph> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/requests/AssetGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class AssetGraphBuilder {
hashString(
`${PARCEL_VERSION}${name}${JSON.stringify(entries) ?? ''}${
options.mode
}`,
}${options.shouldBuildLazily ? 'lazy' : 'eager'}`,
) + '-AssetGraph';

this.isSingleChangeRebuild =
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/requests/BundleGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class BundlerRunner {
hashString(
`${PARCEL_VERSION}:BundleGraph:${
JSON.stringify(options.entries) ?? ''
}${options.mode}`,
}${options.mode}${options.shouldBuildLazily ? 'lazy' : 'eager'}`,
) + '-BundleGraph';
}

Expand Down
45 changes: 43 additions & 2 deletions packages/core/integration-tests/test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6200,7 +6200,7 @@ describe('cache', function () {
loadConfig({config, options}) {
return DefaultBundler[CONFIG].loadConfig({config, options});
},
bundle({bundleGraph, config}) {
DefaultBundler[CONFIG].bundle({bundleGraph, config});
},
Expand All @@ -6218,7 +6218,9 @@ describe('cache', function () {
hashString(
`${version}:BundleGraph:${
JSON.stringify(resolvedOptions.entries) ?? ''
}${resolvedOptions.mode}`,
}${resolvedOptions.mode}${
resolvedOptions.shouldBuildLazily ? 'lazy' : 'eager'
}`,
) + '-BundleGraph';

assert(
Expand Down Expand Up @@ -6788,4 +6790,43 @@ describe('cache', function () {
let res = await run(build.bundleGraph);
assert.deepEqual(res, {default: 'foo'});
});

it('invalidates correctly when switching from lazy to eager modes', async function () {
let overlayFSPackageManager = new NodePackageManager(overlayFS, __dirname);
let entry = 'source/index.js';
let options = {
mode: 'production',
defaultTargetOptions: {
shouldScopeHoist: false,
},
packageManager: overlayFSPackageManager,
shouldContentHash: false,
shouldDisableCache: false,
inputFS: overlayFS,
cacheDir: path.join(__dirname, '.parcel-cache'),
};

await fsFixture(overlayFS)`
source
lazy.js:
export default 'lazy-file';
index.js:
import('./lazy');
export default 'index-file';
`;

let lazyBundleGraph = await bundle(entry, {
...options,
shouldBuildLazily: true,
});
assert.equal(lazyBundleGraph.getBundles().length, 1);

let eagerBundleGraph = await bundle(entry, {
...options,
shouldBuildLazily: false,
});
assert.equal(eagerBundleGraph.getBundles().length, 2);
});
});

0 comments on commit 4debc2e

Please sign in to comment.