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>