【发布时间】:2019-04-06 05:47:06
【问题描述】:
在我们的 Web 应用程序中,我们有一些 JSON 文件,每个文件大约 10-80k 行。这些都包含在我们的主包中。这些由名为 react-lottie 的动画插件使用。
我们webpack.config.js的一个例子
module.exports = {
entry: ["./src/index.js"],
module: {
rules: [
{ test: /\.(js|jsx)$/, exclude: /node_modules/, use: ["babel-loader"] },
{
test: /\.(jpg|png|gif|ico)$/,
use: {
loader: "file-loader",
options: { name: "[path][name].[hash].[ext]" }
}
}
]
},
resolve: { extensions: ["*", ".js", ".jsx"] },
output: {
path: __dirname + "/dist",
publicPath: "/",
filename: "[name].[hash].js"
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({ hash: false, template: "src/index.html" }),
new DashboardPlugin(),
new CopyWebpackPlugin([
{
from: "src/components/Assets/BookingBar.js",
to: "assets/BookingBar.js"
}
]),
new BundleAnalyzerPlugin()
],
devServer: {
contentBase: "./dist",
hot: true,
historyApiFallback: true,
port: 4000
}
};
预期的行为是什么?
应该有一种方法可以从主包中排除 .json 文件。我试过 File-Loader、json-loader 和 const someJson = require(./someJson)
其他相关信息: 网络包版本:4.16.1 Node.js 版本:10.12.0
操作系统:Mac OS 10.14 Mojave
在下面回答(至少我是如何解决的)。如果没有任何数据,我无法初始化 lottie。
【问题讨论】:
-
尝试阅读延迟加载,也称为代码拆分,因此最终 JSON 将作为一个块加载,而不是在主包中。
-
You can leverage Webpack's
import将 JSON 从您的主包中拆分出来。
标签: json reactjs webpack react-router