【发布时间】:2016-11-23 00:22:26
【问题描述】:
我需要使用 webpack 加载 ico 和 svg 文件。但是,文件名被转换为哈希数,因此 HTML 文件无法归档这些资产并生成 404 错误。
我需要加载器对资产文件名进行哈希处理,并同时在 HTML 文件中将文件名更改为哈希名。我怎样才能做到这一点?
这是显示一个 svg 和一个图标的 html 代码。
<object type="image/svg+xml" data="spider-web.svg">
Your browser does not support SVG
</object>
<img src="favicon.ico" alt="">
下面是 webpack 配置文件:
'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' }
]
}
}
【问题讨论】:
标签: webpack