1
1
import path from "path"
2
- import { get , mapValues , isPlainObject , trim } from "lodash"
2
+ import { mapValues , isPlainObject , trim } from "lodash"
3
3
import webpack from "webpack"
4
4
import HtmlWebpackPlugin from "html-webpack-plugin"
5
5
import HtmlWebpackExcludeAssetsPlugin from "html-webpack-exclude-assets-plugin"
6
6
import MiniCssExtractPlugin from "mini-css-extract-plugin"
7
7
// TODO: swap back when https://github.com/geowarin/friendly-errors-webpack-plugin/pull/86 lands
8
8
import FriendlyErrorsPlugin from "@pieh/friendly-errors-webpack-plugin"
9
9
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.
14
12
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.
19
15
const mapped = fn ( obj )
20
16
if ( mapped !== obj ) {
21
17
return mapped
22
18
}
23
19
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.
27
21
if ( Array . isArray ( obj ) ) {
28
22
return obj . map ( value => deepMap ( value , fn ) )
29
23
}
@@ -33,6 +27,42 @@ function deepMap(obj, fn) {
33
27
return obj
34
28
}
35
29
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
+
36
66
exports . onCreateDevServer = ( { app, store } , { publicPath = `admin` } ) => {
37
67
const { program } = store . getState ( )
38
68
const publicPathClean = trim ( publicPath , `/` )
@@ -49,7 +79,7 @@ exports.onCreateDevServer = ({ app, store }, { publicPath = `admin` }) => {
49
79
}
50
80
51
81
exports . onCreateWebpackConfig = (
52
- { store, stage, getConfig, plugins, pathPrefix } ,
82
+ { store, stage, getConfig, plugins, pathPrefix, loaders } ,
53
83
{
54
84
modulePath,
55
85
publicPath = `admin` ,
@@ -79,26 +109,11 @@ exports.onCreateWebpackConfig = (
79
109
path : path . join ( program . directory , `public` , publicPathClean ) ,
80
110
} ,
81
111
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 ) ,
96
113
} ,
97
114
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.
102
117
...gatsbyConfig . plugins . filter (
103
118
plugin =>
104
119
! [ `MiniCssExtractPlugin` , `GatsbyWebpackStatsExtractor` ] . find (
@@ -122,49 +137,37 @@ exports.onCreateWebpackConfig = (
122
137
} ,
123
138
} ) ,
124
139
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.
129
142
new MiniCssExtractPlugin ( {
130
143
filename : `[name].css` ,
131
144
} ) ,
132
145
133
- /**
134
- * Auto generate CMS index.html page.
135
- */
146
+ // Auto generate CMS index.html page.
136
147
new HtmlWebpackPlugin ( {
137
148
title : htmlTitle ,
138
149
chunks : [ `cms` ] ,
139
150
excludeAssets : [ / c m s .c s s / ] ,
140
151
} ) ,
141
152
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.
147
156
new HtmlWebpackExcludeAssetsPlugin ( ) ,
148
157
149
- /**
150
- * Pass in needed Gatsby config values.
151
- */
158
+ // Pass in needed Gatsby config values.
152
159
new webpack . DefinePlugin ( {
153
160
__PATH__PREFIX__ : pathPrefix ,
154
161
CMS_PUBLIC_PATH : JSON . stringify ( publicPath ) ,
155
162
} ) ,
156
163
] . filter ( p => p ) ,
157
164
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.
162
167
mode : stage === `develop` ? `development` : `production` ,
163
168
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.
168
171
minimizer : stage === `develop` ? [ ] : gatsbyConfig . optimization . minimizer ,
169
172
} ,
170
173
devtool : stage === `develop` ? `cheap-module-source-map` : `source-map` ,
0 commit comments