laraveltypescriptvuejs2laravel-mixhot-module-replacement

npm run watch/hot only successful on the first run


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.


Infos:

I'm working with:

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.


Solution

  • 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.