Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File loader changed image file name but not the file name in HTML file

Tags:

webpack

I need to load ico and svg file using webpack. However, file name is converted to hash number, therefore HTML file cannot file those asset and generate a 404 error.

enter image description here

I need the loaders to hash the asset file name, AND change the file name to the hash name at the same time in HTML file. How can I do this?

Here is the html code to display one svg and one icon.

    <object type="image/svg+xml" data="spider-web.svg">
      Your browser does not support SVG
    </object>
    <img src="favicon.ico" alt="">

below is webpack config file:

'use strict';
// webpack.config.js

var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var entryBasePath  = __dirname;
var outputBasePath = __dirname + '/dist';


module.exports = {
    context: entryBasePath,
    entry:{
        app: ['webpack/hot/dev-server', './appEntry.js']
    },
    output: {
        path: outputBasePath,
        filename: './bundle.js',
        sourceMapFilename: '[file].map' // set source map output name rule
    }, 
    devtool: 'source-map', // enable source map
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({template: 'index.html'}),
        // new webpack.optimize.UglifyJsPlugin({compress: {warnings: false}})
    ], 
    module: {
        loaders: [
            {test: /\.scss$/, loader: 'style!css!sass'}, 
            {test: /\.css$/, loader: 'style!css'},
            {test: /\.(html|ico)(\?[\s\S]+)?$/, loader: 'raw'},
            {
        test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: "url?limit=10000" 
      },
      {
        test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
        loader: 'file'
      },
      { test: /bootstrap-sass\/assets\/javascripts\//, loader: 'imports?jQuery=jquery' },
      { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192' } 
    ]
    }
}
like image 899
Nicolas S.Xu Avatar asked Jul 19 '16 22:07

Nicolas S.Xu


1 Answers

You have a couple of options here. The quickest one would be to provide a name query string option to the loader.

For example the file loader would be:

   {
    test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
    loader: 'file-loader?name=[name].[ext]'
  },

You should be able to use the same query parameter for the url-loader.

like image 63
cgatian Avatar answered Nov 15 '22 13:11

cgatian