xxXyh1908 / swc-plugin-transform-vue3-jsx

SWC plugin for transform Vue3-jsx syntax
https://swc-plugin-transform-vue3-jsx.netlify.app
MIT License
8 stars 2 forks source link

swc-core核心不兼容吗? #3

Open chenyulun opened 3 weeks ago

chenyulun commented 3 weeks ago

Describe the bug

● ━━━━━━━━━━━━━━━━━━━━━━━━━ (10%) building /Users/chenyulun390/learn/rspack-vue3-jsx/node_modules/.pnpm/@rspack+core@1.0.14/node_modules/@rspack/core/hot/dev-server.js thread '' panicked at 'called Result::unwrap() on an Err value: LayoutError', C:\Users\xyh.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-7a04d2510079875b\rkyv-0.7.37\src\impls\core\mod.rs:265:67 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace Panic occurred at runtime. Please file an issue on GitHub with the backtrace below: https://github.com/web-infra-dev/rspack/issues Message: failed to invoke plugin: failed to invoke plugin on 'Some("/Users/chenyulun390/learn/rspack-vue3-jsx/src/main.jsx")'

Caused by: 0: failed to invoke swc-plugin-transform-vue3-jsx as js transform plugin at swc-plugin-transform-vue3-jsx 1: failed to run Wasm plugin transform. Please ensure the version of swc_core used by the plugin is compatible with the host runtime. See the documentation for compatibility information. If you are an author of the plugin, please update swc_core to the compatible version.

                   Note that if you want to use the os features like filesystem, you need to use `wasi`. Wasm itself does not have concept of filesystem.

                   https://swc.rs/docs/plugin/selecting-swc-core

                   See https://plugins.swc.rs/versions/from-plugin-runner/1.0.0 for the list of the compatible versions.

                   Build info: 
                       Date: 2024-10-22
                       Timestamp: 2024-10-22T06:11:03.042675000Z

                   Version info: 
                       swc_plugin_runner: 1.0.0
                       Dependencies: anyhow 1.0.90,codspeed-criterion-compat 2.7.2,criterion 0.5.1,enumset 1.1.5,futures 0.3.31,once_cell 1.20.2,parking_lot 0.12.3,serde 1.0.210,serde_json 1.0.132,swc_atoms 2.0.0,swc_common 1.0.0,swc_css_ast 1.0.0,swc_css_parser 1.0.0,swc_ecma_ast 1.0.0,swc_ecma_loader 1.0.0,swc_ecma_parser 1.0.1,swc_ecma_visit 1.0.0,swc_malloc 1.0.0,swc_plugin_proxy 1.0.0,testing 1.0.0,tokio 1.40.0,tracing 0.1.40,vergen 9.0.1,virtual-fs 0.16.0,wasmer 4.3.7,wasmer-cache 4.3.7,wasmer-compiler-cranelift 4.3.7,wasmer-wasix 0.27.0

2: RuntimeError: unreachable
       at __rust_start_panic (<module>[3650]:0x26ebcd)
       at rust_panic (<module>[3641]:0x26e8a1)
       at std::panicking::rust_panic_with_hook::h5af4a166307aff48 (<module>[3640]:0x26e81e)
       at std::panicking::begin_panic_handler::{{closure}}::h42a9871ead2de5e8 (<module>[3627]:0x26da84)
       at std::sys_common::backtrace::__rust_end_short_backtrace::h42a6bde96d4a4a1f (<module>[3626]:0x26d9c3)
       at rust_begin_unwind (<module>[3635]:0x26e16e)
       at core::panicking::panic_fmt::h33d7d4c3033d60da (<module>[3741]:0x2755b3)
       at core::result::unwrap_failed::h5d36bed23403009b (<module>[3790]:0x27c9aa)
       at rkyv::impls::core::<impl rkyv::DeserializeUnsized<[U],D> for [T]>::deserialize_unsized::hdbbc5437aae83928 (<module>[180]:0x2afec)
       at rkyv::impls::core::<impl rkyv::DeserializeUnsized<[U],D> for [T]>::deserialize_unsized::h8a52658b03ad48cd (<module>[796]:0xbbc2b)
       at swc_common::plugin::serialized::PluginSerializedBytes::deserialize::h7ddc6846d758ad97 (<module>[332]:0x55773)
       at swc_common::plugin::serialized::deserialize_from_ptr::h6ff5de1a9851235b (<module>[331]:0x55507)
       at __transform_plugin_process_impl (<module>[633]:0x9f4b3)
       at __transform_plugin_process_impl.command_export (<module>[3877]:0x282843)

Location: index.crates.io-6f17d22bba15001f/swc-1.0.0/src/plugin.rs:170

Backtrace omitted.

Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets.

playground-url

https://github.com/rspack-contrib/rspack-examples/tree/main/rspack/vue3-jsx

Expected generated code

修改
`package.json`+`rspack.config.js`

{
  "name": "example-vue3-jsx",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "build": "rspack build",
    "dev": "rspack serve"
  },
  "dependencies": {
    "vue": "3.2.45"
  },
  "devDependencies": {
    "@babel/core": "7.25.8",
    "@rspack/cli": "^1.0.14",
    "@rspack/core": "^1.0.14",
    "@vue/babel-plugin-jsx": "^1.2.5",
    "babel-loader": "9.1.3",
    "swc-plugin-transform-vue3-jsx": "^0.2.1"
  }
}

