最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - webpack module test the regexp not work - Stack Overflow

programmeradmin1浏览0评论
  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
Add a ment  | 

3 Answers 3

Reset to default 1

First 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
发布评论

评论列表(0)

  1. 暂无评论