更改接口后,webpack-dev-server和ts-loader无法重新加载

kur*_*441 9 typescript webpack webpack-dev-server ts-loader

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

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

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

接口/IHelloState.ts

export interface IHelloState {
    message: string;
}
Run Code Online (Sandbox Code Playgroud)

Interface.index.ts

export {IHelloState} from "./IHelloState";
Run Code Online (Sandbox Code Playgroud)

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"));
Run Code Online (Sandbox Code Playgroud)

当我像这样更改Interface / IHelloState.ts时

接口/IHelloState.ts

export interface IHelloState {
    // message: string;
}
Run Code Online (Sandbox Code Playgroud)

没发生什么事。甚至没有“ [HMR]正在检查服务器上的更新...”或“ [HMR]没有最新更新。” 节目。

当我更改Interface / IHelloState.tsindex.tsx时,如下所示:

接口/IHelloState.ts

export interface IHelloState {
    message: string;
    state: boolean;
}
Run Code Online (Sandbox Code Playgroud)

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
};
Run Code Online (Sandbox Code Playgroud)

现在和错误报告。

[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'.
Run Code Online (Sandbox Code Playgroud)

我应该改变什么?

我使用运行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"
});
Run Code Online (Sandbox Code Playgroud)

小智 7

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

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

在您的情况下,您的文件不生成 JavaScript 输出并且不是打字稿定义文件。因此不会从它们生成任何输出,并且 webpack 观察者不会注意到它们何时发生变化。