Webpack文件加载器调整公共路径



我的images文件夹如下:

module.exports = {
        test: /.(png|jpe?g|gif|svg)$/,
        include: config.paths.images, // this is: path.resolve(__dirname, '../assets/images')
        loader: 'file-loader',
        options: {
            name: '[path][name].[ext]',
            publicPath: '../',
        }
    };

这是css的输出:background-image: url(../images/assets/images/doctor-results/g-map-location.png);

我需要的是路径为:background-image: url(../images/doctor-results/g-map-location.png);

基本上从路径中删除前两个目录,同时保持相对。

我最终使用splitsplice而不是添加另一个库。

{    
    options: {
        name: config.outputs.image.filename,
        publicPath: (url) => {
            const pathArray = url.split('/');
            pathArray.splice(0, 2);
            const path = pathArray.join('/');
            return `../${path}`;
        },
        emitFile: false
    }
}

最新更新