Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/webpack5-localization-plugin",
"comment": "Fix \"Could not find asset\" errors on rebuilds that reuse cached localized chunks when `realContentHash` is enabled.",
"type": "patch"
}
],
"packageName": "@rushstack/webpack5-localization-plugin"
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ export async function processLocalizedAssetCachedAsync(
await cacheItem.storePromise(output);
}

const { localizedFiles, processedAssets } = output;
const { processedAssets } = output;
// Copy the map, because the realContentHash step renames its entries in place.
// Without the copy, a later build that reads this item from the cache gets the renamed filenames.
const localizedFiles: Record<string, string> = { ...output.localizedFiles };

(chunk as ILocalizedWebpackChunk).localizedFiles = localizedFiles;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

jest.disableAutomock();
import { resolve } from 'node:path';
import { promisify } from 'node:util';

import webpack, { type Compiler, type Stats, type StatsError } from 'webpack';
import { Volume } from 'memfs/lib/volume';

import { LocalizationPlugin } from '../LocalizationPlugin';
import type { ILocalizationPluginOptions } from '../interfaces';
import { MemFSPlugin } from './MemFSPlugin';

interface IBuildResult {
errors: StatsError[] | undefined;
warnings: StatsError[] | undefined;
files: string[];
}

describe(LocalizationPlugin.name, () => {
it('Reuses cached localized chunks on a rebuild with realContentHash', async () => {
const memoryFileSystem: Volume = new Volume();
memoryFileSystem.fromJSON(
{
'/a/package.json': '{ "name": "a", "sideEffects": ["entry.js", "async.js", "asyncLoc.js"] }',
'/a/async.js': `console.log("first");`,
'/a/asyncLoc.js': `import strings from './strings.loc.json'; console.log(strings.test);`,
'/a/entry.js': `import(/* webpackChunkName: 'asyncLoc' */ './asyncLoc');import(/* webpackChunkName: 'async' */ './async');`,
'/a/strings.loc.json': `{"test":{"value":"blah","comment":"A string"}}`
},
'/'
);

const loader: string = resolve(__dirname, '../loaders/locjson-loader.js');
const options: ILocalizationPluginOptions = {
localizedData: {
defaultLocale: {
localeName: 'LOCALE1'
},
translatedStrings: {
LOCALE2: {
'/a/strings.loc.json': {
test: 'baz'
}
}
}
},
realContentHash: true
};

const compiler: Compiler = webpack({
entry: {
main: '/a/entry.js'
},
output: {
path: '/release',
filename: '[name]-[locale]-[contenthash].js',
chunkFilename: 'chunks/[name]-[locale]-[contenthash].js',
hashSalt: '1'
},
module: {
rules: [
{
test: /\.loc.json$/,
use: {
loader
},
type: 'javascript/esm',
sideEffects: false
}
]
},
optimization: {
minimize: false,
moduleIds: 'named'
},
// Keep the plugin's cache entries between the two runs, as watch mode does
cache: {
type: 'memory'
},
context: '/',
mode: 'production',
plugins: [new LocalizationPlugin(options), new MemFSPlugin(memoryFileSystem)]
});

const runAsync: () => Promise<Stats | undefined> = promisify(compiler.run.bind(compiler));

async function buildAsync(): Promise<IBuildResult> {
const stats: Stats | undefined = await runAsync();
if (!stats) {
throw new Error(`Expected stats`);
}

const { errors, warnings } = stats.toJson('errors-warnings');
const files: string[] = Array.from(stats.compilation.getAssets(), ({ name }) => name).sort();
return { errors, warnings, files };
}

const firstBuild: IBuildResult = await buildAsync();
expect(firstBuild.errors).toHaveLength(0);
expect(firstBuild.warnings).toHaveLength(0);

// Change a module outside of the localized chunk, so that the localized chunk is read from the cache
memoryFileSystem.writeFileSync('/a/async.js', `console.log("second");`);

const secondBuild: IBuildResult = await buildAsync();
await promisify(compiler.close.bind(compiler))();

expect(secondBuild.errors).toHaveLength(0);
expect(secondBuild.warnings).toHaveLength(0);

const localizedChunkFiles = (files: string[]): string[] =>
files.filter((file) => file.startsWith('chunks/asyncLoc-'));
expect(localizedChunkFiles(secondBuild.files)).toEqual(localizedChunkFiles(firstBuild.files));
});
});
Loading