主js文件未导入创建的分块文件

时间:2019-07-18 14:54:12

标签: reactjs webpack webpack-splitchunks splitchunksplugin

我遇到了一个问题。创建了webpack中定义的所有入口js文件,对于分块的供应商文件也是如此。在webpack中,我将优化部分定义为:

optimization: {
   splitChunks: {
       chunks: "all"
    }
}

在我的dist文件夹中,一切看起来都很好:内容文件在那里,生成了js和分块的供应商文件,但是当我访问站点js文件时,已正确获取了文件,但接下来我的内容都没有,也没有分块的供应商。

Invalid result - blank screen

这是我使用的Webpack配置。没有优化部分,所有内容(img,字体)和js文件都会成功提供。

module.exports = {
  entry: {
    tppauth: "./src/pages/tppauth.js",
    notfound: "./src/pages/notfound.js",
    servererror: "./src/pages/servererror.js",
  },
  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "dist"),
    publicPath: "/dist/",
  },
  devServer: {
    contentBase: "/dist/"
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: { loader: "babel-loader" }
      },
      {
        test: /\.(woff|woff2|eot|ttf|png|gif|svg)?$/,
        use: { loader: "file-loader" }
      },
      {
        test: /\.(less|css)$/,
        use: ["style-loader","css-loader","less-loader"]
      }
    ]
  },
  resolve: {
    extensions: [".js", ".jsx"],
    alias: {
      ['~']: __dirname
    }
  },
  optimization: {
    splitChunks : {
      chunks: 'all'
    }
  },
};

我的js文件,该文件呈现了一些其他已定义的组件

import React from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
import dateFormat from "dateformat";

import "~/Content/Styles/container.less";

class PaymentAuthorization extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      ...this.state,
      request: {}
    };
  }
  render() {
    return (
      <IntlProvider locale="fr" messages={messages["fr"]}>
        <ErrorBoundary>
          <div className="center">{ComponentToRender}</div>
        </ErrorBoundary>
      </IntlProvider>
    );
  }
}
ReactDOM.render(<PaymentAuthorization/>, document.getElementById("app"));

当我尝试对文件进行分块时,我是否缺少js文件中的其他导入? Valid response - without otpimization section

2 个答案:

答案 0 :(得分:0)

尝试删除splitChunks选项以查看其是否有效。

您可以按以下方式查看我的配置

const path = require("path");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const WebpackShellPlugin = require('webpack-shell-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

process.traceDeprecation = true;

module.exports = {
    entry: {
        admin: "./React/js/App/Modules/Admin/admin.js",
        client: "./React/js/App/Modules/Client/client.js",
        login: "./React/js/EntryPoint/Modules/Admin/Account/Login/Login.js",
        password: "./React/js/EntryPoint/Modules/Admin/Account/Password/Password.js",
        manageaccount: "./React/js/EntryPoint/Modules/Admin/Account/Index.js",
        categories: "./React/js/EntryPoint/Modules/Admin/Categories/Categories.js",
        tag: "./React/js/EntryPoint/Modules/Admin/Tag/Tag.js",
        post: "./React/js/EntryPoint/Modules/Admin/Post/Post.js",
        comment: "./React/js/EntryPoint/Modules/Admin/Comment/Comment.js",
        portal: "./React/js/EntryPoint/Modules/Admin/Portal/PortalIndex.js",
        ClientIndex: "./React/js/EntryPoint/Modules/Client/Index/ClientIndex.js",
        adminSettings: "./React/js/EntryPoint/Modules/Admin/Settings/Settings.js",
        socialLinks: "./React/js/EntryPoint/Modules/Shared/SocialLinks.js"
    },
    output: {
        path: path.resolve(__dirname, "wwwroot/dist"),
        filename: "[name].js",
        publicPath: "/dist/"
    },
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                cache: true,
                parallel: true,
                sourceMap: false,
                extractComments: 'all',
                uglifyOptions: {
                    compress: true,
                    output: null
                }
            }),
            new OptimizeCSSAssetsPlugin({
                cssProcessorOptions: {
                    safe: true,
                    discardComments: {
                        removeAll: true,
                    },
                },
            })
        ]
    },
    plugins: [
        new webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /jsx$/),
        new webpack.LoaderOptionsPlugin({
            options: {}
        }),
        new MiniCssExtractPlugin({
            filename: "[name].css",
            chunkFilename: "[id].css"
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            Popper: ['popper.js', 'default']
        }),
        new CompressionPlugin({
            test: /\.(js|css)/
        }),
        new UglifyJsPlugin(),
        new WebpackShellPlugin({
            onBuildStart: ['echo "Starting postcss command"'],
            onBuildEnd: ['postcss --dir wwwroot/dist wwwroot/dist/*.css']
        })
    ],
    resolve: {
        modules: [
            path.resolve('./React/js/App'),
            path.resolve('./React/js/App/Modules/Client'),
            path.resolve('./React/js/App/Modules/Adnmin'),
            path.resolve('./node_modules')
        ]
    },
    module: {
        rules: [{
                test: /\.scss$/,
                use: [
                    'style-loader',
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            minimize: true,
                            sourceMap: true
                        }
                    },
                    {
                        loader: "sass-loader"
                    }
                ]
            },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: ["babel-loader", "eslint-loader"]
            },
            {
                test: /\.(jpe?g|png|gif)$/i,
                loader: "file-loader"
            },
            {
                test: /\.(woff|ttf|otf|eot|woff2|svg)$/i,
                loader: "file-loader"
            }
        ]
    }
};

答案 1 :(得分:0)

好几次休息后,我发现自己做错了。我没有为新的捆绑文件添加<script src='...' />标签。

我使用html-webpack-plugin来获取我的html模板并将所需的javascript文件放入其中,但是它也可以手动完成。 这是我的新cshtml文件,其中的node_modules分为两个单独的文件:

@{
    Layout = "~/Views/Shared/LayoutPage.cshtml";
}<script type="text/javascript" src="/dist/vendor2.js"></script><script type="text/javascript" src="/dist/vendor1.js"></script><script type="text/javascript" src="/dist/tppauth.js"></script>