From ea8a43def0d8e6da19cc9fad9c00d37014c332d2 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 30 May 2017 19:46:54 +0200 Subject: [PATCH] feat(tsc-wrapped): always convert shorthand imports (#16898) Now converts shorthand imports for every TypeScript target. Tsickle is able to expand index shorthand imports for every TypeScript target and module possibility. Expanding shorthand imports for CommonJS modules is also helpful when testing in the browser. Module loaders like SystemJS are not able to understand directory imports (or index shorthand imports) --- tools/@angular/tsc-wrapped/src/main.ts | 8 +-- tools/@angular/tsc-wrapped/test/main.spec.ts | 53 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/tools/@angular/tsc-wrapped/src/main.ts b/tools/@angular/tsc-wrapped/src/main.ts index 5a5ca27a4d..220e883a1c 100644 --- a/tools/@angular/tsc-wrapped/src/main.ts +++ b/tools/@angular/tsc-wrapped/src/main.ts @@ -99,12 +99,8 @@ export function main( addGeneratedFileName(name); } - const tsickleCompilerHostOptions: tsickle.Options = { - googmodule: false, - untyped: true, - convertIndexImportShorthand: - ngOptions.target === ts.ScriptTarget.ES2015, // This covers ES6 too - }; + const tsickleCompilerHostOptions: + tsickle.Options = {googmodule: false, untyped: true, convertIndexImportShorthand: true}; const tsickleHost: tsickle.TsickleHost = { shouldSkipTsickleProcessing: (fileName) => /\.d\.ts$/.test(fileName), diff --git a/tools/@angular/tsc-wrapped/test/main.spec.ts b/tools/@angular/tsc-wrapped/test/main.spec.ts index a5419de29f..287bd95abc 100644 --- a/tools/@angular/tsc-wrapped/test/main.spec.ts +++ b/tools/@angular/tsc-wrapped/test/main.spec.ts @@ -366,4 +366,57 @@ describe('tsc-wrapped', () => { }) .catch(e => done.fail(e)); }); + + it('should expand shorthand imports for ES2015 modules', (done) => { + write('tsconfig.json', `{ + "compilerOptions": { + "experimentalDecorators": true, + "types": [], + "outDir": "built", + "declaration": true, + "moduleResolution": "node", + "target": "es2015", + "module": "es2015" + }, + "angularCompilerOptions": { + "annotateForClosureCompiler": true + }, + "files": ["test.ts"] + }`); + + main(basePath, {basePath}) + .then(() => { + const fileOutput = readOut('js'); + expect(fileOutput).toContain(`export { A, B } from './dep/index'`); + done(); + }) + .catch(e => done.fail(e)); + }); + + it('should expand shorthand imports for ES5 CommonJS modules', (done) => { + write('tsconfig.json', `{ + "compilerOptions": { + "experimentalDecorators": true, + "types": [], + "outDir": "built", + "declaration": true, + "moduleResolution": "node", + "target": "es5", + "module": "commonjs" + }, + "angularCompilerOptions": { + "annotateForClosureCompiler": true + }, + "files": ["test.ts"] + }`); + + main(basePath, {basePath}) + .then(() => { + const fileOutput = readOut('js'); + expect(fileOutput).toContain(`var index_1 = require("./dep/index");`); + done(); + }) + .catch(e => done.fail(e)); + }); + });