简体   繁体   English

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

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

I'm using webpack@4.16.1 , webpack-dev-server@3.1.4 and ts-loader@4.4.2 .我正在使用webpack@4.16.1webpack-dev-server@3.1.4ts-loader@4.4.2

I use Interface/index.ts to manage imports, to organize multiple interface imports.我使用Interface/index.ts来管理导入,组织多个界面导入。

But When I change interface file, webpack-dev-server(or ts-loader, I don`t know) not reload & transpile changed interface file.但是当我更改接口文件时,webpack-dev-server(或 ts-loader,我不知道)不会重新加载和转换已更改的接口文件。

Interface/IHelloState.ts接口/IHelloState.ts

export interface IHelloState {
    message: string;
}

Interface.index.ts接口.index.ts

export {IHelloState} from "./IHelloState";

index.tsx索引.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"));

When I change Interface/IHelloState.ts like:当我更改Interface/IHelloState.ts 时

Interface/IHelloState.ts接口/IHelloState.ts

export interface IHelloState {
    // message: string;
}

Nothing happens.没发生什么事。 Not even "[HMR] Checking for updates on the server..." or "[HMR] Nothing hot updated."甚至没有“[HMR] 检查服务器上的更新...”或“[HMR] 没有热更新。” shows.显示。

When I change Interface/IHelloState.ts and index.tsx like:当我更改Interface/IHelloState.tsindex.tsx 时

Interface/IHelloState.ts接口/IHelloState.ts

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

index.tsx索引.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
};

Now and error reports.现在和错误报告。

[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'.

What should I change?我应该改变什么?

I run webpack-dev-server with webpack-dev-server --config webpack.dev.config.js --hot .我使用webpack-dev-server --config webpack.dev.config.js --hot运行 webpack-dev-server 。

This is my config file.这是我的配置文件。

webpack.dev.config.js 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"
});

I encountered a similar issue.我遇到了类似的问题。 What solved it for me was to change the filenames of files that contain only interfaces from *.ts to *.d.ts.为我解决的是将仅包含接口的文件的文件名从 *.ts 更改为 *.d.ts。

Apparently, the ts-loader generates output only for files that give JavaScript output, and typescript definition files.显然,ts-loader 只为提供 JavaScript 输出的文件和打字稿定义文件生成输出。 The output is then read by the webpack watcher, and webpack updates if one of these files changes.然后 webpack watcher 读取输出,如果这些文件之一发生变化,webpack 就会更新。

In your case, you have files that generate no JavaScript output and are not typescript definition files.在您的情况下,您的文件不生成 JavaScript 输出并且不是打字稿定义文件。 So no output will be generated from them, and the webpack watcher won't notice when they change.因此不会从它们生成任何输出,并且 webpack 观察者不会注意到它们何时发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM