Skip to content

Commit f95887a

Browse files
committed
Some code cleanup in LocalizationPlugin.ts
1 parent 807a210 commit f95887a

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

webpack/localization-plugin/src/LocalizationPlugin.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -505,34 +505,30 @@ export class LocalizationPlugin implements Webpack.Plugin {
505505
const stringsMap: Map<string, string> = new Map<string, string>();
506506
filesMap.set(localizedFilePath, stringsMap);
507507

508-
for (const stringName in localizedFileData) {
509-
if (!localizedFileData.hasOwnProperty || localizedFileData.hasOwnProperty(stringName)) {
510-
const stringKey: string = `${localizedFilePath}?${stringName}`;
511-
if (!this.stringKeys.has(stringKey)) {
512-
const placeholder: IStringPlaceholder = this._getPlaceholderString();
513-
this.stringKeys.set(stringKey, placeholder);
514-
}
515-
516-
const placeholder: IStringPlaceholder = this.stringKeys.get(stringKey)!;
517-
if (!this._stringPlaceholderMap.has(placeholder.suffix)) {
518-
this._stringPlaceholderMap.set(
519-
placeholder.suffix,
520-
{
521-
values: {
522-
[this._passthroughLocaleName]: stringName
523-
},
524-
locFilePath: localizedFilePath,
525-
stringName: stringName
526-
}
527-
);
528-
}
508+
for (const [stringName, stringValue] of Object.entries(localizedFileData)) {
509+
const stringKey: string = `${localizedFilePath}?${stringName}`;
510+
if (!this.stringKeys.has(stringKey)) {
511+
const placeholder: IStringPlaceholder = this._getPlaceholderString();
512+
this.stringKeys.set(stringKey, placeholder);
513+
}
529514

530-
const stringValue: string = localizedFileData[stringName];
515+
const placeholder: IStringPlaceholder = this.stringKeys.get(stringKey)!;
516+
if (!this._stringPlaceholderMap.has(placeholder.suffix)) {
517+
this._stringPlaceholderMap.set(
518+
placeholder.suffix,
519+
{
520+
values: {
521+
[this._passthroughLocaleName]: stringName
522+
},
523+
locFilePath: localizedFilePath,
524+
stringName: stringName
525+
}
526+
);
527+
}
531528

532-
this._stringPlaceholderMap.get(placeholder.suffix)!.values[localeName] = stringValue;
529+
this._stringPlaceholderMap.get(placeholder.suffix)!.values[localeName] = stringValue;
533530

534-
stringsMap.set(stringName, stringValue);
535-
}
531+
stringsMap.set(stringName, stringValue);
536532
}
537533
}
538534

@@ -726,19 +722,22 @@ export class LocalizationPlugin implements Webpack.Plugin {
726722
}
727723

728724
private _chunkHasLocalizedModules(chunk: Webpack.compilation.Chunk): boolean {
729-
if (EntityMarker.getMark(chunk) === undefined) {
730-
let chunkHasAnyLocModules: boolean = false;
731-
if (!chunkHasAnyLocModules) {
732-
for (const module of chunk.getModules()) {
733-
if (EntityMarker.getMark(module)) {
734-
chunkHasAnyLocModules = true;
735-
break;
736-
}
725+
let chunkHasAnyLocModules: boolean | undefined = EntityMarker.getMark(chunk);
726+
if (chunkHasAnyLocModules === undefined) {
727+
chunkHasAnyLocModules = false;
728+
for (const module of chunk.getModules()) {
729+
if (EntityMarker.getMark(module)) {
730+
chunkHasAnyLocModules = true;
731+
break;
737732
}
738733
}
739734

740-
// Check async chunks if this is a runtime chunk and we haven't directly found any localized modules
741-
if (chunk.hasRuntime() && !chunkHasAnyLocModules) {
735+
// If this chunk doesn't directly contain any localized resources, it still
736+
// needs to be localized if it's an entrypoint chunk (i.e. - it has a runtime)
737+
// and it loads localized async chunks.
738+
// In that case, the generated chunk URL generation code needs to contain
739+
// the locale name.
740+
if (!chunkHasAnyLocModules && chunk.hasRuntime()) {
742741
for (const asyncChunk of chunk.getAllAsyncChunks()) {
743742
if (this._chunkHasLocalizedModules(asyncChunk)) {
744743
chunkHasAnyLocModules = true;
@@ -750,13 +749,13 @@ export class LocalizationPlugin implements Webpack.Plugin {
750749
EntityMarker.markEntity(chunk, chunkHasAnyLocModules);
751750
}
752751

753-
return EntityMarker.getMark(chunk)!;
752+
return chunkHasAnyLocModules;
754753
}
755754

756755
private _convertLocalizationFileToLocData(locFile: ILocalizationFile): ILocaleFileData {
757756
const locFileData: ILocaleFileData = {};
758-
for (const stringName in locFile) { // eslint-disable-line guard-for-in
759-
locFileData[stringName] = locFile[stringName].value;
757+
for (const [stringName, locFileEntry] of Object.entries(locFile)) {
758+
locFileData[stringName] = locFileEntry.value;
760759
}
761760

762761
return locFileData;

0 commit comments

Comments
 (0)