Skip to content

Cannot supply flags when constructing one RegExp from another #31

@shirishp

Description

@shirishp

Configuration

module.exports = {
  // ... 
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'string-replace',
        query: {
          multiple: [
             { search: '{vars.id}', replace: '90', flags: 'g' },
             { search: '{path.api}', replace: 'http://localhost:9000', flags: 'g'}
          ]
        }
      }
    ]
  }
}

Error:

WARNING in <file_name>
Module build failed: TypeError: Cannot supply flags when constructing one RegExp from another
    at new RegExp (native)
    at processOptions (node_modules/string-replace-loader/index.js:8:24)
    at node_modules/string-replace-loader/index.js:31:16
    at Array.forEach (native)
    at Object.module.exports (node_modules/string-replace-loader/index.js:29:22)
 @ ./src/javascript/scripts \.js$

This seems to be happening because https://github.com/Va1/string-replace-loader/blob/master/index.js#L29 returns source as regular expression, and then the regular expression is again passed to RegEx constructor at https://github.com/Va1/string-replace-loader/blob/master/index.js#L7

Workaround

For now, changing the configuration to following seems to work:

module.exports = {
  // ... 
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'string-replace',
        query: {
          multiple: [
             { search: new RegExp('{vars.id}', 'g'), replace: '90'},
             { search: new RegExp('{path.api}', 'g'), replace: 'http://localhost:9000'}
          ]
        }
      }
    ]
  }
}

Notice the multiple array using search option as RegExp instead of plain string without flags option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions