Skip to content

Commit

Permalink
feat(tsc-wrapped): always convert shorthand imports (angular#16898)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
devversion authored and juleskremer committed Aug 24, 2017
1 parent 4449abd commit 7522cea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
8 changes: 2 additions & 6 deletions tools/@angular/tsc-wrapped/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
53 changes: 53 additions & 0 deletions tools/@angular/tsc-wrapped/test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

});

0 comments on commit 7522cea

Please sign in to comment.