PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] npm run watch/hot only successful on the first run

 January 21, 2022     hot-module-replacement, laravel, laravel-mix, typescript, vuejs2     No comments   

Issue

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.


Solution

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

  • https://github.com/TypeStrong/ts-loader#hot-module-replacement

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.



Answered By - Darkproduct
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing