Skip to content

Commit 2e0bdff

Browse files
committed
Implement a cache for loc file parsing.
1 parent de40bb6 commit 2e0bdff

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

webpack/localization-plugin/src/TypingsGenerator.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import {
55
FileSystem,
6-
JsonFile,
76
Terminal,
87
ConsoleTerminalProvider,
98
Path
@@ -14,13 +13,7 @@ import { EOL } from 'os';
1413
import * as chokidar from 'chokidar';
1514

1615
import { ILocFile } from './interfaces';
17-
import { ResxReader } from './utilities/ResxReader';
18-
import { Constants } from './utilities/Constants';
19-
import {
20-
Logging,
21-
ILoggingFunctions,
22-
ILoggerOptions
23-
} from './utilities/Logging';
16+
import { ILoggerOptions } from './utilities/Logging';
2417
import { LocFileParser } from './utilities/LocFileParser';
2518

2619
/**
@@ -41,7 +34,6 @@ export interface ITypingsGeneratorOptions {
4134
*/
4235
export class TypingsGenerator {
4336
private _options: ITypingsGeneratorOptions;
44-
private _loggingFunctions: ILoggingFunctions;
4537
private _loggingOptions: ILoggerOptions;
4638

4739
public constructor(options: ITypingsGeneratorOptions) {
@@ -74,7 +66,6 @@ export class TypingsGenerator {
7466
writeError: this._options.terminal.writeErrorLine.bind(this._options.terminal),
7567
writeWarning: this._options.terminal.writeWarningLine.bind(this._options.terminal)
7668
};
77-
this._loggingFunctions = Logging.getLoggingFunctions(this._loggingOptions);
7869
}
7970

8071
public generateTypings(): void {
@@ -84,42 +75,22 @@ export class TypingsGenerator {
8475
return path.resolve(this._options.srcFolder, fileToIgnore);
8576
}));
8677

87-
const locJsonFilePaths: string[] = glob.sync(
88-
path.join('**', '*.loc.json'),
89-
{
90-
root: this._options.srcFolder,
91-
absolute: true
92-
}
93-
);
94-
95-
for (let locJsonFilePath of locJsonFilePaths) {
96-
locJsonFilePath = path.resolve(locJsonFilePath);
97-
98-
if (filesToIgnore.has(locJsonFilePath)) {
99-
continue;
100-
}
101-
102-
const locFileData: ILocFile = JsonFile.loadAndValidate(locJsonFilePath, Constants.LOC_JSON_SCHEMA);
103-
this._generateTypingsForLocFile(locJsonFilePath, locFileData);
104-
}
105-
106-
const resxFiles: string[] = glob.sync(
107-
path.join('**', '*.resx'),
78+
const locFilePaths: string[] = glob.sync(
79+
path.join('**', '*+(.resx|.loc.json)'),
10880
{
10981
root: this._options.srcFolder,
11082
absolute: true
11183
}
11284
);
11385

114-
for (let resxFilePath of resxFiles) {
115-
resxFilePath = path.resolve(resxFilePath);
86+
for (let locFilePath of locFilePaths) {
87+
locFilePath = path.resolve(locFilePath);
11688

117-
if (filesToIgnore.has(resxFilePath)) {
89+
if (filesToIgnore.has(locFilePath)) {
11890
continue;
11991
}
12092

121-
const locFileData: ILocFile = ResxReader.readResxFileAsLocFile({ ...this._loggingFunctions, resxFilePath });
122-
this._generateTypingsForLocFile(resxFilePath, locFileData);
93+
this._parseFileAndGenerateTypings(locFilePath);
12394
}
12495
}
12596

webpack/localization-plugin/src/utilities/LocFileParser.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ export interface IParseLocFileOptions {
1818
content: string;
1919
}
2020

21+
interface IParseCacheEntry {
22+
content: string;
23+
parsedFile: ILocFile;
24+
}
25+
26+
const parseCache: Map<string, IParseCacheEntry> = new Map<string, IParseCacheEntry>();
27+
2128
export class LocFileParser {
2229
public static parseLocFileFromLoader(content: string, loaderContext: loader.LoaderContext): ILocFile {
23-
debugger;
2430
return LocFileParser.parseLocFile({
2531
filePath: loaderContext.resourcePath,
2632
loggerOptions: { writeError: loaderContext.emitError, writeWarning: loaderContext.emitWarning },
@@ -29,23 +35,32 @@ export class LocFileParser {
2935
}
3036

3137
public static parseLocFile(options: IParseLocFileOptions): ILocFile {
38+
if (parseCache.has(options.filePath)) {
39+
const entry: IParseCacheEntry = parseCache.get(options.filePath)!;
40+
if (entry.content === options.content) {
41+
return entry.parsedFile;
42+
}
43+
}
44+
45+
let parsedFile: ILocFile;
3246
if (/\.resx$/i.test(options.filePath)) {
33-
return ResxReader.readResxAsLocFile(
47+
parsedFile = ResxReader.readResxAsLocFile(
3448
options.content,
3549
{
3650
...Logging.getLoggingFunctions(options.loggerOptions),
3751
resxFilePath: options.filePath
3852
}
3953
);
4054
} else {
41-
const locJsonFileData: ILocFile = jju.parse(options.content);
55+
parsedFile = jju.parse(options.content);
4256
try {
43-
Constants.LOC_JSON_SCHEMA.validateObject(locJsonFileData, options.filePath);
57+
Constants.LOC_JSON_SCHEMA.validateObject(parsedFile, options.filePath);
4458
} catch (e) {
4559
options.loggerOptions.writeError(`The loc file is invalid. Error: ${e}`);
4660
}
47-
48-
return locJsonFileData;
4961
}
62+
63+
parseCache.set(options.filePath, { content: options.content, parsedFile });
64+
return parsedFile;
5065
}
5166
}

0 commit comments

Comments
 (0)