npm run watch/hot only successful on the first run
Asked Answered
T

3

3

Background:

I added TypeScript support to my existing project, so I added ts-loader and typescript. I think, I configured everything right and it is working fine in dev and prod mode.

I would like to update gradually, keeping all the JavaScript code in place and using TypeScript for everything new or where there is a need for refactoring. So it may be important to note that TableValue.vue is an old js component.

Problem:

Edit: It also occurs with npm run watch

When I run npm run hot in package.json: "scripts": { ..., "hot": "mix watch --hot", ...} it only works on the first try. As soon as I change any file and trigger a recompile, I get:

√ Mix: Compiled successfully in 19.15s
webpack compiled successfully

// Here the recompile is triggered
i Compiling Mix
√ Mix: Compiled with some errors in 509.01ms
ERROR in C:\fakepath\resources\js\components\test\component.vue.ts
24:23-41
[tsl] ERROR in C:\fakepath\resources\js\components\test\component.vue.ts(24,24)
      TS2307: Cannot find module './TableValue.vue' or its corresponding type declarations.

webpack compiled with 1 error

I suspect that this error comes from ts-loader, but why is everything working on the first try?

I could just ignore this error, but then hot module replacement is unusable, because I have to manually trigger a new build process every time anyway.

  • Has someone got such an setup working?
  • What can I do to solve this error?

Infos:

I'm working with:

  • Laravel 8.58
  • Laravel Mix 6.0.25
  • Vue 2.6.14
  • ts-loader 9.2.5
  • typescript 4.4.2

Here the script tag from the test component:

<script lang="ts">
import Vue, { PropType } from 'vue';

import TableValue from "./TableValue.vue";
import Model from "@/js/types/model.js";

export default Vue.extend({
  name: "TestComponent",
  components: {
    TableValue
  },
  props: {
    'model': {
      type: Object as PropType<Model>,
      required: true
    }
  },
  data() {
    return {};
  },
});
</script>

Project Structure:

app/
bootstrap/
config/
database/
node_modules/
public/
resources/
  js/
    components/
    store/
    types/
    views/
    app.js
    bootstrap.js
    routes.js
    shims-vue.d.ts
  lang/
  sass/
  views/
routes/
storage/
tests/
vendor/
composer.json
composer.lock
tsconfig.json
package-lock.json
package.json
phpunit.xml
vs.code-workspace
webpack.mix.js

webpack.mix.js:

const mix = require('laravel-mix');
const ResolveTypeScriptPlugin = require("resolve-typescript-plugin").default;

mix.webpackConfig({
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: "ts-loader",
                options: { appendTsSuffixTo: [/\.vue$/] },
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.ts', '.vue'],
        alias: {
            '@': __dirname + '/resources'
        },
        fullySpecified: false,
        plugins: [new ResolveTypeScriptPlugin()]
    },
    devtool: 'source-map'
}).sourceMaps();

mix.ts('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.sass', 'public/css').sourceMaps()
    .vue();

mix.extract();

tsconfig.json:

{
    "compilerOptions": {
        "target": "esnext",
        "module": "esnext",
        "strict": true,
        "noImplicitAny": false,
        "importHelpers": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "allowJs": true,
        "checkJs": false,
        "sourceMap": true,
        "baseUrl": ".",
        "paths": {
            "@/*": [
                "resources/*"
            ]
        },
        "lib": [
            "esnext",
            "dom",
            "dom.iterable",
            "scripthost"
        ]
    },
    "files": [
        "resources/js/shims-vue.d.ts"
    ],
    "include": [
        "resources/js/**/*.ts",
        "resources/js/**/*.vue",
    ],
    "exclude": [
        "node_modules",
        ".vscode",
        "app",
        "bootstrap",
        "config",
        "database",
        "public",
        "routes",
        "storage",
        "tests",
        "vendor"
    ]
}

Update:

When I remove shims-vue.d.ts, I get the error immediately.

declare module "*.vue" {
    import Vue from "vue";
    export default Vue;
}

It looks like this file is only read/applyed once and not after? Not sure.

Tacnode answered 9/9, 2021 at 15:17 Comment(2)
Is this just with hot or is it also with watch?Guanidine
The problem is also there with watch. It works on the first try, but as soon as I change any file, I get the same error.Tacnode
T
2

It looks like ts-loader doesn't support HMR yet.

I installed fork-ts-checker-webpack-plugin and updated webpack.mix.js to:

const mix = require('laravel-mix');
const path = require('path');

const ResolveTypeScriptPlugin = require("resolve-typescript-plugin").default;
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

mix.webpackConfig({
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: "ts-loader",
                options: {
                    appendTsSuffixTo: [/\.vue$/],
                    transpileOnly: true
                },
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.ts', '.tsx', '.vue'],
        alias: {
            '@': path.resolve(__dirname + '/resources'),
            '@store': path.resolve(__dirname + '/resources/js/store'),
            '@components': path.resolve(__dirname + '/resources/js/components')
        },
        fullySpecified: false,
        plugins: [new ResolveTypeScriptPlugin()]
    },
    plugins: [new ForkTsCheckerWebpackPlugin()],
    devtool: 'source-map'
}).sourceMaps();

mix.ts('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.sass', 'public/css').sourceMaps()
    .vue();

mix.extract();

Now everything is working fine but I'm still not sure why watch was also affected and where exactly the problem was.

Tacnode answered 10/9, 2021 at 12:48 Comment(1)
For me issue hasn't resolved even after configuring ForkTsCheckerWebpackPlugin my problem is that in my .d.ts file I'm declaring global variable like this declare const DOTNET_API_ENDPOINT: string; typescript doesn't give any error on first run, however if I trigger hot refresh by changing something in my component, it would the show TS2304: Cannot find name 'DOTNET_API_ENDPOINT'. on update.Longcloth
S
2

I'm not using Typescript, but the same thing was happening to me, when i ran npm run watch/hot where only successful on the first change of the code, then, you can not see the changes until you run npm run watch/hot or npm run dev again. The strange thing was that everything was compiling successfully on every change I made.

I manage to debug it with git, and found out that I was importing a component with a wrong name but did not get an error on the console. My component name was WhosApplying.vue

I got:

import WhosApplying from "@/whosApplying.vue"

And change it for:

import WhosApplying from "@/WhosApplying.vue";

That mistake in the w instead of W make me lose hours. 😅

Saturated answered 30/9, 2021 at 21:17 Comment(0)
I
2

I ran into this using laravel-mix after adding typescript to an Inertia / Vue 3 project.

Using Volar (Not Vuter)

To fix it I changed my webpack.config.js file from:

const path = require('path');

module.exports = {
    resolve: {
        alias: {
            '@': path.resolve('resources/js'),
        },
    },
};

to:

const path = require('path');

module.exports = {
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: "ts-loader",
                options: {
                    appendTsSuffixTo: [/\.vue$/],
                    transpileOnly: true
                },
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.ts', '.tsx', '.vue'],
        alias: {
            '@': path.resolve(__dirname + '/resources/js'),
        },
    },
};
Igal answered 19/11, 2021 at 1:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.