crebuh
crebuh

Reputation: 357

Webpack string replace loader not working

I'm bundling my project with webpack. In order to manipulate some of the files I've to replace specific values in a certain node-module. To achieve that I'm using the string-replace-loader. I added the following code to my webpack config without success.

  {
    test: /\.js$/,
    loader: 'string-replace',
    query: {
      search: '[MouseEvent]',
      replace: '[]'
    }
  }

In the bundeld file the string is not replaced. The files are within the folder node_modules/ng2-bootstrap. so may i have to specify this as well?

Upvotes: 1

Views: 3940

Answers (1)

TeoMatthew
TeoMatthew

Reputation: 579

you should use include: [/node_modules\/module_name/] because webpack by default exclude node_modules.

But while reading an issue for similar problems, probably you must use "exclude, include" like this:

{
  test: /\.js$/,
  loader: 'string-replace',
  exclude: /node_modules(?!\/module_name)/
  query: {
    search: '[MouseEvent]',
    replace: '[]'
  }
}

if you want read the issue, go here.

EDIT AFTER COMMENTS:

after some tried, i've done this (with an other plugin, but seems like yours), i've done that with this code:

  preLoaders: [
    {
      test: /\.js$/,
      loader: StringReplacePlugin.replace({
        replacements: [
          {
            pattern: /.*?(react).*?/ig,
            replacement: function (match, p1, offset, string) {
              console.log("found");
              return "found";
            }
          }
        ]}),
      include: /node_modules\\react.*/
    }
  ],

it might works on loaders, i've but on preloaders because i already have a loader for js. Note that the error was on regex of include/exclude

Upvotes: 2

Related Questions