-
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
JavaScript in TypeScript compilations #4792
Comments
Here are some opinions:
Side note: I've experience Visual Studio Code erroring on es6 files unless the 'target' compiler option was set to es6. It should be using a 'source' specifier; target is what we want to transpile to. |
This should be available now with #5471 using |
Any usage docs? |
I think the support of globs in tsconfig.json file is really necessary for this feature to be useful. How do I keep typescript from pulling in a lot of devtime dependencies (like gulp modules)? |
specify "exclude" property in your tsconfig. |
JavaScript in TypeScript compilations
(See parent issue #4789 for an overview)
There are a number of considerations for the TypeScript compiler to handle JavaScript code:
target
ECMAScript mode, or theJSX
flags for handling React/JSX code.node_modules
folder?express
) already has a.d.ts
declaration, and crawling the JavaScript files is unnecessary?.js
, or can other file types be provided (e.g..es
)?JavaScript files for input
There are four ways in which source files may be included in a compilation:
tsconfig.json
file exists in a folder that is compiled, and it does not explicitly list files to include. In this case, all source files in this folder and subfolders are crawled for source files to compile.files
property in atsconfig.json
file, or via files listed on thetsc
command line./// <reference ...>
tags in other source files.Implementation note: Currently, files are parsed and their dependencies included and parsed, before the next input file is parsed. For example, if a compilation includes
a.ts
andb.ts
(in that order), buta.ts
imports.\c
, thenc
will be parsed and its dependencies resolved and included beforeb.ts
is processed.Steps 1 - 3 above are unambiguous as to which file to include in the set to compile, as the file extension is part of the given or discovered file name. For step 4 (module resolution), the file extension is not given, and precedence rules are followed (module resolution will attempt to location
.ts
, then.d.ts
files for the module name. The.js
extension would go on the end of this list).Once the set of source files is gathered, the list of emit files can be calculated. If there is a collision (i.e.
src\foo.js
is both a source and emit file), then it should be removed from the list of source files if it was found via discovery (i.e. crawling the folder or resolving a module path discovered the file to be emitted). If it was listed explicitly as an input file, yet would be overwritten on emit, then an error should be raised.Transpiling JavaScript
It would be valuable to allow developers to write the same ES6 constructs they can use in TypeScript (e.g. arrow functions, classes, etc.) and provide the same conversion to ES5/ES3 that is available for TypeScript. As this should be relatively simple to implement, this would be worth adding. Of note is that due to the input/output file conflict outlined above, the emitting files would need to go to a separate folder (
--outDir
), or be combined into a separate output file (--outFile
) in order to avoid attempting to overwrite the source file on emit (else input and output will have the same file path).Compiler flags
There are two categories of compiler flags to consider:
Existing flags of interest include:
target
: should the compiler/langauge service care about the targeted ECMAScript level (especially if not transpiling)?jsx
: should the compiler/language service treat JavaScript files written in JSX syntax the same as source.tsx
files?outDir
: should the compiler copy the input JavaScript files to the output directory (especially if not transpiling)?declaration
: should the compiler emit a.d.ts
file for JavaScript files it has parsed and gathered types from?module
: should the compiler care about module type for.js
files?TODO
node_modules
js
file extensionsThe text was updated successfully, but these errors were encountered: