UNPKG

@tscc/tscc

Version:

A typescript transpiler and bundler that wires up tsickle and closure compiler seamlessly

44 lines (43 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGluingModules = exports.getExternsForExternalModules = void 0; const annotator_host_1 = require("tsickle/out/src/annotator_host"); function getExternsForExternalModules(tsccSpec, tsickleHost) { const header = `\n/** Generated by TSCC */`; let out = ''; for (let [moduleName, { globalName }] of tsccSpec.getExternalModuleDataMap()) { // If a module's type definition is from node_modules, its path is used as a namespace. // otherwise, it comes from declare module '...' in user-provided files, in which the module name string // is used as a namespace. let typeRefFile = tsccSpec.resolveExternalModuleTypeReference(moduleName) || moduleName; out += ` /** * @type{typeof ${(0, annotator_host_1.moduleNameAsIdentifier)(tsickleHost, typeRefFile)}} * @const */ var ${globalName} = {};\n`; } if (out.length) out = header + out; return out; } exports.getExternsForExternalModules = getExternsForExternalModules; function getGluingModules(tsccSpec, tsickleHost) { const out = []; for (let [moduleName, { globalName }] of tsccSpec.getExternalModuleDataMap()) { // This is just no-op for normal external modules. moduleName = tsickleHost.pathToModuleName('', moduleName); const content = `goog.module('${moduleName.replace(/([\\'])/g, '\\$1')}')\n` + `/** Generated by TSCC */\n` + `exports = ${globalName};`; // A hypothetical path of this gluing module. // Note that if the alternative code path is taken, it means that something may be wrong // with the provided typescript project. TODO find a repro which the second code path is // taken and add it as a test case. let path = tsccSpec.resolveExternalModuleTypeReference(moduleName) || moduleName + `.ts`; path = path.replace(/(?:\.d)?\.ts$/, '.js'); out.push({ path, content }); } return out; } exports.getGluingModules = getGluingModules;