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

How to configure SWC options? To use Stage 3 decorators, for example. #85

Closed
yay opened this issue Apr 12, 2023 · 11 comments
Closed

How to configure SWC options? To use Stage 3 decorators, for example. #85

yay opened this issue Apr 12, 2023 · 11 comments

Comments

@yay
Copy link

yay commented Apr 12, 2023

Hi,

Basically the question is this:

How can I test SWC Stage 3 decorator support when using React SWC plugin?

Some background:

SWC recently shipped with Stage 3 decorator support: https://swc.rs/docs/configuration/compilation#jsctransformdecoratorversion

I'd like to try it out with React SWC plugin, but I don't see where to pass those options. Simply creating .swcrc file (with the following contents) at the root of the project does nothing:

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "decorators": true
    },
    "transform": {
      "decoratorVersion": "2022-03"
    }
  }
}

I also don't see a config to pass SWC options to the React SWC plugin:

import { PluginOption } from "vite";
type Options = {
    /**
     * Control where the JSX factory is imported from.
     * @default "react"
     */
    jsxImportSource?: string;
    /**
     * Enable TypeScript decorators. Requires experimentalDecorators in tsconfig.
     * @default false
     */
    tsDecorators?: boolean;
    /**
     * Use SWC plugins. Enable SWC at build time.
     * @default undefined
     */
    plugins?: [string, Record<string, any>][];
};
declare const react: (_options?: Options) => PluginOption[];
export default react;

I would expect being able to pass SWC options to the React SWC plugin like so, if the options in .swcrc file are not picked up when using SWC via the React SWC plugin:

plugins: [react({
  swcOptions: {
    "jsc": {
      "parser": {
        "syntax": "ecmascript",
        "decorators": true
      },
      "transform": {
        "decoratorVersion": "2022-03"
      }
    }
  }
})],

Any help is appreciated!

@yay yay changed the title How to configure SWC options? To use Stage 3 decorators for example. How to configure SWC options? To use Stage 3 decorators, for example. Apr 12, 2023
@ArnaudBarre
Copy link
Member

Hi,

We decided to not support decorators for JS files: #60 (comment)

Seeing how much this spec has changed make me confortable with the idea that this should not be easy to use until shipped browser.

If you really want to use it with pure JS, the plugin is easy to patch

@ArnaudBarre ArnaudBarre closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2023
@yay
Copy link
Author

yay commented Apr 12, 2023

@ArnaudBarre Thank you for your quick response. I'm actually trying to make use of Stage 3 decorators in a TypeScript (v5) project. From my understanding, Stage 3 decorators are transpiled to JS code that works in current browsers, as can be seen in the following example.

If you copy-paste the transpiled output into the console, you'll see the expected output mentioned in the official TS 5.0 announcement.

With that in mind, what do you think is still missing for Stage 3 decorators to be usable in a Vite/React/Typescript project?

@ArnaudBarre
Copy link
Member

Oh I see. I need to look at how esbuild handles this, but as soon as esbuild also correctly handles the new decorator version for TS (maybe it already does) I will publish a major version that makes tsDecorators default to the standards

@yay
Copy link
Author

yay commented Apr 12, 2023

I believe the work hasn't even started in esbuild: evanw/esbuild#104
But SWC shipped with initial support a few days ago. Some work is still outstanding though:
swc-project/swc#5053 (reply in thread)
swc-project/swc#7193
But from my understanding there's enough there already for decorators to be usable.

@ArnaudBarre
Copy link
Member

Thanks for the links.
The proposal was merged 6 hours ago 😮

I will wait a week or two to see what API will be shipped in esbuild. Maybe this will be in a breaking version of esbuild, meaning that we will need to wait for Vite core to release a version with it so that the plugin can expect it.

If you want to try output the change in the meantime, you can patch the plugin to pass decoratorVersion to SWC and use plugins: [] to force the plugin to use SWC at build time so that decorators are transformed by SWC and not esbuild

@yay
Copy link
Author

yay commented Apr 12, 2023

The proposal was merged 6 hours ago

I think it was initially thought to be ready a few months ago, followed by some normative updates:
microsoft/TypeScript#52582
tc39/proposal-decorators#499

If you want to try output the change in the meantime, you can patch the plugin to pass decoratorVersion to SWC and use plugins: [] to force the plugin to use SWC at build time so that decorators are transformed by SWC and not esbuild

Thank you. I'll give it a try!

@thenbe
Copy link

thenbe commented Feb 24, 2024

Were you able to use ecmascript decorators with vite?

@uinz
Copy link

uinz commented Mar 18, 2024

If you want to use TS decorator and auto accessor, you can try unplugin-swc

import { defineConfig } from "vite";
import swc from "unplugin-swc";

export default defineConfig({
  plugins: [
    swc.vite({
      jsc: {
        parser: {
          syntax: "typescript",
          decorators: true,
        },
        transform: {
          decoratorMetadata: true,
          decoratorVersion: "2022-03",
          react: {
            runtime: "automatic",
          },
        },
      },
    }),
  ],
});

@AntonOfTheWoods
Copy link

@uinz can this be used with vite-plugin-react-swc, or is it intended to be used instead of it?

@uinz
Copy link

uinz commented May 24, 2024

@uinz can this be used with vite-plugin-react-swc, or is it intended to be used instead of it?

esbuild 0.21.3 has implemented the ES decorator, so vite should support it by default.

Try locking the esbuild version at 0.21.3 (e.g. pnpm override)

@AntonOfTheWoods
Copy link

esbuild 0.21.3 has implemented the ES decorator, so vite should support it by default.

I am using typescript so I'm not 100% that this is even directly the cause of my issue but isn't vitejs/vite-plugin-react#314 (comment) relevant for direct vite support?

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

No branches or pull requests

5 participants