【问题标题】:Webpack & SCSS: how to work with image paths?Webpack 和 SCSS:如何使用图像路径?
【发布时间】:2018-07-06 15:04:18
【问题描述】:

我正在尝试建立一个基本的 webpack 项目。一切都很顺利,除了生成的 CSS 中的生成的图像路径。

文件夹结构:

src/
    assets/
        images/
    js/
    scss/
dist/              
    assets/           <--- generated correctly, incl. images
        images/
    bundle.js
    main.bundle.css   <--- includes "wrong" paths, starting with dist/
index.htm
webpack.config.js

webpack.config.js

var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    watch: true,
    entry: ['./src/js/main.js', './src/scss/main.scss'],
    output: {
        filename: 'dist/bundle.js'
    },
    module: {
        rules: [
            { // regular css files
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({
                    use: 'css-loader?importLoaders=1',
                }),
            },
            { // sass / scss loader for webpack
                test: /\.(sass|scss)$/,
                use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
            },
            { // images
            test: /\.(png|jpg|gif)$/,
            use: [
                {
                    loader: 'file-loader',
                    options: {
                        name: '[path][name].[ext]',
                        context: path.resolve(__dirname, "src/"),
                        outputPath: 'dist/'
                    }
                }
            ] 
        },
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename: 'dist/[name].bundle.css',
            allChunks: true
        })
    ]
};

src/scss/main.scss

@import "./../../node_modules/bootstrap/scss/bootstrap";

body {
    background-image: url('../assets/images/bg.jpg');
}

dist/main.bundle.css

body {
    background-image:url(dist/assets/images/bg.jpg)
}

index.htm

<link rel="stylesheet" href="dist/main.bundle.css">

问题:

dist/main.bundles.css 已经位于dist/,但在图像路径前加上dist/。我这边一定有配置问题。

有什么想法吗?提前致谢!

【问题讨论】:

    标签: css webpack sass


    【解决方案1】:

    通过添加publicPath: '../' (docs) 和useRelativePaths: true (docs) 解决了问题:

    module.exports = {
        // ...        
        module: {
            rules: [                 
                { 
                    test: /\.(png|jpg|gif)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[path][name].[ext]',
                                context: path.resolve(__dirname, "src/"),
                                outputPath: 'dist/',
                                publicPath: '../',
                                useRelativePaths: true
                            }
                        }
                    ] 
                }
            ]
        },
        plugins: [
            new ExtractTextPlugin({
                filename: 'dist/[name].bundle.css',
                allChunks: true
            })
        ]
    };
    

    【讨论】:

      【解决方案2】:

      老兄,你拯救了我的一天 useRelativePaths 甚至在 webpack 文档中都没有提到......而且,即使知道它,它仍然太混乱,我不得不返回一个文件夹然后手动指向再次输出路径:

      "options": {
          outputPath: (url, resourcePath, context) => {
              return `fonts/${url}`;
          },
          publicPath: '../fonts',
          useRelativePaths: true,
          name(resourcePath, resourceQuery) {
              return isDevelopment ? '[path][name].[ext]' : '[contenthash].[ext]'
          },
      }
      

      非常感谢您花时间回来回答您自己的问题。

      【讨论】:

      • 很高兴它对您有所帮助! :-)
      猜你喜欢
      • 2017-11-25
      • 1970-01-01
      • 2016-06-26
      • 2018-02-23
      • 2018-08-14
      • 2018-08-05
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      相关资源
      最近更新 更多