From 4f09f1aa4a47d0d9afc83dc601094b8e1f24c673 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Wed, 17 Oct 2018 16:54:36 +0200 Subject: [PATCH] Cache TS source files between runs of generator --- build/lib/compilation.js | 4 ++- build/lib/compilation.ts | 5 +++- build/monaco/api.js | 52 +++++++++++++++++++------------- build/monaco/api.ts | 65 +++++++++++++++++++++++++--------------- 4 files changed, 79 insertions(+), 47 deletions(-) diff --git a/build/lib/compilation.js b/build/lib/compilation.js index 14c777078c4..fb311e14a01 100644 --- a/build/lib/compilation.js +++ b/build/lib/compilation.js @@ -145,6 +145,7 @@ class MonacoGenerator { this._inputFiles.forEach(file => this._inputFileChanged[file] = true); this._recipeFileChanged = true; this._dtsFilesContents = {}; + this._dtsFilesContents2 = {}; } dispose() { this._watchers.forEach(watcher => watcher.close()); @@ -156,6 +157,7 @@ class MonacoGenerator { return; } this._dtsFilesContents[file] = contents; + this._dtsFilesContents2[file] = ts.createSourceFile(file, contents, ts.ScriptTarget.ES5); somethingChanged = true; }; const fileMap = {}; @@ -188,7 +190,7 @@ class MonacoGenerator { // Nothing changed return null; } - return monacodts.run('src', this._dtsFilesContents); + return monacodts.run2('src', this._dtsFilesContents2); } _log(message, ...rest) { util2.log(util2.colors.cyan('[monaco.d.ts]'), message, ...rest); diff --git a/build/lib/compilation.ts b/build/lib/compilation.ts index 78fb02c7cbb..30d03c069cb 100644 --- a/build/lib/compilation.ts +++ b/build/lib/compilation.ts @@ -146,6 +146,7 @@ class MonacoGenerator { private _recipeFileChanged: boolean; private _dtsFilesContents: { [filePath: string]: string; }; + private _dtsFilesContents2: { [filePath: string]: ts.SourceFile; }; constructor(isWatch: boolean) { this._isWatch = isWatch; @@ -185,6 +186,7 @@ class MonacoGenerator { this._inputFiles.forEach(file => this._inputFileChanged[file] = true); this._recipeFileChanged = true; this._dtsFilesContents = {}; + this._dtsFilesContents2 = {}; } public dispose(): void { @@ -199,6 +201,7 @@ class MonacoGenerator { return; } this._dtsFilesContents[file] = contents; + this._dtsFilesContents2[file] = ts.createSourceFile(file, contents, ts.ScriptTarget.ES5); somethingChanged = true; }; @@ -240,7 +243,7 @@ class MonacoGenerator { return null; } - return monacodts.run('src', this._dtsFilesContents); + return monacodts.run2('src', this._dtsFilesContents2); } private _log(message: any, ...rest: any[]): void { diff --git a/build/monaco/api.js b/build/monaco/api.js index e1fa18442f2..8b131969081 100644 --- a/build/monaco/api.js +++ b/build/monaco/api.js @@ -27,20 +27,6 @@ function moduleIdToPath(out, moduleId) { } return path.join(OUT_ROOT, out, moduleId) + '.d.ts'; } -let SOURCE_FILE_MAP = {}; -function getSourceFile(out, inputFiles, moduleId) { - if (!SOURCE_FILE_MAP[moduleId]) { - let filePath = path.normalize(moduleIdToPath(out, moduleId)); - if (!inputFiles.hasOwnProperty(filePath)) { - logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp'); - return null; - } - let fileContents = inputFiles[filePath]; - let sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5); - SOURCE_FILE_MAP[moduleId] = sourceFile; - } - return SOURCE_FILE_MAP[moduleId]; -} function isDeclaration(a) { return (a.kind === ts.SyntaxKind.InterfaceDeclaration || a.kind === ts.SyntaxKind.EnumDeclaration @@ -222,7 +208,7 @@ function createReplacer(data) { return str; }; } -function generateDeclarationFile(out, inputFiles, recipe) { +function generateDeclarationFile(recipe, sourceFileGetter) { const endl = /\r\n/.test(recipe) ? '\r\n' : '\n'; let lines = recipe.split(endl); let result = []; @@ -241,7 +227,7 @@ function generateDeclarationFile(out, inputFiles, recipe) { if (m1) { CURRENT_PROCESSING_RULE = line; let moduleId = m1[1]; - const sourceFile = getSourceFile(out, inputFiles, moduleId); + const sourceFile = sourceFileGetter(moduleId); if (!sourceFile) { return; } @@ -266,7 +252,7 @@ function generateDeclarationFile(out, inputFiles, recipe) { if (m2) { CURRENT_PROCESSING_RULE = line; let moduleId = m2[1]; - const sourceFile = getSourceFile(out, inputFiles, moduleId); + const sourceFile = sourceFileGetter(moduleId); if (!sourceFile) { return; } @@ -338,16 +324,15 @@ function getFilesToWatch(out) { return getIncludesInRecipe().map((moduleId) => moduleIdToPath(out, moduleId)); } exports.getFilesToWatch = getFilesToWatch; -function run(out, inputFiles) { +function _run(sourceFileGetter) { log('Starting monaco.d.ts generation'); - SOURCE_FILE_MAP = {}; let recipe = fs.readFileSync(exports.RECIPE_PATH).toString(); - let [result, usageContent] = generateDeclarationFile(out, inputFiles, recipe); + let [result, usageContent] = generateDeclarationFile(recipe, sourceFileGetter); let currentContent = fs.readFileSync(DECLARATION_PATH).toString(); log('Finished monaco.d.ts generation'); const one = currentContent.replace(/\r\n/gm, '\n'); const other = result.replace(/\r\n/gm, '\n'); - const isTheSame = one === other; + const isTheSame = (one === other); return { content: result, usageContent: usageContent, @@ -355,7 +340,32 @@ function run(out, inputFiles) { isTheSame }; } +function run(out, inputFiles) { + let SOURCE_FILE_MAP = {}; + const sourceFileGetter = (moduleId) => { + if (!SOURCE_FILE_MAP[moduleId]) { + let filePath = path.normalize(moduleIdToPath(out, moduleId)); + if (!inputFiles.hasOwnProperty(filePath)) { + logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp'); + return null; + } + let fileContents = inputFiles[filePath]; + let sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5); + SOURCE_FILE_MAP[moduleId] = sourceFile; + } + return SOURCE_FILE_MAP[moduleId]; + }; + return _run(sourceFileGetter); +} exports.run = run; +function run2(out, sourceFileMap) { + const sourceFileGetter = (moduleId) => { + let filePath = path.normalize(moduleIdToPath(out, moduleId)); + return sourceFileMap[filePath]; + }; + return _run(sourceFileGetter); +} +exports.run2 = run2; function complainErrors() { logErr('Not running monaco.d.ts generation due to compile errors'); } diff --git a/build/monaco/api.ts b/build/monaco/api.ts index 8e7eead0590..5b3fa8eb56f 100644 --- a/build/monaco/api.ts +++ b/build/monaco/api.ts @@ -32,24 +32,10 @@ function moduleIdToPath(out: string, moduleId: string): string { return path.join(OUT_ROOT, out, moduleId) + '.d.ts'; } -let SOURCE_FILE_MAP: { [moduleId: string]: ts.SourceFile; } = {}; -function getSourceFile(out: string, inputFiles: { [file: string]: string; }, moduleId: string): ts.SourceFile | null { - if (!SOURCE_FILE_MAP[moduleId]) { - let filePath = path.normalize(moduleIdToPath(out, moduleId)); - - if (!inputFiles.hasOwnProperty(filePath)) { - logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp'); - return null; - } - - let fileContents = inputFiles[filePath]; - let sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5); - - SOURCE_FILE_MAP[moduleId] = sourceFile; - } - return SOURCE_FILE_MAP[moduleId]; +export interface ISourceFileMap { + [moduleId: string]: ts.SourceFile; } - +export type SourceFileGetter = (moduleId: string) => ts.SourceFile | null; type TSTopLevelDeclaration = ts.InterfaceDeclaration | ts.EnumDeclaration | ts.ClassDeclaration | ts.TypeAliasDeclaration | ts.FunctionDeclaration | ts.ModuleDeclaration; type TSTopLevelDeclare = TSTopLevelDeclaration | ts.VariableStatement; @@ -266,7 +252,7 @@ function createReplacer(data: string): (str: string) => string { }; } -function generateDeclarationFile(out: string, inputFiles: { [file: string]: string; }, recipe: string): [string, string] { +function generateDeclarationFile(recipe: string, sourceFileGetter: SourceFileGetter): [string, string] { const endl = /\r\n/.test(recipe) ? '\r\n' : '\n'; let lines = recipe.split(endl); @@ -291,7 +277,7 @@ function generateDeclarationFile(out: string, inputFiles: { [file: string]: stri if (m1) { CURRENT_PROCESSING_RULE = line; let moduleId = m1[1]; - const sourceFile = getSourceFile(out, inputFiles, moduleId); + const sourceFile = sourceFileGetter(moduleId); if (!sourceFile) { return; } @@ -320,7 +306,7 @@ function generateDeclarationFile(out: string, inputFiles: { [file: string]: stri if (m2) { CURRENT_PROCESSING_RULE = line; let moduleId = m2[1]; - const sourceFile = getSourceFile(out, inputFiles, moduleId); + const sourceFile = sourceFileGetter(moduleId); if (!sourceFile) { return; } @@ -411,19 +397,18 @@ export interface IMonacoDeclarationResult { isTheSame: boolean; } -export function run(out: string, inputFiles: { [file: string]: string; }): IMonacoDeclarationResult { +function _run(sourceFileGetter: SourceFileGetter): IMonacoDeclarationResult { log('Starting monaco.d.ts generation'); - SOURCE_FILE_MAP = {}; let recipe = fs.readFileSync(RECIPE_PATH).toString(); - let [result, usageContent] = generateDeclarationFile(out, inputFiles, recipe); + let [result, usageContent] = generateDeclarationFile(recipe, sourceFileGetter); let currentContent = fs.readFileSync(DECLARATION_PATH).toString(); log('Finished monaco.d.ts generation'); const one = currentContent.replace(/\r\n/gm, '\n'); const other = result.replace(/\r\n/gm, '\n'); - const isTheSame = one === other; + const isTheSame = (one === other); return { content: result, @@ -433,6 +418,38 @@ export function run(out: string, inputFiles: { [file: string]: string; }): IMona }; } +export function run(out: string, inputFiles: { [file: string]: string; }): IMonacoDeclarationResult { + + let SOURCE_FILE_MAP: { [moduleId: string]: ts.SourceFile; } = {}; + const sourceFileGetter = (moduleId: string): ts.SourceFile | null => { + if (!SOURCE_FILE_MAP[moduleId]) { + let filePath = path.normalize(moduleIdToPath(out, moduleId)); + + if (!inputFiles.hasOwnProperty(filePath)) { + logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp'); + return null; + } + + let fileContents = inputFiles[filePath]; + let sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5); + + SOURCE_FILE_MAP[moduleId] = sourceFile; + } + return SOURCE_FILE_MAP[moduleId]; + }; + + return _run(sourceFileGetter); +} + +export function run2(out: string, sourceFileMap: ISourceFileMap): IMonacoDeclarationResult { + const sourceFileGetter = (moduleId: string): ts.SourceFile | null => { + let filePath = path.normalize(moduleIdToPath(out, moduleId)); + return sourceFileMap[filePath]; + }; + + return _run(sourceFileGetter); +} + export function complainErrors() { logErr('Not running monaco.d.ts generation due to compile errors'); } -- GitLab