Skip to content

Allow passing a function as a RegExp substitute#245

Merged
tleunen merged 6 commits intomasterfrom
function-substitute
Dec 13, 2017
Merged

Allow passing a function as a RegExp substitute#245
tleunen merged 6 commits intomasterfrom
function-substitute

Conversation

@fatfisz
Copy link
Copy Markdown
Contributor

@fatfisz fatfisz commented Dec 9, 2017

This implements a feature requested in #242.

@fatfisz fatfisz changed the title Allow passing a function as a RegExp substitute WIP: Allow passing a function as a RegExp substitute Dec 9, 2017
@fatfisz fatfisz requested a review from tleunen December 9, 2017 20:33
@fatfisz
Copy link
Copy Markdown
Contributor Author

fatfisz commented Dec 9, 2017

@tleunen This still requires a docs update, will do that tomorrow. For now please give me a heads up if more tests are necessary.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 9, 2017

Codecov Report

Merging #245 into master will not change coverage.
The diff coverage is 100%.

Impacted Files Coverage Δ
src/normalizeOptions.js 100% <100%> (ø) ⬆️

@tleunen
Copy link
Copy Markdown
Owner

tleunen commented Dec 9, 2017

looks good to me

@fatfisz fatfisz force-pushed the function-substitute branch from 24c5d1f to 728dd9e Compare December 10, 2017 23:52
@fatfisz fatfisz changed the title WIP: Allow passing a function as a RegExp substitute Allow passing a function as a RegExp substitute Dec 10, 2017
@fatfisz
Copy link
Copy Markdown
Contributor Author

fatfisz commented Dec 10, 2017

We can merge if you're ok with the wording in the docs 🙂

Comment thread DOCS.md Outdated

Because the function is only called when there is a match, the argument can never be `null`.

*Important: a function can only be used with regular expressions.*
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't all aliases converted into regex anyway?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are, but the value for the "basic" alias is made into a substitution string like so:

getAliasPair(`^${key}(/.*|)$`, `${alias[key]}\\1`);

I could add some more logic there to always detect functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup I think being consistent here would be less surprising and the note in the DOCS wouldn't be necessary. Win-win!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread DOCS.md Outdated

Using the config from this example `'@namespace/foo-bar'` will become `'packages/bar'` or `'packages\\bar'` depending on the OS.
Using the config from this example:
* `'foo/baz'` will become `'bar/baz'` or `'bar\\baz'`
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should use path.join as an example.

On windows or unix, all paths in node for the import/require files should in theory be in a posix format afaik... right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK Node allows me to use \ on Windows. I'll adjust the examples though, to make them more simple.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, manually joining path parts is a bad practice... I'd rather stick to path.join than use +.

Copy link
Copy Markdown
Owner

@tleunen tleunen Dec 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree that in general people should always use path.join to create a path. But in this case, inside a node/web project, I don't think it's a good idea to mix posix and windows paths.

What if a project is built on windows and provide paths with \? Would it run on a mac/linux pc? Will webpack understand the paths?
I feel it's risky to have this in our doc since most people should never use windows paths for their imports/require.

Maybe replacing the example to remove path.join would be best.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will remove them.

@tleunen tleunen merged commit 9299d9a into master Dec 13, 2017
@tleunen tleunen deleted the function-substitute branch December 13, 2017 00:07
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

Successfully merging this pull request may close these issues.

2 participants