question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vue loader 17.0.0 + Vue3 + <script setup> + typescript = confused imports/exports error

See original GitHub issue

Version

16.8.3

Reproduction link

github.com

Steps to reproduce

Hi, I encounter the following, very puzzling, build&runtime error :

index.ts

import { createApp } from "vue";
import test from "./test.ce.vue";

console.log("imported");
console.log("imported", test);

export const toto = "toto";

createApp(test).mount(document.body);

test.ce.vue

<script setup lang="ts">
    import { toto } from './index';

    const greeting= 'Hello ' + toto + ' !';
</script>

<template>
  <p class="greeting">{{ greeting }}</p>
</template>

<style>
.greeting {
  color: red;
  font-weight: bold;
}
</style>

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { VueLoaderPlugin } = require("vue-loader");

module.exports = {
  entry: './src/index.ts',
  mode: "production",
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.tsx?$/,
        use: [{loader: 'ts-loader', options: {onlyCompileBundledFiles: true}}],
      },
      {
        test: /\.jsx?$/,
        use: [
          {
            loader: "babel-loader",
          },
        ],
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          "style-loader",
          "css-loader",
          "sass-loader",
        ],
      },
      {
        test: /\.css$/i,
        use: [
          "style-loader",
          "css-loader",
        ],
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: "MAIN UI",
      scriptLoading: "module",
      template: "src/index.ejs",
      filename: "index.html",
    }),
  ],
};

tsconfig.json

{
    "compilerOptions": {
      "outDir": "./dist/",
      "noImplicitAny": true,
      "module": "es2020",
      "target": "es5",
      "jsx": "preserve",
      "allowJs": true,
      "moduleResolution": "node"
    }
  }

What is expected?

No error

What is actually happening?

WARNING in ./src/test.ce.vue?vue&type=script&setup=true&lang=ts 1:0-205
export 'default' (reexported as 'default') was not found in '-!../node_modules/ts-loader/index.js??clonedRuleSet-1.use[0]!../node_modules/vue-loader/dist/index.js??ruleSet[1].rules[8].use[0]!./test.ce.vue?vue&type=script&setup=true&lang=ts' (possible exports: toto)
 @ ./src/test.ce.vue 1:0-69 2:0-64 2:0-64 7:49-55
 @ ./src/index.ts 2:0-33 4:24-28 6:10-14

When building, somehow the loader think that the exports from the vue file are instead the exports from the index.ts file. The same happen in another big project where the .vue export seems to be confused from any random exports in any random .ts file. It’s not only a type warning, the actual imported object at runtime is wrong too!

Maybe there’s a conflit between vue-loader and ts-loader? Maybe my configuration is wrong?


Workaround

If I replace the script with a non-Typescript one:

<script setup>
    import { toto } from './index';

    const greeting= 'Hello ' + toto + ' !';
</script>

It works fine and without error. However, not using Ts is not an option in my context and I’d prefer if we could fix the causes instead of dodging the consequences =)

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
jbruwescommented, May 13, 2022

You probably should use appendTsSuffixTo: [/\.vue$/] in the ts-loader to avoid the problem.

{
 test: /\.tsx?$/,
 loader: "ts-loader",
 options: {
  appendTsSuffixTo: [/\.vue$/]
 },
 exclude: /node_modules/,
},
{
 test: /\.vue$/i,
 loader: "vue-loader",
 options: {
  esModule: true,
 },
},
0reactions
mjbradvicacommented, May 13, 2022

Um, no this is a bug. The @vue/cli-plugin-babel broke this going from V4.5 to ~V5.0 for re-exporting components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does typescript (or is it webpack) not find my vue view?
I have basic webpack configuration. It has as entry point a typescript file. Output is a folder build. I am using the typescript...
Read more >
<script setup> | Vue.js
Ability to declare props and emitted events using pure TypeScript; Better runtime performance (the template is compiled into a render function in the...
Read more >
vue-loader | Yarn - Package Manager
vue -loader parses the SFC source code into an SFC Descriptor using @vue/component-compiler-utils . · We want the content in script block to...
Read more >
06 April 2020 - Webpack for Vue 3 - Lachlan Miller
In this article we build a webpack configuration from scratch for developing Vue.js 3 apps. Specifically, we will support .vue files, ...
Read more >
Vue 3 Composition API: Ref vs Reactive - Dan Vega
The biggest feature coming to Vue 3 is the Composition API. ... </template> <script> import { ref } from "vue"; export default {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found