webpack-dev-server 和 ts-loader 不会在界面更改时重新加载

webpack-dev-server & ts-loader not reloads when interface changed

我正在使用 webpack@4.16.1webpack-dev-server@3.1.4ts-loader@4.4.2

我使用Interface/index.ts来管理导入,组织多个接口导入。

但是当我更改接口文件时,webpack-dev-server(或 ts-loader,我不知道)不会重新加载和转译更改的接口文件。

Interface/IHelloState.ts

export interface IHelloState {
    message: string;
}

Interface.index.ts

export {IHelloState} from "./IHelloState";

index.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";
import "./index.css";
import {IHelloState} from "./Interface";

const helloState: IHelloState = {
    message: "hello!"
};

ReactDOM.render(<div>{helloState.message}</div>, document.getElementById("root"));

当我将 Interface/IHelloState.ts 更改为:

Interface/IHelloState.ts

export interface IHelloState {
    // message: string;
}

没有任何反应。甚至“[HMR] 正在检查服务器上的更新...”或“[HMR] 没有任何热更新。”显示。

当我将 Interface/IHelloState.tsindex.tsx 更改为:

Interface/IHelloState.ts

export interface IHelloState {
    message: string;
    state: boolean;
}

index.tsx

import * as React from "react";
import * as ReactDOM from "react-dom";
import "./index.css";
import {IHelloState} from "./Interface";

const helloState: IHelloState = {
    message: "hello!",
    state: true
};

现在和错误报告。

[tsl] ERROR in (PATH...)\index.tsx(8,5)
      TS2322: Type '{ message: string; state: boolean; }' is not assignable to type 'IHelloState'.
  Object literal may only specify known properties, and 'state' does not exist in type 'IHelloState'.

我应该改变什么?

我 运行 webpack-dev-server 与 webpack-dev-server --config webpack.dev.config.js --hot.

这是我的配置文件。

webpack.dev.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");

module.exports = Object.assign(require("./webpack.base.config"), {
    entry: [
        path.join(__dirname, "src/app/index.tsx"),

        "webpack/hot/only-dev-server"
    ],

    output: {
        path: path.join(__dirname, "build/app"),
        filename: "static/js/bundle.[hash].js"
    },

    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: "babel-loader"
            }, {
                test: /\.tsx?$/,
                loader: "ts-loader",
                options: {
                    configFile: "tsconfig.app.json"
                }
            },
            {
                test: /\.css$/,
                use: [
                    "style-loader",
                    {
                        loader: "css-loader",
                        options: {
                            modules: false
                        }
                    }
                ]
            },
            {
                exclude: [/\.tsx?/, /\.jsx?$/, /\.html$/, /\.css$/, /\.json$/],
                loader: "file-loader",
                options: {
                    name: "static/files/[hash].[ext]"
                }
            }
        ]
    },

    resolve: {
        extensions: [".ts", ".tsx", ".js", ".jsx", ".css", ".json"]
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: path.join(__dirname, "src/app/index.html")
        }),

        new webpack.HotModuleReplacementPlugin()
    ],

    devServer: {
        historyApiFallback: {
            index: "/"
        }
    },

    target: "electron-renderer",

    mode: "development"
});

我遇到了类似的问题。为我解决的是将仅包含接口的文件的文件名从 *.ts 更改为 *.d.ts.

显然,ts-loader 只为提供 JavaScript 输出的文件和打字稿定义文件生成输出。然后 webpack 观察程序读取输出,如果这些文件之一发生更改,webpack 会更新。

在您的情况下,您的文件不生成 JavaScript 输出并且不是打字稿定义文件。所以它们不会产生任何输出,webpack watcher 也不会注意到它们的变化。