webpack保持重建,而没有使用dotnet core 2.0更新源代码React代码

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (24)

我的dotnet core 2.0和webpack的反应项目存在很大问题。我在终端中使用dotnet run命令并显示一些这样的信息。在Chrome控制台中,一些信息会继续生成,就像下图所示。此信息由反应node_module目录中的webpack模块生成,有人可以指出我如何解决此问题?谢谢!

以下是我可以提供的一些信息: 终端信息

chrome控制台输出

package.json文件:

{   "name": "dotnetcore",   "private": true,   "version": "0.0.0",   "homepage": "/app/canteen/employee",   "devDependencies": {
    "@types/history": "4.6.0",
    "@types/react": "15.0.35",
    "@types/react-dom": "15.5.1",
    "@types/react-hot-loader": "3.0.3",
    "@types/react-router": "4.0.12",
    "@types/react-router-dom": "4.0.5",
    "@types/webpack-env": "1.13.0",
    "aspnet-webpack": "^2.0.1",
    "aspnet-webpack-react": "^3.0.0",
    "awesome-typescript-loader": "3.2.1",
    "bootstrap": "3.3.7",
    "css-loader": "0.28.4",
    "event-source-polyfill": "0.0.9",
    "extract-text-webpack-plugin": "2.1.2",
    "file-loader": "0.11.2",
    "isomorphic-fetch": "2.2.1",
    "jquery": "3.2.1",
    "json-loader": "0.5.4",
    "react": "15.6.1",
    "react-dom": "15.6.1",
    "react-hot-loader": "3.0.0-beta.7",
    "react-router-dom": "4.1.1",
    "style-loader": "0.18.2",
    "typescript": "2.4.1",
    "url-loader": "0.5.9",
    "webpack": "2.5.1",
    "webpack-hot-middleware": "2.18.2",
    "babel-core": "^6.3.26",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "moment": "^2.18.1",
    "react-datetime": "^2.10.1",
    "react-mobile-datepicker": "^3.0.6",
    "react-mobile-picker": "^0.1.10",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-scripts": "1.0.13",
    "react-time": "^4.3.0",
    "redux": "^3.7.2"   } }

webpack.config.js文件

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const bundleOutputDir = './wwwroot/dist';

module.exports = (env) => {
    const isDevBuild = !(env && env.prod);
    return [{
        stats: { modules: false },
        // entry: { 'main': './ClientApp/index.js' },
        entry: { 'main': './ClientApp/boot.tsx' },
        resolve: { extensions: ['.js', '.jsx', '.ts', '.tsx'] },
        output: {
            path: path.join(__dirname, bundleOutputDir),
            filename: '[name].js',
            publicPath: 'dist/'
        },
        module: {
            rules: [
                { test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' },
                { test: /\.css$/, use: isDevBuild ? ['style-loader', 'css-loader'] : ExtractTextPlugin.extract({ use: 'css-loader?minimize' }) },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' },

                { test: /\.js?$/, loader: 'babel-loader', exclude: /node_modules/,
                    query:
                    {
                        presets:['react','es2015']
                    } 
                },
            ]
        },
        plugins: [
            new CheckerPlugin(),
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(bundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin()
            // new webpack.optimize.UglifyJsPlugin(),
            // new ExtractTextPlugin('site.css')
        ])
    }];
};
提问于
用户回答回答于

您可以在发现Webpack进入此热模块替换(HMR)循环时应用此解决方案,而不是每次都修改Webpack以清除所有文件。我注意到,当您重命名文件时会发生这种情况,导致HMR机制混淆。您可以在此处应用我的答案中的解决方案。

用户回答回答于

我有同样的问题和几乎相同的webpack设置(我们都使用dotnet核心启动模板)。

我无法解释导致不断重新加载的原因,但我能够通过在每次运行之前或之后“清理”我的构建文件来解决它dotnet run。它似乎dotnet run在它触发时运行一次基本的webpack命令。这意味着您的前端文件会再次构建(在ClientApp / dist和wwwroot / dist中)。如果在运行之前删除所有这些文件dotnet run,则问题就会消失。(警告:不要删除vendor.*文件,因为主webpack配置文件不会重建这些文件。你需要webpack.config.vendor.js脚本,并且在运行时不会自动运行dotnet run)。

您可以使用clean-webpack-plugin自动执行此删除/清除过程。这是我的webpack.config.js使用该插件的样子片段:

const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = (env) => {
    const clientBundleOutputDir = './wwwroot/dist';
    const serverBundleOutputDir = './ClientApp/dist';
    const cleanOptions = {
        options:  {
            exclude: ['vendor.js', 'vendor.css', 'vendor-manifest.json'],
            verbose:  true,
        },
        pathsToClean: [clientBundleOutputDir, serverBundleOutputDir],
    };

    //.....

    const clientBundleConfig = {
        // ... some webpack settings ...

        plugins: [
            //.... array of some plugins....,
            new CleanWebpackPlugin(cleanOptions.pathsToClean, cleanOptions.options),
        ],

        // ... some more webpack settings ...
    }

    // ....
    return [clientBundleConfig];
}

使用这种方法的一个警告 - 我发现使用这种技术会受到影响,因为它dotnet run会运行webpack脚本(删除文件并重建它们)并激活茶隼服务器和浏览器来运行你的应用程序。在Webpack完成重建文件之前,Kestrel会启动并尝试加载页面,因此您在浏览器中看到的第一个页面会抱怨它无法找到您应用的JS文件。因此,您必须等待几秒钟才能完成webpack,然后重新加载页面。它并不理想,但至少不断重新加载已经消失。

扫码关注云+社区

领取腾讯云代金券