Webpack 没有加载背景图片

Webpack not loading background image

我正在尝试加载图像:

background: transparent url("../img/select-icon.png") no-repeat center right 8px;

在我的 style.scss 上,它不工作

这是我的 webpack.config:

function _path(p) {
  return path.join(__dirname, p);
}

module.exports = {

    context: __dirname,
    entry: [
        './assets/js/index'
    ], 

    output: {
        path: path.resolve('./assets/bundles/'), 
        filename: '[name].js'
    },

    devtool: 'inline-eval-cheap-source-map',

    plugins: [
        new BundleTracker({filename: './webpack-stats.json'}), 
        new webpack.ProvidePlugin({ 
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery' 
        }),
        new HappyPack({
            id: 'jsx',
            threads: 4,
            loaders: ["babel-loader"]
        })

    ],

    module: {
        loaders: [
            {
                test: /\.css$/,
                include: path.resolve(__dirname, './assets/css/'),
                loader: "style-loader!css-loader"
            },

            {
                test: /\.scss$/,
                include: path.resolve(__dirname, './assets/css/'),
                loader: "style-loader!css-loader!sass-loader"
            },

            {
                test: /\.jsx?$/, 
                include: path.resolve(__dirname, './assets/js/'),
                exclude: /node_modules/, 
                loaders: ["happypack/loader?id=jsx"]
            },
            {
                test: /\.png$/,
                loader: 'file-loader'
            }
        ]
    },

    resolve: {
        modulesDirectories: ['node_modules'],
        extensions: ['', '.js', '.jsx'],
        alias: {
          'inputmask' : _path('node_modules/jquery-mask-plugin/dist/jquery.mask'),
        }, 
    }   
}

我正在使用文件加载器,但在浏览器中呈现的 url 与图像的 url 不匹配。

我认为我没有使用源映射:https://github.com/webpack/style-loader/issues/55

有人能帮忙吗?

我使用 Angular Class 启动包所以我不知道你是否有 "helpers" class 可用。

在 webpack congif/common 中添加一个辅助常量。

const helpers = require('./helpers');

在你的决心内:添加

root: helpers.root('src'),

并在加载程序中添加:

  {
    test: /\.html$/,
    loader: 'raw-loader',
    exclude: [helpers.root('src/index.html')]
  },

现在将 scss 加载程序更改为此

  {
    test: /\.scss$/,
    exclude: /node_modules/,
    loaders: ['raw-loader', 'sass-loader']
  },

现在我不确定你是否还能使用 "include.path" 我只是在 Url 中添加 "assets" 就像这样 "url ('.[=31= .png').

希望对你有所帮助,祝你好运。

要将 url('...') 解析为您需要在 css-loader.

之前使用 resolve-url-loader 的文件输出路径

在您的 webpack.config.js 中,您需要进行以下更改:

//...
module: {
    loaders: [
        {
            test: /\.css$/,
            include: path.resolve(__dirname, './assets/css/'),
            loader: "style-loader!css-loader!resolve-url-loader"
        },

        {
            test: /\.scss$/,
            include: path.resolve(__dirname, './assets/css/'),
            loader: "style-loader!css-loader!resolve-url-loader!sass-loader"
        },
        //...
    ]
}
//...

作为可选配置,您可以指定 file-loader 使用的文件名模板。因此,例如 96ab4c4434475d0d‌​23b82bcfc87be595.png,您可以 /img/select-icon.png.

在您的 webpack.config.js 中,正在使用 file-loader 的默认选项。这意味着输出文件名正在使用模板 [id].[ext]。作为 [id] 块 ID 和 [ext] 文件的扩展名。

要指定输出文件名模板,您需要将 name 参数传递给加载程序的查询。

到目前为止,require('../img/select-icon.png') 应该 return '/img/select-icon.png'resolve-url-loader 将使用此值。

//...
module: {
    loaders: [
        //...
        {
            test: /\.png$/,
            loader: 'file-loader',
            query: {
                name: 'img/[name].[ext]'
            }
        },
        //...
    ]
}
//...