Skip to content

Commit 0373f6e

Browse files
committed
Merge branch 'develop' of github.com:privatenumber/webpack-localize-assets-plugin
2 parents 7e05618 + 93c2dda commit 0373f6e

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class LocalizeAssetsPlugin<LocalizedData = string> {
118118
* The placeholder is a unique enough string to guarantee that we're not accidentally
119119
* replacing `[locale]` if it happens to be in the source JS.
120120
*/
121-
interpolateLocaleToFileName(compilation, fileNameTemplatePlaceholder);
121+
interpolateLocaleToFileName(compilation, fileNameTemplatePlaceholder, true);
122122

123123
// Create localized assets by swapping out placeholders with localized strings
124124
generateLocalizedAssets(

src/utils/localize-filename.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ const { name } = require('../../package.json');
1313
export const interpolateLocaleToFileName = (
1414
compilation: WP5.Compilation,
1515
replaceWith: LocaleName,
16+
requireLocaleInFilename?: boolean,
1617
) => {
1718
const { filename, chunkFilename } = compilation.outputOptions;
1819

19-
if (typeof filename === 'string') {
20-
assert(filename.includes('[locale]'), 'output.filename must include [locale]');
21-
}
20+
if (requireLocaleInFilename) {
21+
if (typeof filename === 'string') {
22+
assert(filename.includes('[locale]'), 'output.filename must include [locale]');
23+
}
2224

23-
if (typeof chunkFilename === 'string') {
24-
assert(chunkFilename.includes('[locale]'), 'output.chunkFilename must include [locale]');
25+
if (typeof chunkFilename === 'string') {
26+
assert(chunkFilename.includes('[locale]'), 'output.chunkFilename must include [locale]');
27+
}
2528
}
2629

2730
const interpolateHook = (

tests/webpack-localize-assets-plugin.spec.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ describe(`Webpack ${webpack.version}`, () => {
269269
expect(errors[0].message).toMatch('Missing localization for key "bad key" used in /src/index.js:1:15 from locales: en, es, ja');
270270
});
271271

272-
test('missing [locale] from filename', async () => {
272+
test('missing [locale] from filename on multi locale', async () => {
273273
await expect(async () => {
274274
await build(
275275
{
@@ -279,15 +279,15 @@ describe(`Webpack ${webpack.version}`, () => {
279279
config.output.filename = '[name].js';
280280
config.plugins.push(
281281
new WebpackLocalizeAssetsPlugin({
282-
locales: localesSingle,
282+
locales: localesMulti,
283283
}),
284284
);
285285
},
286286
);
287287
}).rejects.toThrow('output.filename must include [locale]');
288288
});
289289

290-
test('missing [locale] from chunkFilename', async () => {
290+
test('missing [locale] from chunkFilename on multi locale', async () => {
291291
await expect(async () => {
292292
await build(
293293
{
@@ -299,7 +299,7 @@ describe(`Webpack ${webpack.version}`, () => {
299299
config.output.chunkFilename = '[name].js';
300300
config.plugins.push(
301301
new WebpackLocalizeAssetsPlugin({
302-
locales: localesSingle,
302+
locales: localesMulti,
303303
}),
304304
);
305305
},
@@ -413,8 +413,6 @@ describe(`Webpack ${webpack.version}`, () => {
413413
'/src/index.js': 'export default __("hello-key");',
414414
},
415415
(config) => {
416-
configureWebpack(config);
417-
418416
config.plugins.push(
419417
new WebpackLocalizeAssetsPlugin({
420418
locales: localesSingle,
@@ -426,11 +424,11 @@ describe(`Webpack ${webpack.version}`, () => {
426424
const { assets } = built.stats.compilation;
427425
expect(Object.keys(assets).length).toBe(1);
428426

429-
const enBuild = built.require('/dist/index.en.js');
427+
const enBuild = built.require('/dist/index.js');
430428
expect(enBuild).toBe(localesMulti.en['hello-key']);
431429

432430
const statsOutput = built.stats.toString();
433-
expect(statsOutput).toMatch(/index\.en\.js/);
431+
expect(statsOutput).toMatch(/index\.js/);
434432
});
435433

436434
test('multi locale', async () => {

0 commit comments

Comments
 (0)