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

Allow passing a function as a RegExp substitute #245

Merged
merged 6 commits into from
Dec 13, 2017
Merged

Conversation

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

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
Owner

tleunen commented Dec 9, 2017

looks good to me

@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
Contributor Author

fatfisz commented Dec 10, 2017

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

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
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

Done.

DOCS.md Outdated
"^@namespace/foo-(.+)": ([, name]) => path.join('packages', name)
}
}]
]
}
```

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
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
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
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
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
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.

None yet

2 participants