Search code examples
rustnext.jswebassemblyswcswc-compiler

SWC plugins not working / Any plugin I compile breaks Next js


The Next js framework uses the SWC compiler instead of Babel.

I need to write a plugin for SWC. Everything was working fine, but something happened. Now any plugin that I compile on my computer doesn't work in Next js.

I wrote about this many times in the SWC github. The SWC developers say something incomprehensible, but it seems that everything works for everyone except me.

I will leave links to all github discussions about this. There both the text of an error, and all data.

I don't write code. I'm installing the starter example from SWC

Separately, I note that I have tried a lot of versions of Rust, swc_core, swc_cli. Every time Next js breaks. I also tried doing this on a windows virtual machine and ubuntu. I really need help, help me understand why this is not working, even if I downgrade the swc version

https://github.com/swc-project/swc/issues/7381

https://github.com/swc-project/swc/issues/7386

https://github.com/swc-project/swc/discussions/7361


To reproduce a minimal example with an error you need to follow these steps

  1. download and install rust https://www.rust-lang.org/learn/get-started
  2. rustup default nightly-2023-03-20
  3. cargo install swc_cli
  4. swc plugin new --target-type wasm32-wasi my-first-plugin
  5. cd my-first-plugin
  6. rustup target add wasm32-wasi
  7. cargo build-wasi --release

Install next js, put the plugin in the root folder, and include it in the config(next.config.js):

const nextConfig = {
      experimental: {
        swcPlugins: [
            [require.resolve("./my_first_plugin.wasm"),{}]
        ],
  },
}

module.exports = nextConfig

Auxiliary links:

https://swc.rs/docs/plugin/ecmascript/getting-started

https://swc.rs/docs/plugin/publishing

https://nextjs.org/docs/pages/api-reference/create-next-app


Solution

  • The problem was solved with the update of SWC and Next js.

    swc_cli - v0.91.42 next js - 13.4.3-canary.2