无法访问 bundle.js,当 url 是多层深度时
Cannot access bundle.js, when url is multiple layers deep
bundle.js
适用于 localhost:3000/abc
(因为它在 localhost:3000/assets
文件夹中查找),但如果我转到 localhost:3000/abc/xyz
,我会得到一个 404
(因为它在 localhost:3000/abc/assets
文件夹中查找)。
我的webpack.config.js
var webpack = require("webpack");
var path = require('path')
module.exports = {
entry: "./src/index.js",
output: {
path: path.join(__dirname, '/dist'),
filename: "bundle.js",
publicPath: "/assets/",
},
devServer: {
inline: true,
contentBase: "./dist",
port: 3000,
historyApiFallback: true,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: "babel-loader",
query: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
},
{
type: 'javascript/auto',
test: /\.html/,
exclude: /(node_modules)/,
},
{
test: /\.css$/,
use:['style-loader','css-loader']
},
{
test: /\.scss$/,
use:['style-loader','css-loader', 'sass-loader']
}
]
}
}
我不能为 publicPath
使用绝对路径。
当我有多层深度时,如何访问 bundle.js
?
在index.html
我有
<script type="text/javascript" src="./assets/bundle.js"></script>
我所要做的就是将其更改为
<script type="text/javascript" src="/assets/bundle.js"></script>
bundle.js
适用于 localhost:3000/abc
(因为它在 localhost:3000/assets
文件夹中查找),但如果我转到 localhost:3000/abc/xyz
,我会得到一个 404
(因为它在 localhost:3000/abc/assets
文件夹中查找)。
我的webpack.config.js
var webpack = require("webpack");
var path = require('path')
module.exports = {
entry: "./src/index.js",
output: {
path: path.join(__dirname, '/dist'),
filename: "bundle.js",
publicPath: "/assets/",
},
devServer: {
inline: true,
contentBase: "./dist",
port: 3000,
historyApiFallback: true,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: "babel-loader",
query: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
},
{
type: 'javascript/auto',
test: /\.html/,
exclude: /(node_modules)/,
},
{
test: /\.css$/,
use:['style-loader','css-loader']
},
{
test: /\.scss$/,
use:['style-loader','css-loader', 'sass-loader']
}
]
}
}
我不能为 publicPath
使用绝对路径。
当我有多层深度时,如何访问 bundle.js
?
在index.html
我有
<script type="text/javascript" src="./assets/bundle.js"></script>
我所要做的就是将其更改为
<script type="text/javascript" src="/assets/bundle.js"></script>