`rspack.config.js`

```js
const rspack = require('@rspack/core');
/** @type {import('@rspack/cli').Configuration} */
const config = {
  context: __dirname,
  entry: {
    main: './src/main.jsx',
  },
  resolve: {
    // extensions: ['...', '.ts', '.tsx', '.jsx'],
    extensions: ['.jsx', '.js'],
  },
  experiments: {
    css: true,
  },
  module: {
    rules: [
      {
        test: /\.jsx$/,
        use: {
          loader: 'builtin:swc-loader',
          options: {
            jsc: {
              parser: {
                syntax: 'ecmascript',
                jsx: true,
              },
              "experimental": {
                "plugins": [["swc-plugin-transform-vue3-jsx", {}]]
              }
            },
          },
        },
        type: 'javascript/auto',
      },
      // {
      //   test: /\.jsx$/,
      //   use: [
      //     {
      //       loader: 'babel-loader',
      //       options: {
      //         plugins: ['@vue/babel-plugin-jsx'],
      //       },
      //     },
      //   ],
      // },
      {
        test: /\.svg$/,
        type: 'asset',
      },
    ],
  },
  optimization: {
    minimize: false, // Disabling minification because it takes too long on CI
  },
  plugins: [
    new rspack.HtmlRspackPlugin({
      template: './index.html',
    }),
    new rspack.DefinePlugin({
      __VUE_OPTIONS_API__: JSON.stringify(true),
      __VUE_PROD_DEVTOOLS__: JSON.stringify(false),
    }),
  ],
};
module.exports = config;
chenyulun commented 3 weeks ago

https://plugins.swc.rs/versions/range/18 swc_core

@0.106.0 - * change swc_core to 0.106.3, like lingui_macro_plugin

https://github.com/lingui/swc-plugin/blob/main/Cargo.toml

chenyulun commented 3 weeks ago

or https://github.com/Codex-/swc-plugin-import-meta-env/blob/main/Cargo.toml

chenyulun commented 3 weeks ago

maybe 👇🏻?

[package]
authors = ["xxXyh1908 <15626874924@163.com>"]
edition = "2021"
license = "MIT"
name = "swc_plugin_transform_vue3_jsx"
version = "0.3.0"

[lib]
crate-type = ["cdylib", "rlib"]
name = "swc_plugin_transform_vue3_jsx"

[workspace]
members = ["playground", "folder"]

[profile.release]
lto = true
opt-level = 2

[dependencies]
serde = "1"
serde_json = "1"
swc_core = {version = "0.106.4", features = [
  "ecma_plugin_transform",
  "ecma_visit",
  "ecma_ast",
  "common",
]}
vue3_jsx_folder = {path = "./folder"}

# .cargo/config defines few alias to build plugin.
# cargo build-wasi generates wasm-wasi32 binary
# cargo build-wasm32 generates wasm32-unknown-unknown binary.
chenyulun commented 3 weeks ago
error[E0599]: no method named set_span found for mutable reference &mut swc_core::ecma::ast::IdentName in the current scope --> folder/src/visitor_helpers.rs:1437:43 1437 PropName::Ident(name) => name.set_span(_span), ^^^^^^^^
 = help: items from traits can only be used if the trait is implemented and in scope

note: AstSpanAccessor defines an item set_span, perhaps you need to implement it

....

Some errors have detailed explanations: E0061, E0063, E0277, E0308, E0412, E0432, E0560, E0599, E0609. For more information about an error, try rustc --explain E0061. warning: vue3_jsx_folder (lib) generated 6 warnings error: could not compile vue3_jsx_folder (lib) due to 75 previous errors; 6 warnings emitted warning: build failed, waiting for other jobs to finish...

chenyulun commented 3 weeks ago
swc_core = {version = "0.109", features = [
  "ecma_utils",
  "ecma_visit",
  "ecma_ast",
  "ecma_quote",
  "common",
  "base",
]}

很多节点需要添加ctxt: SyntaxContext::empty() 但是我不知道SyntaxContext::empty()对不对

workspace: /Users/myname/github/swc-plugin-transform-vue3-jsx/Cargo.toml Compiling proc-macro2 v1.0.89 Fresh unicode-ident v1.0.13 Fresh version_check v0.9.5 Fresh autocfg v1.4.0 Fresh wasi v0.11.0+wasi-snapshot-preview1 Fresh rand_core v0.6.4 Running /Users/myname/github/swc-plugin-transform-vue3-jsx/target/release/build/proc-macro2-aa7c211a991b9f6f/build-script-build error: failed to run custom build command for proc-macro2 v1.0.89

Caused by: process didn't exit successfully: /Users/myname/github/swc-plugin-transform-vue3-jsx/target/release/build/proc-macro2-aa7c211a991b9f6f/build-script-build (signal: 9, SIGKILL: kill) warning: build failed, waiting for other jobs to finish...

chenyulun commented 3 weeks ago

fix by https://github.com/xxXyh1908/swc-plugin-transform-vue3-jsx/pull/4