const svgToMiniDataURI = require("mini-svg-data-uri");
module.exports = {
output: {
assetModuleFilename: "images/[hash][ext]"
},
module: {
rules: [
{
test: /\.(png|jpg)$/,
type: "asset"
},
{
test: /\.svg$/,
type: "asset",
generator: {
dataUrl: content => {
if (typeof content !== "string") {
content = content.toString();
}
return svgToMiniDataURI(content);
}
}
}
]
},
devtool: false,
experiments: {
asset: true
}
};
该例子展示了module.type和module.generator的用法。可以使用generator处理导入的文件,并且不需要使用file-loader和url-loader