swc-project / plugins

Plugins for swc, written in rust
Apache License 2.0
331 stars 53 forks source link

@swc/plugin-styled-components broken with craco and swc-loader #137

Closed brunocroh closed 1 year ago

brunocroh commented 1 year ago

When I add @swc/plugin-styled-components, my build stop work and I receive the output:

thread '<unnamed>' panicked at 'failed to invoke plugin: failed to invoke plugin on 'Some("/home/brunocroh/projects/floki/front-web/src/index.tsx")'

Caused by:
    0: Failed to create plugin instance
    1: Error while importing "env"."__set_transform_plugin_core_pkg_diagnostics": unknown import. Expected Function(FunctionType { params: [I32, I32], results: [] })', crates/swc/src/plugin.rs:273:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

image

craco.config.js

...
webpackConfig.module.rules = [
  ...webpackConfig.module.rules,
  {
    test: /\.(ts|tsx)$/,
    loader: 'swc-loader',
    exclude: /node_modules/,
    options: {
      parseMap: true
    }
  },
]

return webpackConfig;

.swcrc

{
  "jsc": {
  // when experimental is added the build broken
   "experimental": {
      "plugins": [
        [
          "@swc/plugin-styled-components",
          {
            "displayName": true,
            "ssr": true
          }
        ]
      ]
    },
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "dynamicImport": true,
      "exportDefaultFrom": true
    },
    "transform": {
      "react": {
        "runtime": "automatic",
        "development": true,
        "throwIfNamespace": true,
        "importSource": "react",
        "pragma": "React.createElement",
        "pragmaFrag": "React.Fragment",
        "useBuiltIns": true
      }
    }
  }
}
kwonoj commented 1 year ago

Have you tried latest cersion of @swc/core and plugins?

brunocroh commented 1 year ago

@kwonoj Problem solved with the latest version of @swc/core !

I used an earlier version because @swc/plugin-styled-components says to use @swc/core@1.2.215

https://github.com/swc-project/plugins/blob/1c055fd7183751f41f1adc9f55ba117915cc099c/packages/styled-components/README.md?plain=1#L6

Thanks!