-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use gluing modules for external module support
Changed the way we support external modules to what is described in angular/tsickle/issues/1041. Previously, what we did is described in a previous readme: https://github.com/theseanl/tscc/tree/b0f656e773bc4b43dba6876aa68340f2b5d71dd8#detailed-description-of-external-modules-handling. We replaced names that references the export of an external module. In addition to that, we used some wild hacks that required patching tsickle in order to prevent generation of `goog.requireType()` for external modules. The way described in the above linked issue requires is simpler and make us free of such hacks. I also vaguely think that this will provide a more correct behavior in case of accessing an external module's global name having side effects.
- Loading branch information
Showing
14 changed files
with
133 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
node_modules | ||
/node_modules | ||
/packages/*/node_modules | ||
dist | ||
lerna-debug.log | ||
junit.xml | ||
.tscc_temp | ||
yarn-error.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @fileoverview Transforms `import localName from "external_module"` to | ||
* `const localName = global_name_for_the_external_module`. | ||
* Also transforms `import tslib_any from 'tslib'` to `goog.require("tslib")`. | ||
*/ | ||
import ITsccSpecWithTS from './spec/ITsccSpecWithTS'; | ||
import {TsickleHost} from 'tsickle'; | ||
import {moduleNameAsIdentifier} from 'tsickle/src/annotator_host'; | ||
|
||
export function getExternsForExternalModules(tsccSpec: ITsccSpecWithTS, tsickleHost: TsickleHost): string { | ||
const moduleNames = tsccSpec.getExternalModuleNames(); | ||
const toGlobalName = tsccSpec.getExternalModuleNamesToGlobalsMap(); | ||
const header = `\n/** Generated by TSCC */` | ||
let out = ''; | ||
for (let moduleName of moduleNames) { | ||
// 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{${moduleNameAsIdentifier(tsickleHost, typeRefFile)}} | ||
* @const | ||
*/ | ||
${tsickleHost.es5Mode ? 'var' : 'const'} ${toGlobalName[moduleName]} = {};\n`; | ||
} | ||
if (out.length) out = header + out; | ||
return out; | ||
} | ||
|
||
export function getGluingModules(tsccSpec: ITsccSpecWithTS, tsickleHost: TsickleHost) { | ||
const moduleNames = tsccSpec.getExternalModuleNames(); | ||
const toGlobalName = tsccSpec.getExternalModuleNamesToGlobalsMap(); | ||
const out: {path: string, content: string}[] = []; | ||
for (let moduleName of moduleNames) { | ||
const content = `goog.module('${moduleName.replace(/([\\'])/g, '\\$1')}')\n` + | ||
`/** Generated by TSCC */\n` + | ||
`exports = ${toGlobalName[moduleName]};`; | ||
// A hypothetical path of this gluing module. | ||
let path = tsccSpec.resolveExternalModuleTypeReference(moduleName) || moduleName; | ||
path = path.replace(/(?:\.d)?\.ts$/, '.js'); | ||
out.push({path, content}); | ||
} | ||
return out; | ||
} | ||
|
162 changes: 0 additions & 162 deletions
162
packages/tscc/src/transformer/externalModuleTransformer.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.