Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback replace not working #60

Open
Durss opened this issue Oct 23, 2020 · 4 comments
Open

Callback replace not working #60

Durss opened this issue Oct 23, 2020 · 4 comments

Comments

@Durss
Copy link

Durss commented Oct 23, 2020

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
Copy link
Author

Durss commented Oct 23, 2020

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
Copy link

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

@Durss
Copy link
Author

Durss commented Feb 7, 2021

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

@Va1
Copy link
Owner

Va1 commented Feb 12, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants