Va1 / string-replace-loader

Replace loader for Webpack
MIT License
250 stars 57 forks source link

Callback replace not working #60

Open Durss opened 4 years ago

Durss commented 4 years ago

Hello ! I'm having lots of troubles having the callback replace working. I put logs in many places and i'm tempted to say it may come either from schema-utils or loader-utils or ajv-keyword modules but i'm very new to webpack plugins so i can't tell for sure.

If i add this log in replace.js

  console.log("\nSEARCH :: ", search, replace);
  const newSource = source.replace(search, replace)

I also added a log on getOptionsArray.js :

  const rawOptions = getOptions(config)
  console.log(rawOptions);

And the "replace" field is simply missing from the log. The search and flags keys are here.

If the replacement is a string the logs show the replacement properly, if the replacement is a fucntion, i get "null" everytime, except once sometimes at the end of the build.

This works :

multiple: [
       {
        search: 'hello',
        flags: 'gi',
        replace: 'test'
       },
]

This does not :

multiple: [
       {
        search: 'hello',
        flags: 'gi',
        replace: function(match, p1, offset, string) {
            return "test";
        },
    },
]

Sometimes when i close the project (vscode), kill the node processes, and try to build again, the callback is passed properly everytime. But this happens very very randomly. It seems like there's also a compile cache somewhere that blocks the plugin's call or something. I never put my hands inside webpack so i may be missing a thing.

Any idea what could be the reason of this?

Durss commented 4 years ago

Some more info.

I tried to copy/paste the plugin to my local folder, removed all the external dependencies, and i still have the problem.

If i log the raw options, any Function typed param get stripped out the object. I'm starting to think it might actually be an issue with Vue-Cli as i'm using it in a Vue project.

The way to use a plugin with Vue env is a bit special (not sure it's specific to Vue actually) as it looks like this :

    chainWebpack: (config) => {
        config.module.rule()
        .test(/\.ts$/)
        .use()
        .loader("./local-string-replace")
        .options({
            multiple: [
               {
                    search: 'hello',
                    flags: 'gi',
                    testRep: function(match, p1, offset, string) {
                        return "test?"
                    },
                    replace: function(match, p1, offset, string) {
                        return "morning!";
                    },
                },
            ]
          }
        );
    },

Maybe there's some sanitazing somewhere that cleansup params to remove potentially sensitive stuff like function calls... Again, quite new to webpack so i might be missing something obvious.

williamweckl commented 3 years ago

I`m experiencing the same issue. @Durss did you find a solution?

Durss commented 3 years ago

Nope i just created something from scratch more or less equivalent to this plugin that answered my needs :/

Va1 commented 3 years ago

hi. there' a test-case written for the callback replacement and it works. can you check if that's approximately how you're approaching callback replacement as well? thanks

https://github.com/Va1/string-replace-loader/blob/master/test/index.test.js#L135