簡體   English   中英

使用 typescript 和 webpack 熱重載

[英]Hot reload with typescript and webpack

我在 Typescript 中開發前端 (Redux) 和后端 (Express) 時遇到了問題。 我無法使熱重載工作。 這是根文件夾中webpack.config.js配置:

const webpack = require('webpack');
const path = require('path');
const { CheckerPlugin } = require('awesome-typescript-loader');

const config = {
    cache: true,
    mode: 'development',
    entry: {
        'user': ['./dist/client/User/index', 'webpack-hot-middleware/client'],
        'guest': ['./dist/client/Guest/index', 'webpack-hot-middleware/client']
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new CheckerPlugin()
    ],
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/static'
    },
    devServer: {
        contentBase: './dist',
        hot: true
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js', '.jsx']
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'awesome-typescript-loader',
                exclude: /node_modules/,
                include: path.join(__dirname, 'client'),
                options: { cacheDirectory: true }
            }
        ]
    },
    node: { fs: 'empty' }
};

module.exports = config;

tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "jsx": "react",
        "esModuleInterop": true,
        "outDir": "dist",
        "sourceMap": true,
        "baseUrl": "."
    },
    "include": ["src/**/*.ts", "src/**/*.tsx"],
    "exclude": ["node_modules"]
}

src我將兩個文件夾clientserver分開,我的npm start腳本是tsc && node dist/server server/index我像這樣聲明了 webpack 編譯器的使用:

import config from '../../webpack.config';
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
    noInfo: true,
    publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));

當我啟動應用程序時,webpack 正常構建,但當我在客戶端文件中進行更改時,它永遠不會構建熱重載。 我花了一整天,但仍然困惑,不知道如何解決它。

你目前在使用 React 17 嗎?

如果是這樣,請在 .env 文件中創建以下內容

FAST_REFRESH=false

目錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM