-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Leverage SWC parser to transpile JSX and TypeScript #5210
Comments
That is indeed something I see on the roadmap. There could even be two different modes of operation for each: "transpile" and "preserve". The problem is only that a lot of work is involved to make this possible. "preserve" is probably a little easier, as we would "only" need to define the corresponding AST nodes in Rollup, write the encoder/decoders for those nodes and think how scoping etc. works. For "transpile", we would need to do the same, but we would also need to handle rendering. For TypeScript, this mostly means removing types, but when we would also need to start thinking about output compatibility targets, and then it gets really hairy, e.g. do we want to start replacing classes with other constructs like tsc would? |
Of course, "preserve" could also become a killer feature for TypeScript, as we could basically bundle TypeScript to TypeScript, not sure if there are many other tools that support that... |
Amazing! I'm glad to hear that that I haven't totally lost my mind and the idea makes sense at a high level. Related: ESBuild can also run in the browser thanks to WASM, since May evanw/esbuild#797 (comment) I think in the mean time I may try to craft a custom Rollup plugin that invokes the ESBuild WASM build and uses that for transpilation of TS and JSX. Then the app will unfortunately be pulling in two relatively large WASM assets, one for Rollup's SWC parser and another the ESBuild. At least this is the path forward that I see now - there may be better options.
I'd be curious to map out this space of work and contribute where I can. Here's a rough first pass at structuring the work:
|
I guess the first thing to do is a theoretical question: What should a TypeScript/JSX AST look like? We currently go to great lengths to ensure the AST in Rollup (that is exposed via Then there is one thing that we could do as a first step for both the "preserve" and "transpile" efforts which would be to support TypeScript and JSX only in
Once that is done, we have a usable new feature that we can release while based on that, we can then progress into one or the other direction. |
Here's another idea, which would be lighter weight than augmenting the AST while still fulfilling the requirements I'm looking for:
Would that make sense? |
That plugin just uses the opaque |
Feature Use Case
As a developer building an in-browser IDE using
@rollup/browser
, I want to enable my users to author.jsx
,.ts
, and.tsx
files while also minimizing bundle size (of the in-browser IDE app itself, not the user-generated content).Feature Proposal
There exists at present several Rollup plugins that transpile JSX and strip out TypeScript, such as @rollup/plugin-babel and @rollup/plugin-sucrase, but these are not designed to run in the browser and, and if they could run in the browser would introduce substantial overhead.
The feature proposal is to leverage the new WASM SWC build introduced in #5073 to transpile JSX and strip out TypeScript. This should, in theory, be possible because SWC provides options to perform these transformations as part of its API. See https://swc.rs/docs/configuration/compilation
It may be possible to implement this feature by allowing Rollup JavaScript API users to specify custom overrides for the SWC parser options, specifically for TypeScript (
jsc.parser.syntax = "typescript"
) and JSX (jsc.parser.tsx = true
).This would allow Rollup users to transpile TypeScript and JSX without relying on additional third party Rollup plugins.
Might this be feasible?
The text was updated successfully, but these errors were encountered: