当url是多层深层时,无法访问bundle.js

问题描述 投票:0回答:1

bundle.jslocalhost: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

reactjs webpack webpack-4
1个回答
0
投票

index.html我有

<script type="text/javascript" src="./assets/bundle.js"></script>

我所要做的就是改变它

<script type="text/javascript" src="/assets/bundle.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.