Skip to content

Commit a767854

Browse files
erquhartwardpeet
authored andcommitted
fix(gatsby-plugin-netlify-cms): exclude node_modules from cms (#15191)
1 parent 4662d62 commit a767854

File tree

1 file changed

+57
-54
lines changed

1 file changed

+57
-54
lines changed

packages/gatsby-plugin-netlify-cms/src/gatsby-node.js

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
import path from "path"
2-
import { get, mapValues, isPlainObject, trim } from "lodash"
2+
import { mapValues, isPlainObject, trim } from "lodash"
33
import webpack from "webpack"
44
import HtmlWebpackPlugin from "html-webpack-plugin"
55
import HtmlWebpackExcludeAssetsPlugin from "html-webpack-exclude-assets-plugin"
66
import MiniCssExtractPlugin from "mini-css-extract-plugin"
77
// TODO: swap back when https://github.com/geowarin/friendly-errors-webpack-plugin/pull/86 lands
88
import FriendlyErrorsPlugin from "@pieh/friendly-errors-webpack-plugin"
99

10-
/**
11-
* Deep mapping function for plain objects and arrays. Allows any value,
12-
* including an object or array, to be transformed.
13-
*/
10+
// Deep mapping function for plain objects and arrays. Allows any value,
11+
// including an object or array, to be transformed.
1412
function deepMap(obj, fn) {
15-
/**
16-
* If the transform function transforms the value, regardless of type,
17-
* return the transformed value.
18-
*/
13+
// If the transform function transforms the value, regardless of type, return
14+
// the transformed value.
1915
const mapped = fn(obj)
2016
if (mapped !== obj) {
2117
return mapped
2218
}
2319

24-
/**
25-
* Recursively deep map arrays and plain objects, otherwise return the value.
26-
*/
20+
// Recursively deep map arrays and plain objects, otherwise return the value.
2721
if (Array.isArray(obj)) {
2822
return obj.map(value => deepMap(value, fn))
2923
}
@@ -33,6 +27,42 @@ function deepMap(obj, fn) {
3327
return obj
3428
}
3529

30+
function replaceRule(value) {
31+
// If `value` does not have a `test` property, it isn't a rule object.
32+
if (!value || !value.test) {
33+
return value
34+
}
35+
36+
// when javascript we exclude node_modules
37+
if (
38+
value.type === `javascript/auto` &&
39+
value.exclude &&
40+
value.exclude instanceof RegExp
41+
) {
42+
return {
43+
...value,
44+
exclude: new RegExp(
45+
[value.exclude.source, `node_modules|bower_components`].join(`|`)
46+
),
47+
}
48+
}
49+
50+
// Manually swap `style-loader` for `MiniCssExtractPlugin.loader`.
51+
// `style-loader` is only used in development, and doesn't allow us to pass
52+
// the `styles` entry css path to Netlify CMS.
53+
if (
54+
typeof value.loader === `string` &&
55+
value.loader.includes(`style-loader`)
56+
) {
57+
return {
58+
...value,
59+
loader: MiniCssExtractPlugin.loader,
60+
}
61+
}
62+
63+
return value
64+
}
65+
3666
exports.onCreateDevServer = ({ app, store }, { publicPath = `admin` }) => {
3767
const { program } = store.getState()
3868
const publicPathClean = trim(publicPath, `/`)
@@ -49,7 +79,7 @@ exports.onCreateDevServer = ({ app, store }, { publicPath = `admin` }) => {
4979
}
5080

5181
exports.onCreateWebpackConfig = (
52-
{ store, stage, getConfig, plugins, pathPrefix },
82+
{ store, stage, getConfig, plugins, pathPrefix, loaders },
5383
{
5484
modulePath,
5585
publicPath = `admin`,
@@ -79,26 +109,11 @@ exports.onCreateWebpackConfig = (
79109
path: path.join(program.directory, `public`, publicPathClean),
80110
},
81111
module: {
82-
/**
83-
* Manually swap `style-loader` for `MiniCssExtractPlugin.loader`.
84-
* `style-loader` is only used in development, and doesn't allow us to
85-
* pass the `styles` entry css path to Netlify CMS.
86-
*/
87-
rules: deepMap(gatsbyConfig.module.rules, value => {
88-
if (
89-
typeof get(value, `loader`) === `string` &&
90-
value.loader.includes(`style-loader`)
91-
) {
92-
return { ...value, loader: MiniCssExtractPlugin.loader }
93-
}
94-
return value
95-
}),
112+
rules: deepMap(gatsbyConfig.module.rules, replaceRule),
96113
},
97114
plugins: [
98-
/**
99-
* Remove plugins that either attempt to process the core Netlify CMS
100-
* application, or that we want to replace with our own instance.
101-
*/
115+
// Remove plugins that either attempt to process the core Netlify CMS
116+
// application, or that we want to replace with our own instance.
102117
...gatsbyConfig.plugins.filter(
103118
plugin =>
104119
![`MiniCssExtractPlugin`, `GatsbyWebpackStatsExtractor`].find(
@@ -122,49 +137,37 @@ exports.onCreateWebpackConfig = (
122137
},
123138
}),
124139

125-
/**
126-
* Use a simple filename with no hash so we can access from source by
127-
* path.
128-
*/
140+
// Use a simple filename with no hash so we can access from source by
141+
// path.
129142
new MiniCssExtractPlugin({
130143
filename: `[name].css`,
131144
}),
132145

133-
/**
134-
* Auto generate CMS index.html page.
135-
*/
146+
// Auto generate CMS index.html page.
136147
new HtmlWebpackPlugin({
137148
title: htmlTitle,
138149
chunks: [`cms`],
139150
excludeAssets: [/cms.css/],
140151
}),
141152

142-
/**
143-
* Exclude CSS from index.html, as any imported styles are assumed to be
144-
* targeting the editor preview pane. Uses `excludeAssets` option from
145-
* `HtmlWebpackPlugin` config.
146-
*/
153+
// Exclude CSS from index.html, as any imported styles are assumed to be
154+
// targeting the editor preview pane. Uses `excludeAssets` option from
155+
// `HtmlWebpackPlugin` config.
147156
new HtmlWebpackExcludeAssetsPlugin(),
148157

149-
/**
150-
* Pass in needed Gatsby config values.
151-
*/
158+
// Pass in needed Gatsby config values.
152159
new webpack.DefinePlugin({
153160
__PATH__PREFIX__: pathPrefix,
154161
CMS_PUBLIC_PATH: JSON.stringify(publicPath),
155162
}),
156163
].filter(p => p),
157164

158-
/**
159-
* Remove common chunks style optimizations from Gatsby's default
160-
* config, they cause issues for our pre-bundled code.
161-
*/
165+
// Remove common chunks style optimizations from Gatsby's default
166+
// config, they cause issues for our pre-bundled code.
162167
mode: stage === `develop` ? `development` : `production`,
163168
optimization: {
164-
/**
165-
* Without this, node can get out of memory errors
166-
* when building for production.
167-
*/
169+
// Without this, node can get out of memory errors when building for
170+
// production.
168171
minimizer: stage === `develop` ? [] : gatsbyConfig.optimization.minimizer,
169172
},
170173
devtool: stage === `develop` ? `cheap-module-source-map` : `source-map`,

0 commit comments

Comments
 (0)