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

Fix #12 Support configurable tokens for the gulp module tagger #561

Merged
merged 2 commits into from
Aug 17, 2016

Conversation

doug-wade
Copy link
Collaborator

Support configurable tokens for the gulp module tagger. Fixes #12

@doug-wade
Copy link
Collaborator Author

builds on #560

@gigabo
Copy link
Contributor

gigabo commented Aug 15, 2016

The stack's getting too tall here. Will review when #560 lands.

@doug-wade
Copy link
Collaborator Author

@gigabo #560 merged and I rebased onto latest master

, THIS_MODULE = isWindows
? /(?:[^\\]+\\node_modules\\)?react-server-gulp-module-tagger\\index\.js$/
: /(?:[^\/]+\/node_modules\/)?react-server-gulp-module-tagger\/index\.js$/

module.exports = function(config) {
config || (config = {});
var REPLACE_TOKEN;
if (config.token) {
REPLACE_TOKEN = new RegExp("(?:" + config.token + ")(?:\\(\\s*(\\{[\\s\\S]*?\\})\\s*\\))?", 'g');
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't like having the regexp machinery duplicated...

Maybe this could be in a function, and the default could be defined using it?

Like:

DEFAULT_REPLACE_TOKEN = tokenToRegexp("__LOGGER__|__CHANNEL__|__CACHE__")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't reuse the second capture group based on this advice from MDN

The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.

The constructor of the regular expression object, for example, new RegExp('ab+c'), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

The benefit of using a pre-compiled regex is ~10% -- if the code reuse tradeoff seems worth the lost performance in the default case, I'm more than happy to make the change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that's a good consideration. I think in this case we can have our cake and eat it, too, though. So long as DEFAULT_REPLACE_TOKEN stays at the package level where it is, the penalty for running it through the RegExp constructor will only be paid once. After that it's just a compiled RegExp. What we want to avoid for performance reasons is new RegExp(...) at run time.

@doug-wade doug-wade added the enhancement New functionality. label Aug 17, 2016
@@ -6,13 +6,17 @@ var replace = require("gulp-replace")
// - "__LOGGER__"
// - "__LOGGER__({ /* options */ })"
var isWindows = ('win32' === process.platform)
, REPLACE_TOKEN = /(?:__LOGGER__|__CHANNEL__|__CACHE__)(?:\(\s*(\{[\s\S]*?\})\s*\))?/g
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@gigabo like this?

, THIS_MODULE = isWindows
? /(?:[^\\]+\\node_modules\\)?react-server-gulp-module-tagger\\index\.js$/
: /(?:[^\/]+\/node_modules\/)?react-server-gulp-module-tagger\/index\.js$/

module.exports = function(config) {
config || (config = {});
if (config.token) {
REPLACE_TOKEN = tokenToRegExp(config.token);
Copy link
Contributor

Choose a reason for hiding this comment

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

So... it looks like this replaces the default. If I use the module tagger once with config.token and then later once without I'll get the previous invocation's config.token value instead of the default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

new diff

@gigabo
Copy link
Contributor

gigabo commented Aug 17, 2016

Looks awesome to me! 🤘

Thanks for humoring my obsession with factoring out the regexp line noise @doug-wade. 😅

@doug-wade doug-wade merged commit 7f61d9b into redfin:master Aug 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants