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

时间:2018-11-15 04:10:49

标签: 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)不会重新加载并传输更改后的接口文件。

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.ts index.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 --config webpack.dev.config.js --hot运行webpack-dev-server。

这是我的配置文件。

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"
});

1 个答案:

答案 0 :(得分:2)

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

显然,ts-loader仅针对提供JavaScript输出的文件和打字稿定义文件生成输出。然后,Webpack监视程序将读取输出,如果这些文件之一发生更改,则Webpack会更新。

在您的情况下,您有不生成JavaScript输出且不是打字稿定义文件的文件。因此,它们不会产生任何输出,并且webpack监视程序不会在它们更改时注意到。