-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support 'target':'es5' with 'module':'es6' to allow dead code elimination #6319
Comments
@pkozlowski-opensource thanks for the two relevant related issues. #5806 presented the same issue, but it was closed in favor of #5790 to prevent redundancy. (#5790 contains relevant discussion on the topic. The real issue was clarified after being closed. @weswigham stated that 'if enough people see a need for it, we could conceivably support the configuration - it's just not something our emitter currently expects to need to do' and @kitsonk said that 'there is no harm in emitting ES5 code in ES6 module format (though my opinion is far from official)'. The issue also mentions that UglifyJS and Google Closure Compiler do not implement the same tree-shaking capabilities of Rollup/Webpack 2) So, the 2 previous issues were closed for reasons beyond the issue itself. |
This issue currently prevents using the systemjs-builder rollup support unless you also use babel in the pipeline to output es6 modules with es5 code. See frankwallis/plugin-typescript#97 (comment) So +1 from me, thanks. |
👍 Another issue for me is that Typescript's module resolution is pretty much only compatible with true ES6 modules, which means that using good old CommonJS modules (like from npm) is problematic when you want to get the default export of one of those modules. The workaround is to import them as This means that if using this workaround isn't acceptable (in my case it is, so I'm fine), you are today forced to compile to ES6 and chain with Babel to transpile everything, whereas you could have simply compiled your ES6 to ES5 with Typescript and used Babel's module transform (or even directly Webpack 2 that works the same). |
@JabX consider using |
@mhegazy That flag is only about getting the type definitions right. The problem lies in the transpiled code. Writing These are not the same, and only the second works with CommonJS. |
You can also use:
to import commonjs |
I know about this syntax, but you can't use it in JS files, which I also transpile using Typescript. I should have said so earlier. |
This is critical to the Rollup integration with TypeScript. |
Oh I see module: es6 + target: es6 will work, so scratch the above argument. Still important for Rollup support though! |
I can't find the justification why we should compile twice in order to use a separate bundler? Edit: partially works using the Compiler API. @Victorystick implemented a workaround) for $ node -e 'const ts=require("typescript");console.log(ts.transpileModule("export const cube=x=>x*x*x",{reportDiagnostics:true,compilerOptions:{module:ts.ModuleKind.ES2015}}))'
{ outputText: 'export var cube = function (x) { return x * x * x; };\n',
diagnostics:
[ { file: undefined,
start: undefined,
length: undefined,
messageText: 'Cannot compile modules into \'es2015\' when targeting \'ES5\' or lower.',
category: 1,
code: 1204 } ],
sourceMapText: undefined }
$ node -e 'const ts=require("typescript");console.log(ts.transpileModule("export const cube=x=>x*x*x",{compilerOptions:{module:ts.ModuleKind.ES2015}}))'
{ outputText: 'export var cube = function (x) { return x * x * x; };\n',
diagnostics: undefined,
sourceMapText: undefined } |
Fix #6319: Add support for `--t: es5` and `--m es6`
The import/export statements used in ES6 modules allow tools such as Webpack and Rollup.js to eliminate all the code from unused exports. (For example, see Dr. Axel Rauschmayer's post on this topic)
However, targeting ES5 remove the import/export statements, so these tools cannot remove unused exports.
TypeScript 1.7 included 'es6' and 'es2015' as valid options for the 'module' setting, but the following config:
Produces this error:
So, it looks like it is not currently possible to generate ES5 code while retaining the import/export statements of ES6 modules.
This is related to issue #4692 that proposes enabling more granular targeting beyond 'ES5' or 'ES6', but in this particular case the two required options already exist in tsconfig.json ('target' and 'module'), but the particular combination of values ('target':'es5', 'module':'es6') doesn't work.
The text was updated successfully, but these errors were encountered: