diff options
Diffstat (limited to 'tools/ts_library_builder/build_library.ts')
-rw-r--r-- | tools/ts_library_builder/build_library.ts | 65 |
1 files changed, 23 insertions, 42 deletions
diff --git a/tools/ts_library_builder/build_library.ts b/tools/ts_library_builder/build_library.ts index 9bd4d4ee5..e4111fe21 100644 --- a/tools/ts_library_builder/build_library.ts +++ b/tools/ts_library_builder/build_library.ts @@ -123,54 +123,36 @@ export function flatten({ namespace.addStatements(statements); } -interface MergeOptions { +interface MergeGlobalOptions { basePath: string; - declarationProject: Project; debug?: boolean; - globalVarName: string; + declarationProject: Project; filePath: string; + globalVarName: string; inputProject: Project; interfaceName: string; - namespaceName: string; targetSourceFile: SourceFile; } -/** Take a module and merge into into a single namespace */ -export function merge({ +/** Take a module and merge it into the global scope */ +export function mergeGlobal({ basePath, - declarationProject, debug, - globalVarName, + declarationProject, filePath, + globalVarName, inputProject, interfaceName, - namespaceName, targetSourceFile -}: MergeOptions) { - // We have to build the module/namespace in small pieces which will reflect - // how the global runtime environment will be for Deno - - // We need to add a module named `"globals"` which will contain all the global - // runtime context - const mergedModule = targetSourceFile.addNamespace({ - name: namespaceName, - hasDeclareKeyword: true, - declarationKind: NamespaceDeclarationKind.Module - }); - - // Add the global Window interface - const interfaceDeclaration = mergedModule.addInterface({ - name: interfaceName - }); - - // Add the global scope augmentation module of the "globals" module - const mergedGlobalNamespace = mergedModule.addNamespace({ - name: "global", - declarationKind: NamespaceDeclarationKind.Global +}: MergeGlobalOptions): void { + // Add the global object interface + const interfaceDeclaration = targetSourceFile.addInterface({ + name: interfaceName, + hasDeclareKeyword: true }); // Declare the global variable - addVariableDeclaration(mergedGlobalNamespace, globalVarName, interfaceName); + addVariableDeclaration(targetSourceFile, globalVarName, interfaceName, true); // Add self reference to the global variable addInterfaceProperty(interfaceDeclaration, globalVarName, interfaceName); @@ -201,9 +183,9 @@ export function merge({ TypeGuards.isPropertyAccessExpression(leftExpression) && leftExpression.getExpression().getText() === globalVarName ) { - const windowProperty = leftExpression.getName(); - if (windowProperty !== globalVarName) { - globalVariables.set(windowProperty, { + const globalVarProperty = leftExpression.getName(); + if (globalVarProperty !== globalVarName) { + globalVariables.set(globalVarProperty, { type: firstChild.getType(), node }); @@ -228,7 +210,7 @@ export function merge({ dependentSourceFiles.add(valueDeclaration.getSourceFile()); } } - addVariableDeclaration(mergedGlobalNamespace, property, type); + addVariableDeclaration(targetSourceFile, property, type, true); addInterfaceProperty(interfaceDeclaration, property, type); } @@ -255,7 +237,7 @@ export function merge({ const dtsSourceFile = declarationProject.getSourceFileOrThrow( dtsFilePath ); - mergedModule.addStatements( + targetSourceFile.addStatements( namespaceSourceFile(dtsSourceFile, { debug, namespace: declaration.getNamespaceImportOrThrow().getText(), @@ -267,7 +249,7 @@ export function merge({ } if (debug) { - addSourceComment(mergedModule, sourceFile, basePath); + addSourceComment(targetSourceFile, sourceFile, basePath); } } @@ -413,20 +395,19 @@ export function main({ console.log(`Created module "deno".`); } - merge({ + mergeGlobal({ basePath, - declarationProject, debug, - globalVarName: "window", + declarationProject, filePath: `${basePath}/js/globals.ts`, + globalVarName: "window", inputProject, interfaceName: "Window", - namespaceName: `"globals"`, targetSourceFile: libDTs }); if (!silent) { - console.log(`Created module "globals".`); + console.log(`Merged "globals" into global scope.`); } // Add the preamble |