swc-project / swc-loader

Moved to https://github.com/swc-project/pkgs
MIT License
394 stars 29 forks source link

There will be an error when enable sourcemap in vue3 app #19

Open withoutmeat opened 3 years ago

withoutmeat commented 3 years ago
thread '<unnamed>' panicked at 'index out of bounds: the len is 0 but the index is 0', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/sourcemap-6.0.1/src/builder.rs:84:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
[1]    57707 abort      npx webpack
withoutmeat commented 3 years ago

webpack.config.js:

const VueLoaderPlugin = require("vue-loader").VueLoaderPlugin;

module.exports = {
  mode: "development",
  entry: "./index.ts",
  devtool: "eval-cheap-module-source-map",
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: { loader: "vue-loader" },
      },
      {
        test: /\.(ts|js)$/,
        use: {
          loader: "swc-loader",
          options: {
            jsc: {
              parser: {
                syntax: "typescript",
                dynamicImport: true,
              },
              target: "es2015",
            },
          },
        },
      },
    ],
  },
  plugins: [new VueLoaderPlugin()],
};

index.ts:

import { createApp } from "vue";
import App from "./app.vue";

createApp(App).mount("#app");

app.vue:

<template>
  <div>app</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  setup() {
    return {}
  }
});
</script>

package.json:

{
  "name": "swc-test",
  "version": "1.0.0",
  "license": "MIT",
  "devDependencies": {
    "@swc/core": "^1.2.54",
    "@vue/compiler-sfc": "^3.0.11",
    "swc-loader": "^0.1.14",
    "vue-loader": "^16.2.0",
    "webpack": "^5.36.1",
    "webpack-cli": "^4.6.0"
  },
  "dependencies": {
    "vue": "^3.0.11"
  }
}

then run npx webpack.

withoutmeat commented 3 years ago

if i change devtool: "eval-cheap-module-source-map" to devtool: "eval-cheap-source-map" in the webpack.config.js, it will be ok.

lephuongbg commented 3 years ago

We use vue 2 with eval-source-map setting and also meet the same error. Changing devtool setting to eval-cheap-source-map works but not desirable.

Jack-Works commented 3 years ago

Having this problem too in a react app. use eval-cheap-source-map solves the problem.

FuJuntao commented 3 years ago

The latest @swc/core works.

Edge00 commented 3 years ago

My vue3 app get no sourcemap with following config while babel is fine:

use: {
  loader: "swc-loader",
  options: {
    sourceMap: true,
    jsc: {
      parser: {
        syntax: "typescript",
        dynamicImport: true,
      },
      target: "es2015",
    },
  },
},

image

FuJuntao commented 3 years ago

My vue3 app get no sourcemap with following config while babel is fine:

use: {
  loader: "swc-loader",
  options: {
    sourceMap: true,
    jsc: {
      parser: {
        syntax: "typescript",
        dynamicImport: true,
      },
      target: "es2015",
    },
  },
},

image

It's "sourceMaps" not "sourceMap"

Edge00 commented 3 years ago

@FuJuntao I've tried sourceMaps and got same result. more info here:

"@swc/core": "1.2.103",
"@swc/helpers": "0.2.13",
"swc-loader": "0.1.15",
devtool: 'cheap-module-source-map',