admin管理员组文章数量:1394154
module: {
rules: [
{
test: /\.(jpg|png)$/,
exclude: /(^logo\.png$|^logo3\.png$)/,
use: [{
loader: 'url-loader',
options: {
limit: 15 * 1024,
name: "imgs/[name].[ext]"
}
}]
},
{
include: /(^logo\.png$|^logo3\.png$)/,
use: [{
loader: 'file-loader',
options: {
name: 'imgs/[name].[ext]'
}
}]
}
]
}
This config can't let logo.png
and logo3.png
loaded by file-loader
if I change the regexp to /(logo.png|logo3.png)/
it will be work.
Why does the old regexp not work?
module: {
rules: [
{
test: /\.(jpg|png)$/,
exclude: /(^logo\.png$|^logo3\.png$)/,
use: [{
loader: 'url-loader',
options: {
limit: 15 * 1024,
name: "imgs/[name].[ext]"
}
}]
},
{
include: /(^logo\.png$|^logo3\.png$)/,
use: [{
loader: 'file-loader',
options: {
name: 'imgs/[name].[ext]'
}
}]
}
]
}
This config can't let logo.png
and logo3.png
loaded by file-loader
if I change the regexp to /(logo.png|logo3.png)/
it will be work.
Why does the old regexp not work?
Share Improve this question edited Jul 10, 2018 at 9:07 Gucci 9311 gold badge8 silver badges27 bronze badges asked Jul 10, 2018 at 8:14 blastzblastz 3655 silver badges15 bronze badges 2- quite hard regex... can you try this /logo3?\.png$/ Also do not use ^ because file name includes path to resource (i.e. user/app/src/logo.png) – Ilia Commented Jul 10, 2018 at 8:58
- Yes, I forgot the path of the resource. If I use ^, the test will be false. Maybe you can post an answer, so I can close my question. – blastz Commented Jul 10, 2018 at 9:12
3 Answers
Reset to default 1First of all, your regex actually work!
you can head over here for testing you regex pattern online. This is a pretty good page for testing your regex
https://regex101./
And secondly, just my though, that maybe your second rule have no test config init.
You could try to add test: /\.(jpg|png)$/
in your second rule config
IMO you are using ^
which means the filename should strictly start from logo.png
and internally webpack might have appended the path like /path/to/logo.png
and your condition to strictly begin with logo.png
fails and hence your file doesn't get included.
when you removed ^
then /path/to/logo.png
is valid according to /(logo.png|logo3.png)/
Here's an example using webpack 4:
{
test: /.(ts|tsx)?$/,
loader: 'ts-loader',
exclude: {
or: [
function(item){
console.log('regex match', item)
return item.match(/(.*)stories.tsx/)
},
path.resolve(__dirname, 'frontend/@types'),
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, 'frontend/data'),
],
},
include: [
path.resolve(__dirname, 'frontend'),
],
options: {
// disable type checker - we will use it in fork plugin
transpileOnly: true,
},
},
See:
- https://webpack.js/configuration/module/#ruleexclude
- https://webpack.js/configuration/module/#condition
本文标签: javascriptwebpack module test the regexp not workStack Overflow
版权声明:本文标题:javascript - webpack module test the regexp not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684001a2619585.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论