Webpack,新块正在以错误的路径加载 [英] Webpack, new chunk is loading in with wrong path
问题描述
我正在尝试分块我的应用程序 - 尝试遵循 webpacks 指南(https://webpack.github.io/docs/code-splitting.html).所以我为我的应用程序设置了一个单独的块,我可以看到 webpack 在我的构建文件夹中生成 1.bundle.js
,但是它正在将它粘贴到我的 index.html
的路径不正确,在我的控制台中,我看到 1.bundle.js
文件的提取错误.
I am trying to chunk my app - attempting to follow webpacks guide on how-to (https://webpack.github.io/docs/code-splitting.html). So I have a seperate chunk set up for my app, I can see that webpack is generating 1.bundle.js
in my build folder, however it is pasting it onto my index.html
with an incorrect path, and in my console I see the fetch error for the 1.bundle.js
file.
所以我的 webpack 配置看起来像这样(我现在只使用 webpack:dev):
So my webpack config looks like so (im just using the webpack:dev for now):
return {
dev: {
entry: {
index: './client/app.jsx'
},
output: {
path: path.join(__dirname, '..', '..', 'dist', 'client'),
publicPath: "/dist/client/",
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}, {
test: /\.json$/,
loader: 'json-loader'
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
resolveLoader: {
fallback: [path.join(__dirname, 'node_modules')]
},
plugins: [
new webpack.DefinePlugin({
"process.env": {
"NODE_ENV": JSON.stringify("dev")
}
})
]
},
在我的 index.html 中,我手动添加了 <script src="bundle.js"></script>
并且效果很好.看起来现在构建时,webpack 正在像这样在我的索引上应用自己的脚本标签
and in my index.html I manually add <script src="bundle.js"></script>
and that has been working great. It looks like when this builds now, webpack is applying its own script tag on my index like so
<script type="text/javascript" charset="utf-8" async="" src="/dist/client/1.bundle.js"></script>
然而这个路径是不正确的,它应该只是src="1.bundle.js"
.我尝试调整路径和 publicPath,但似乎没有任何效果.有没有办法让 webpack 添加正确的路径?谢谢!
However this path is incorrect, it should just be src="1.bundle.js"
. I have tried tweaking the path and publicPath but nothing seems to work. Is there a way to have webpack add the correct path? Thanks!
推荐答案
您应该更改此代码段的 publicPath
:
You should change publicPath
for this snippet:
publicPath: "/"
它将始终从根路径为您的块提供服务.
It will always serve your chunks from root path.
这篇关于Webpack,新块正在以错误的路径加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!