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(gatsby-plugin-netlify-cms): exclude node_modules from cms #15191

Merged
merged 4 commits into from
Jul 1, 2019

Conversation

erquhart
Copy link
Contributor

@erquhart erquhart commented Jun 27, 2019

Description

#14111 caused a regression for gatsby-plugin-netlify-cms, which reuses a modified version of Gatsby's final webpack config and requires that node_modules be excluded from all builds. This PR adds the exclusion directly to the plugin.

Related to #15190

@erquhart erquhart requested a review from a team as a code owner June 27, 2019 20:16
@moonmeister
Copy link
Contributor

@erquhart Can you give some context, maybe the original issue or a explanation of the regression? I just ran into some issues while updating my site but was able to get it to build and deploy. It appears to be working. see #15190 for my troubles.

P.S. I'm mostly just curious and want to understand...not saying you're incorrect.

wardpeet
wardpeet previously approved these changes Jun 28, 2019
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

Seems like I can't fix it from inside gatsby.

I inlined some of your replace functions, didn't seemed necessary to abstract it away. made it harder to read for me.

Awesome work! when everything is green let me merge and get this patched. Thank you for making gatsby better! 👍

@erquhart
Copy link
Contributor Author

@wardpeet thanks for the edits! I think I might see a couple of issues, let me give a quick look before you merge, I'll be at my machine soon.

Copy link
Contributor Author

@erquhart erquhart left a comment

Choose a reason for hiding this comment

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

@wardpeet I added a few comments - the gist is that, we should be using safe methods like lodash.get and castArray here. This plugin is coloring outside the lines of Gatsby's API, constructing a Webpack config from the raw received config, and there are no guarantees as to what that object will look like.

Performance is also not an issue as this whole thing runs more or less instantaneously, and only once per build.

@@ -33,6 +27,38 @@ function deepMap(obj, fn) {
return obj
}

function replaceRule(value) {
// If `value` does not have a `test` property, it isn't a rule object.
if (!value || !value.test) {
Copy link
Contributor Author

@erquhart erquhart Jun 28, 2019

Choose a reason for hiding this comment

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

value could be a string - we're recursing the entire received object, in which case !value.test would throw. Rather than first making sure it's an object and risking some other unforeseen issue, we should just go bulletproof with lodash.get.

Copy link
Contributor

Choose a reason for hiding this comment

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

if test is a string it will give a falsy value (undefined) when value.test is used. So no harm here, thanks for bringing it up to my attention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, value could be a string. This will throw for some users.

Copy link
Contributor

@DSchau DSchau Jul 1, 2019

Choose a reason for hiding this comment

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

@erquhart do you have an example?

var a = 'node_modules' // or var a = ''

if (!a || !a.test) {
  // we will be in this block
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm not sure why I was thinking that would throw as if value is undefined. I know I ran into issues deep mapping the webpack config and needed safe getters, but you're right, this wouldn't be an issue at this line.

return {
...value,
exclude: new RegExp(
[value.exclude.source, `node_modules|bower_components`].join(`|`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't know for sure that the exclude is and will continue to be a RegExp, it could easily be an array. castArray is the safe route, its guaranteed not to fail, we always know what we're getting, and the output is equally compatible with Webpack.

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 checking that it's a regex now. If people have a custom js rule they should make sure to exclude node_modules themselves.

@erquhart
Copy link
Contributor Author

@moonmeister ah, your issue is exactly what this addresses, I'll comment there.

@erquhart
Copy link
Contributor Author

@wardpeet sorry to bug! Any chance of getting this in before the weekend? Bit of a showstopper for affected users.

rdela added a commit to rdela/voidcluster that referenced this pull request Jun 28, 2019
Gatsby@2.11.0 Build failure
gatsbyjs/gatsby#15190 (comment)
> gatsby-plugin-netlify-cms runs an independent Webpack build within
> Gatsby's Webpack build (insert "yo dawg" reference here) using a
> modified version of the Gatsby Webpack config. Gatsby 2.11.0 no longer
> excludes node_modules, which causes the plugin's Webpack build to attempt
> processing the Netlify CMS modules with Babel. The modules are massive
> and also prebuilt, so it will (and should) crash your system.

> The PR I raised

fix(gatsby-plugin-netlify-cms): exclude node_modules from cms builds
gatsbyjs/gatsby#15191

> just adds that exclusion back in to the plugin's Webpack config.
> Please don't override your node max file size limit to fix this!

feat(gatsby): enable babel-loader for all dependencies
gatsbyjs/gatsby#14111
@Jamamuuga
Copy link

Will any additional changes be needed for a developer who uses the plugin & uses Yarn's PnP feature?

@erquhart
Copy link
Contributor Author

Update: comments on #15190 show that this isn't limited to the Netlify CMS plug-in. The appropriate fix to that issue may supercede this PR.

Sent with GitHawk

Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

Updated PR, thanks @erquhart for bringing those thinks to my attention

@moonmeister, it's just an out of memory as the cms.js bundle is about 3mb if not mistaken and a bit much to store in memory it seems.

@@ -33,6 +27,38 @@ function deepMap(obj, fn) {
return obj
}

function replaceRule(value) {
// If `value` does not have a `test` property, it isn't a rule object.
if (!value || !value.test) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if test is a string it will give a falsy value (undefined) when value.test is used. So no harm here, thanks for bringing it up to my attention.

return {
...value,
exclude: new RegExp(
[value.exclude.source, `node_modules|bower_components`].join(`|`)
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 checking that it's a regex now. If people have a custom js rule they should make sure to exclude node_modules themselves.

Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

Thank you for getting this fixed @erquhart! ❤️

@wardpeet wardpeet changed the title fix(gatsby-plugin-netlify-cms): exclude node_modules from cms builds fix(gatsby-plugin-netlify-cms): exclude node_modules from cms Jul 1, 2019
@wardpeet wardpeet merged commit a767854 into gatsbyjs:master Jul 1, 2019
@erquhart
Copy link
Contributor Author

erquhart commented Jul 1, 2019

@wardpeet you misread my comment: value may be a string, in which case value.test will throw. I also commented that a proper fix to #15190 would likely supercede this PR, which appears to have happened in #15270. I recommend reverting this PR.

@erquhart
Copy link
Contributor Author

erquhart commented Jul 1, 2019

Took another look based on @DSchau's comment on the review, and you're right, value wouldn't throw. I do still think this should be reverted since the bug it addressed has itself been reverted.

@rdela
Copy link
Contributor

rdela commented Jul 6, 2019

@wardpeet @erquhart In case it helps figure any of this out, this repo that uses Netlify CMS is still broken under 2.13.6 2.13.9, but everything is fine under 2.10.5. See master branch vs rdela/voidcluster#54; build failure log at https://app.netlify.com/sites/voidcluster/deploys/5d20f59e9f191b000a971e08
https://app.netlify.com/sites/voidcluster/deploys/5d242cc7332e50bd7113ca72

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

6 participants