Navigation Menu

Skip to content
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

swc transpiler and new --transpiler option to use third-party transpilers #1160

Merged
merged 22 commits into from Feb 27, 2021

Conversation

cspotcode
Copy link
Collaborator

@cspotcode cspotcode commented Nov 22, 2020

Implements #779

Usage information is at the top of this description. Below are design questions and development to-dos.

Usage

// tsconfig.json
{
  "ts-node": {
    "transpiler": "ts-node/transpilers/swc-experimental",
    "transpileOnly": true
  }
}
# Can substitute @swc/wasm if you are unable to use native modules
npm install --save-dev typescript @swc/core @swc/helpers github:TypeStrong/ts-node#main

# should be very fast via swc
ts-node ./index.ts

Gotchas

swc uses a different helpers library, @swc/helpers instead of tslib. If you have importHelpers enabled, you will also need to install @swc/helpers in all the places where you would normally install tslib. When in doubt, there is no harm installing both.


Implementation notes, design questions

This is incomplete. To be a proper drop-in compiler I think it will need to implement additional parts of the TsCommon interface. It can pull these from require('typescript') <--this much has been implemented.

But ideally our compiler option supports composable compilers, so you can specify "compiler": ["ntypescript", "ts-node/compilers/swc"] and the swc wrapper will defer to ntypescript for things it does not implement itself. This has not been implemented yet.

Still, it might be cool for users to specify "transpileOnly": true, "compiler": "ts-node/compilers/swc" and get a performance boost.

Other compilers: (note they are only worth considering if they're noticeably faster than ts.transpileModule)

Configuration with options: bike-shedding

How should we allow passing configuration options to the transpiler?

"transpiler": "ts-node/transpilers/swc-experimental",
"transpilerOptions": {
  "swc": "@swc/wasm"
}
"transpiler": {
  "name": "ts-node/transpilers/swc-experimental",
  "swc": "@swc/wasm"
}
"transpiler": ["name", { "swc": "@swc/wasm"}]

TODO

@codecov
Copy link

codecov bot commented Nov 27, 2020

Codecov Report

Merging #1160 (1a06089) into master (3b5b9c2) will decrease coverage by 0.28%.
The diff coverage is 73.07%.

Impacted Files Coverage Δ
src/index.ts 80.56% <68.75%> (-0.02%) ⬇️
src/transpilers/swc.ts 73.52% <73.52%> (ø)
src/bin.ts 91.17% <100.00%> (+0.08%) ⬆️
transpilers/swc-experimental.js 100.00% <100.00%> (ø)

@cspotcode cspotcode changed the title WIP experimental swc compiler WIP experimental swc and esbuild compilers Dec 11, 2020
- rename from ts-node/compilers/swc to ts-node/compiler/swc-experimental
- add @swc/wasm fallback when @swc/core is not installed or available
- expand TSCommon to include all api surface used by ts-node; use TSCommon consistently (should be extracted to a different PR)
- ts-node's compiler loading logic detects a createTypescriptCompiler function and will invoke it to get instance of compiler
- fix ts-node's sourcemap comment appender to work even when TS compiler does not append a sourcemap comment.  swc does not append such a comment
Copy link

@Gerrit0 Gerrit0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From someone fairly unfamiliar with ts-node's architecture, looks good! As a user, I would definitely want a warning for ts-node failing to use the specified compiler.

targetMapping.set(/* ts.ScriptTarget.ES2019 */ 6, 'es2019')
targetMapping.set(/* ts.ScriptTarget.ES2020 */ 7, 'es2019')
targetMapping.set(/* ts.ScriptTarget.ESNext */ 99, 'es2019')
targetMapping.set(/* ts.ScriptTarget.Latest */ 99, 'es2019')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

99 is set twice - intentional?

@cspotcode
Copy link
Collaborator Author

Thinking about this, I see some problems:

  • With the current design, I'm not sure there's a clean way to warn the user when they use swc without transpileOnly, but I agree we should have such a warning.

  • I was hoping to avoid adding complexity to ts-node's core by doing this via the existing --compiler option , but then I ended up changing the compiler loader anyway to support factories.

  • It's unfortunate that every call to transpileModule needs to re-create the swc options.

Given the above problems, perhaps it is better to introduce a proper --transformer option, similar to --compiler. When this option is used, it provides the --transpileModule implementation and throws an error when used without --transpileOnly. This also allows us to enforce a very clean create() factory API, so compiler options can be parsed and established only once, and then all subsequent calls simply invoke the transformer.

--transformer? --transpiler? --transpileModuleProvider? --transpileOnlyCompiler? Not sure what to call it.

@cspotcode cspotcode changed the title WIP experimental swc and esbuild compilers swc transpiler and new --transpiler option to use third-party transpilers Feb 23, 2021
@cspotcode cspotcode marked this pull request as ready for review February 27, 2021 20:34
@cspotcode cspotcode merged commit 0274f81 into master Feb 27, 2021
@klesun
Copy link

klesun commented May 12, 2021

It shortens ts-node compilation delay about 3-6 times, thanks so much for this PR!

@QuentinFarizon
Copy link

When using yarn one should use yarn add --dev https://github.com/TypeStrong/ts-node#main typescript @swc/core @swc/helpers

Thanks for the 3x launch time improvement !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants