且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Webpack在css和html中的相对路径错误

更新时间:2022-01-23 10:21:24

最后,我找到了解决方案.为MiniCssExtractPlugin.loader添加 publicPath 选项可以解决该问题.

Finally, I found the solution. Add the publicPath option for the MiniCssExtractPlugin.loader solve the problem.

{
    test: /\.sass$/,
    use: [
        {
            loader: MiniCssExtractPlugin.loader,
            options: {
                publicPath: '../',
            },
        },
        'css-loader',
        'sass-loader',
    ],
}

尽管问题已解决,但我对此解决方案并不满意,因为如果更改了文件夹结构,则需要相应地更新publicPath.

Although the problem is solved, I'm not 100% satisfied with this solution as the publicPath need to be updated accordingly if the folder structor is changed.

如果有一些不错的解决方案,根据文件夹结构自动解析相对路径,那就更好了.

It will be better if there's some nice solution to resolve the relative path according to the folder structure automatically.