Open alangpierce opened 6 years ago
@zerkalica
d.ts and flow declarations generation
Unfortunately I think this is too hard for Sucrase to do. I'm not 100% sure about flow declarations, but .d.ts generation is definitely out of scope.
As one example, d.ts generation relies on TypeScript's type inference algorithm, so this code:
export function foo() {
return 3;
}
gets this type definition:
export declare function foo(): number;
Determining that 3
has type number
maybe isn't conceptually so bad, but there are much more complicated cases (e.g. a variable that has a refined type due to previous control flow operations), and doing this right means that Sucrase would need to reimplement the whole TypeScript type system. TypeScript also infers types across files, and one design goal of Sucrase is that it can compile each file individually.
Sucrase is simple and fast because it just removes types and doesn't need to know the whole type system, so any features requiring type system knowledge or cross-file resolution are out of scope. The TypeScript support in Babel has the same limitations. Also, the main intended use case for Sucrase is development builds where iteration speed is important, and my understanding is the main use case for .d.ts files is production builds when shipping a library.
TypeScript recently added a new --emitDeclarationsOnly
flag if you want to have Sucrase compile the JS and TypeScript compile the .d.ts files, so that's one way to get the build to work:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#new---emitdeclarationsonly
resolve import aliases. ts: baseUrl, paths from tsconfig.json support, flow: module.name_mapper from .flowconfig
What's the use case for path resolution? Sucrase just takes import
statements in and produces either the same import
or an equivalent require
. All path resolution should happen later, when running the code emitted by Sucrase. Again, it's just like Babel in that sense. To be clear, Sucrase just transforms code and doesn't do any typechecking; you need to run typechecking separately.
Path aliasing needed to avoid relative paths ../../
hell in libraries and applications above 1K SLOC.
Relative path is a big problem in js imports. There are many workarounds to fix it: tspath, path transform in awesome-typescript-loader, path transform in parcel-plugin-typescript. In babel environment people use babel-plugin-module-resolver.
@zerkalica I see, thanks for explaining. At my work, we have webpack and node set up to accept absolute paths, so it's not needed at transpile time. In webpack, it's the resolve.modules
setting, and in node, it's NODE_PATH
. But I guess NODE_PATH
won't be supported in ES modules (still experimental in node 10, but maybe node 12 will have them), as I understand things, so I can see why you might prefer to resolve paths at transpile time.
I want to keep the project focused and avoid feature creep, so I'd rather hold off on the feature for now, but if there's more demand for it, I could see it being added in the future. Some more thoughts/concerns:
Possibly a way to get this working at transpile time would be a two-phase step where you run Sucrase and then a separate import-rewriting step. I've thought about pulling the Sucrase parser into its own package so that anyone could make a separate transform with a fast parser. It would be a little less efficient than ideal, though, since you'd need to re-parse after the first transform is done. In this case, though, I bet you could get away with more superficial parsing than Sucrase needs, like just using regexes or something.
Yes, @alangpierce , these are the features I consider important for TypeScrip support.
I do not yet know if this is possible with sucrase. But I will watch the topics in anticipation of examples of solving the problem.
Also you should try prepack #269
I'll try to implement stuff around reports
Mobile support: https://github.com/alangpierce/sucrase/pull/451
Allow Sucrase to read a tsconfig.json for its config (including which directories to compile and which to exclude, etc).
Implemented in https://github.com/alangpierce/sucrase/pull/509
I've had a few requests from others about how they can help out. I've been keeping a checklist of possible future tasks, and figured it would be good to make that public to make it easier for others to contribute.
Feel free to stop by and chat in the gitter room, which I just created. Generally my thought is that this can be a big checklist of possible things to do, and as work starts on them, they can be "promoted" to real issues with individual discussions/assignees/etc. I'm sure there's plenty of context to be shared about most of these issues, so feel free to chat if you want to take one of them on.
Currently, I'm focused on performance, and want to see how high I can get the N in "Sucrase is N times faster than Babel". But there's certainly lots of other stuff to do as well.
Stability
Bugs
export let x; [x] = arr;
. Babel and TypeScript are both incomplete here, and ideally we just mimic one or the other.import
statements should be hoisted to the top of the file, but they aren't right now.import
andexport
. Ideally we exactly copy either Babel or TypeScript, based on the config.export default
React class. We could add a filename arg to the Sucrase API to support this, although it's maybe obscure enough that the API change isn't worth it.Tooling
noImplicitThis
in the TypeScript config. Implicitany
forthis
has bit me a few times, but there are enough pre-existing errors that it's hard to enable.self-build
task, but we still use TypeScript for the real build.Website (sucrase.io)
Profiling/benchmarking
Features
Configuration
Integrations
Performance
+
.