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

Switch back to babel-loader #5143

Merged
merged 6 commits into from
Sep 27, 2018
Merged

Conversation

iansu
Copy link
Contributor

@iansu iansu commented Sep 27, 2018

Closes #5122

@gaearon
Copy link
Contributor

gaearon commented Sep 27, 2018

pls remove yarn lock

@iansu
Copy link
Contributor Author

iansu commented Sep 27, 2018

Oops, I left it out initially but it must have got added back when I rebased.

return {
...config.options,
caller: {
name: 'babel-preset-react-app',
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably spread caller if present and keep existing name. Just add the craInvalidationToken.

// file basis.
if (source.indexOf('.macro') !== -1 || source.indexOf('/macro') !== -1) {
return {
...config.options,
Copy link
Contributor

Choose a reason for hiding this comment

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

Rest spread isn't available in lower versions of Node 8, please switch this to Object.assign.


module.exports = loader.custom(() => overrides);
module.exports = function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's name this file something else, e.g. overrides or loader-customization.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe babel-preset-react-app/webpack-overrides

Copy link
Contributor Author

Choose a reason for hiding this comment

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

babel-overrides?

Copy link
Contributor

Choose a reason for hiding this comment

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

Internally, Babel uses overrides so I like that -- this is specific to the webpack loader though.

webpack-overrides is good, but loader-overrides would work too.

@Timer Timer added this to the 2.0.0 milestone Sep 27, 2018
Copy link
Contributor

@Timer Timer left a comment

Choose a reason for hiding this comment

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

Replace loader.js with new file name in package.json.

@iansu
Copy link
Contributor Author

iansu commented Sep 27, 2018

Good catch.

// Babel Macros are notoriously hard to cache, so they shouldn't be
// https://github.com/babel/babel/issues/8497
// We naively detect macros using their package suffix and insert a random
// caller name, a valid option accepted by Babel, to compose a one-time
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: update comment to reflect the move from modifying the name to adding craInvalidationToken

// to the caller, a valid option accepted by Babel, to compose a one-time
// cacheIdentifier for the file. We cannot tune the loader options on a per
// file basis.
if (source.indexOf('.macro') !== -1 || source.indexOf('/macro') !== -1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you can do one lookup for just macro.
Then check the character at previous index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To make that work we'd have to save the index into a variable. Is that tradeoff worth it?

Copy link
Contributor

@Timer Timer Sep 27, 2018

Choose a reason for hiding this comment

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

At that point, we might as well just look for macro. If the previous character wasn't . or /, we'd probably want to search again -- nullifying any performance gain.
Though, 9/10 times we'd probably see that . or / character and get to bail out early.

FWIW, indexOf appears to be extremely fast: https://jsperf.com/test-indexof-vs-includes
This only needs to be ran once on a file that doesn't include macros.

(I'm sure it's way slower on a large file, but faster than babel parsing it!)

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume regex would be way slower?

Copy link
Contributor

Choose a reason for hiding this comment

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

To make that work we'd have to save the index into a variable. Is that tradeoff worth it?

What do you mean? There's no real cost for putting it into a variable in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume regex would be way slower?

It depends. Regex can actually be faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just mean the cost of both seems quite low so is it worth trading one for the other? I think the way it is currently it's easy to read and understand what's going on. I think it gets a bit more complicated/less clear if we change the algorithm (see @Timer's comment above).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Timer I would assume regex would be slower too but then I started to second guess that.

https://koukia.ca/top-6-ways-to-search-for-a-string-in-javascript-and-performance-benchmarks-ce3e9b81ad31#---0-21

Maybe doing both checks in one go with a regex would be fastest?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with using a regex.

I disagree re: the cost being low. That's how you get to slow builds, one small decision at a time. If it's a hot path (which it is for cold builds) let's put in some effort to avoid doing the same work twice.

Copy link
Contributor

Choose a reason for hiding this comment

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

Meh. Whatever. I'm just being grumpy. It probably doesn't matter at this scale. Things like this tend to matter in React but this is not React.

// to the caller, a valid option accepted by Babel, to compose a one-time
// cacheIdentifier for the file. We cannot tune the loader options on a per
// file basis.
if (/[./]macro/.test(source)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now is this going to be slow because I'm creating a new regex each time this function is called? Should it be declared once outside of this function?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, externalize it and feed it through new RegExp.

@Timer Timer merged commit 39c73ce into facebook:master Sep 27, 2018
@@ -272,8 +272,11 @@ module.exports = {
// We need to use our own loader until `babel-loader` supports
// customization
// https://github.com/babel/babel-loader/pull/687
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment needs deleted.

// We need to use our own loader until `babel-loader` supports
// customization
// https://github.com/babel/babel-loader/pull/687

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! Removed in 232e892.

zmitry pushed a commit to zmitry/create-react-app that referenced this pull request Sep 30, 2018
* Switch back to babel-loader

* Preserve existing caller options. Use Object.assign instead of object spread.

* Updated filename in package.json

* Update comment about cache identifier

* Update macro check to use a regex

* Move macro check regex out of function
@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
@iansu iansu deleted the babel-loader-customization branch October 18, 2019 05:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants