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

PoC: SWC #975

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

PoC: SWC #975

wants to merge 3 commits into from

Conversation

mmkal
Copy link

@mmkal mmkal commented Jul 17, 2022

Hi! Was going to write this as a comment but I thought a functional PR would be a nicer way to make a proposal.

A cleaned up version of this could resolve #943 in a way that didn't involve either inflating microbundle's size or completely rewriting the whole library around SWC.

Basically, it looks for the @swc/core dependency, and if it's found, assumes that the user wants to use swc instead of babel or typescript. But @swc/core isn't a dependency of microbundle. Which means most users can just continue using microbundle as is and not be affected. People who want to try microbundle out with SWC just have to install @swc/core which has the nice properties of being natural and intuitive for people who do want to use SWC, and unusual for anyone else.

It does add rollup-plugin-swc3 as a proper dependency of microbundle, because that's just a small javascript module, and it seems like the sort of thing microbundle should save users from having to worry about (whether they should use rollup-plugin-swc, rollup-plugin-swc2 or rollup-plugin-swc3, and how to configure the chosen package).

I tried on a small typescript project with no dependencies, with an output of about 4kb. With swc it took about 6s, without it took 21s (and using --compress false, timings were 3s vs 19s , see point 4 below).

Some things that should be addressed before this PoC were to be shippable (I didn't attempt to do this because I am not very knowledgeable about microbundle, babel, or SWC):

  1. there should be a CLI flag to let people who have @swc/core installed for unrelated reasons to opt out (e.g. --swc false)
  2. it should be configured properly to output the right style in various configurations. In this PR I just hardcoded target: 'es2018' fairly arbitrarily. It should also respect any existing swc config detected, if present.
  3. docs!
  4. the rollup-plugin-swc3 minifier should be used instead of rollup-plugin-terser - probably a bunch of the perf benefits are being negated by not doing this
  5. (maybe) rely on rollup-plugin-swc3 to emit typescript declaration files too. Might or might not be faster 🤷
  6. prettier/code cleanup etc. I deliberately avoided formatting src/index.js to make the diff easy to ready, but probably some refactoring should be done
  7. (maybe) make @swc/core a peerDependency with peerDependenciesMeta.optional = true

If this goes well and people start using it, after a few months of the community helping flush out any issues, maybe something like the solution suggested by @developit in #943 (comment) could be done with SWC. But the advantage of doing this way first is that we could likely do exactly the same thing with rollup-plugin-esbuild, and effectively support users with both. The community seems pretty evenly split between the two, and they're both an order of magnitude faster than typescript or babel.

@changeset-bot
Copy link

changeset-bot bot commented Jul 17, 2022

⚠️ No Changeset found

Latest commit: 8b5295d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -107,6 +107,7 @@
"rollup": "^2.35.1",
"rollup-plugin-bundle-size": "^1.0.3",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-swc3": "^0.3.0",

Choose a reason for hiding this comment

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

You can add this config to package.json for peer deps so we pin to a particular major version of swc but keep it optional:

"peerDependenciesMeta": {
  "@swc/core": {
    "optional": true
  },
}

@quantizor
Copy link

Thanks so much for taking a first stab at this Misha!

@quantizor
Copy link

quantizor commented Jul 20, 2022

We'll want to look into a way to configure SWC plugins somehow without directly involving microbundle. Does SWC offer some sort of .swcrc config mechanism it'll just pick up when running over a codebase?

@rschristian
Copy link
Collaborator

Looks like there is a .swcrc, yes: https://swc.rs/docs/usage/core#options

@quantizor
Copy link

@developit curious to get your thoughts on this!

@rschristian
Copy link
Collaborator

Jason's on vacation right now so a response/review might be a bit.

  1. (maybe) rely on rollup-plugin-swc3 to emit typescript declaration files too. Might or might not be faster 🤷

@mmkal Have you used it for this purpose in the past? Any major issues/shortcomings you know of?

We use rollup-plugin-typescript2 which admittedly there are many problems with, so I'm curious if swc might be viable.

@quantizor

This comment was marked as off-topic.

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.

Option to use a faster transpile/bundle engine?
3 participants