-
Notifications
You must be signed in to change notification settings - Fork 1
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
module: add --experimental-strip-types #2
Conversation
9333ed4
to
d5a23b0
Compare
b1ac849
to
fa32fc9
Compare
This comment was marked as resolved.
This comment was marked as resolved.
cf1d7f5
to
f507952
Compare
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
It's a bit more complex to set up, but couldn't we just get the |
if it's slow it's should not from SWC |
This comment was marked as outdated.
This comment was marked as outdated.
I mean this implementation is not refined for performance |
831ef09
to
be6a202
Compare
**Description:** This PR adds a Wasm binding which is only capable of stripping TypeScript types. **Related issue:** - marco-ippolito/node#2
Since we are going to bundle it the binary, we cant just do
|
Simply untested, so I preferred to do that in a follow up pr |
I added some tests, published As requested at nodejs/loaders#208 (comment), it only strips type and does not touch If required, I can modify it to make the behavior configurable via option. |
Just to clarify, it will throw when enums are encountered? |
It seems the package is not working as I expected, like @swc/wasm, seems more for the browser @swc/wasm-typescript |
I will rename to |
I published a new version using import { transform } from "@swc/wasm-typescript";
const { code } = await transform(
`const a = 1;
type Foo = number;`, {});
console.log(code); emits By the way, should it throw when it encounters an enum? It now preserves enums. Trying to execute the output will fail anyway, but I can add some early errors for it. |
Thank you so much! Yes errors should be throw during transpilation, otherwise they will be syntax errors that are hard to wrap and to make them intellegible |
cd9816c
to
d9d9e32
Compare
tools/patch-swc-wasm.js
Outdated
const originalContent = fs.readFileSync(outputFile); | ||
|
||
fs.writeFileSync(outputFile, `'use strict'\n | ||
const { Buffer } = require('node:buffer');\n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kdy1 I created this script to insert the wasm as byte array in the wasm.js
file because we cannot perform a readFile or require
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I inlined the wasm binary. https://www.npmjs.com/package/@swc/wasm-typescript/v/1.6.8-nightly-20240704.3?activeTab=code
The tag is currently nightly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats amazing! Can we have the same linting error for namespaces?
d9d9e32
to
186fdb9
Compare
5ed84e5
to
ba740b6
Compare
ba740b6
to
e7d70d7
Compare
Moved to the node repo |
It is possible to execute TypeScript files by by setting the experimental flag
--experimental-strip-types
.Node.js will transpile TypeScript source code into JavaScript source code as
esnext
ES Modules
.During the transpilation process, no type checking is performed, and types are discarded.
Motivation
I believe enabling users to execute TypeScript files is crucial to move the ecosystem forward, it has been requested on all the surveys, and it simply cannot be ignored. We must acknowledge users want to run
node foo.ts
without installing external dependencies or loaders.Why type stripping
Type stripping as the name suggest, means removing all the
types
, transform the input in aesm
JavaScript module.Becomes:
Other runtimes also perform transformation of some TypeScript only features into JavaScript, for example enums, which do not exists in JavaScript.
At least initially in this PR no trasformation is performed, meaning that using
Enum
will not be possible.I believe as a project we don't want to vendor TypeScript directly and I would like to push the ecosystem to STANDARDIZE these features (example: https://github.com/tc39/proposal-decorators) so that they become part of the language.
Why I chose @swc/wasm-typescript:
Because of simplicity.
I have considered other tools like
swc/core
andesbuild
but they require either rust or go to be added to the toolchain.@swc/wasm-typescript
its a small package with a wasm and a js file to bind it.swc is currently used by Deno for the same purpose, it's battle tested.
In the future I see this being implemented in native layer.
Currently why just
npm pack @swc/wasm-typescript
, perform some transformation and add it to our sources.This is not the standard way we operate, we want to have the sources under versioning to be able to hotfix and monitor possible security issues.
This could be solved by vendoring swc and build the wasm ourselves.
I will do that in a immediate future iteration.
Why not TSC (typescript.js)
typescript.js
is a (at the time of writing this) a ~8.5MB and it does not perform type-stripping, but only transformation.Limitations
TypeScript only features
By design Node.js will not perform transformations on TypeScript only features.
This means
Enum
,experimentalDecorators
,namespaces
and other TS only features are not supported.You can set
isolatedModules
in yourtsconfig.json
to ensure that you are not using TypeScript only features.Importing modules without
.ts
extensionCurrently importing modules without
.ts
extension is not supported.Only files with the extension
.ts
are supported, meaning it will not support.mts
,.cts
, or.tsx
syntax.REPL
Executing source code in the REPL with TypeScript is not supported.
This also applies to
--eval
,--print
,--check
andinspect
.Source maps
Currently source maps are not supported, this could be trivial to add.
No benchmarks
This PR does not have performance in mind, as its a first iteration so no benchmarks and no bundle size measurements are performed
Next Steps
Short term
@swc/wasm-typescript
sources and build the wasm ourselvesLong term
@swc/core
as a native implementation.