长话短说,我不能在 Assets 文件名中使用某些字符,例如连字符。我没有运气好通过 webpack 文档解析来确定是否可以使用正则表达式或类似的东西重命名文件,所以我可以从我不控制源文件名的第 3 方包中删除任何连字符。
我 super 天真的例子是这样的:
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: `url-loader?limit=${ASSETS_LIMIT}&name=fonts/[name.replace(/-/)].[ext]`
}
有谁知道这是否可能或如何满足这一要求?谢谢!
最佳答案
这个谜语的答案似乎可以在 customInterpolateName
加载器选项中找到。对于 webpack@v3.4.1,下面是我删除连字符的最终结果。
这是关键的花絮:
plugins: [
// ... other plugins ...
new webpack.LoaderOptionsPlugin({
options: {
customInterpolateName: (loaderContext) => {
return loaderContext.replace(/-/g, '');
}
}
})
]
这里有一个更完整的例子来提供一些上下文(注意:.css
被有意附加到字体文件名中作为 Dynamics CRM 中另一个网络资源名称限制的解决方法):
module.exports = {
// ... other config ...
module: {
loaders: [
// ... other loaders ...
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: `url-loader?limit=${ASSETS_LIMIT}&name=fonts/[name].[ext].css`
}
]
},
plugins: [
// ... other plugins ...
new webpack.LoaderOptionsPlugin({
options: {
customInterpolateName: (loaderContext) => {
return loaderContext.replace(/-/g, '');
}
}
})
]
};
关于javascript - 是否可以从 webpack 的文件名中去除特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578418/