string-replace-loader icon indicating copy to clipboard operation
string-replace-loader copied to clipboard

Callback replace not working

Open Durss opened this issue 5 years ago • 4 comments

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 avatar Oct 23 '20 20:10 Durss

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.

Durss avatar Oct 23 '20 21:10 Durss

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

williamweckl avatar Feb 07 '21 20:02 williamweckl

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

Durss avatar Feb 07 '21 21:02 Durss

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

Va1 avatar Feb 12 '21 12:02 Va1