【问题标题】:Hot reload with typescript and webpack使用 typescript 和 webpack 热重载
【发布时间】:2020-12-18 19:37:49
【问题描述】:

我在使用 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 中,我将client 和server 两个文件夹分开,我的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 可以正常构建,但是当我对客户端文件进行更改时,它从不构建热重载。我花了一整天,但仍然很困惑,不知道如何解决它。

【问题讨论】:

    标签: javascript node.js reactjs typescript webpack


    【解决方案1】:

    你目前正在使用 react 17 吗?

    如果是这样,请在 .env 文件中创建以下内容

    FAST_REFRESH=false
    

    directory

    【讨论】:

    • 反应 13 ??对于使用 typescript 和自己的 webpack 配置的用户来说,使用如此过时的 react 版本几乎是不可能的。
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 2021-02-01
    • 2019-10-11
    • 1970-01-01
    • 2015-11-17
    • 2019-10-29
    • 1970-01-01
    • 2017-07-07
    相关资源
    最近更新 更多