@@ -10,12 +10,11 @@ const path = require('path')
10
10
const fs = require ( 'fs' )
11
11
const template = require ( 'lodash/template' )
12
12
const wrap = require ( 'lodash/wrap' )
13
- const { defaultLocale } = require ( './intl/config' )
13
+ const { defaultLocale, availableLocales } = require ( './intl/config' )
14
14
const ServiceWorkerWebpackPlugin = require ( 'serviceworker-webpack-plugin' )
15
- const { getLocalesAcronym } = require ( './src/utils/getLocalesUtils' )
16
- const availableLocalesAcronym = getLocalesAcronym ( )
15
+ const SvgStorePlugin = require ( 'external-svg-sprite-loader/lib/SvgStorePlugin' )
17
16
18
- module . exports . modifyWebpackConfig = ( { config, program } ) => {
17
+ module . exports . modifyWebpackConfig = ( { config, program, stage } ) => {
19
18
// Allow requires from the src/ folder
20
19
config . merge ( {
21
20
resolve : {
@@ -33,20 +32,6 @@ module.exports.modifyWebpackConfig = ({ config, program }) => {
33
32
return current
34
33
} )
35
34
36
- // Add prefix to all svg class or id selectors to avoid styles override
37
- // config.merge((current) => {
38
- // config.loader('svg-react-loader', (current) => {
39
- // current.query = {
40
- // ...current.query,
41
- // classIdPrefix: true,
42
- // };
43
- //
44
- // return current;
45
- // });
46
- //
47
- // return current;
48
- // });
49
-
50
35
// Allow requires from the src/ folder for postcss
51
36
config . merge ( ( current ) => {
52
37
const importPath = [
@@ -114,17 +99,107 @@ module.exports.modifyWebpackConfig = ({ config, program }) => {
114
99
115
100
return current
116
101
} )
102
+
103
+ // SVGs (external + inline + standard)
104
+ config . merge ( ( current ) => {
105
+ // External SVGs
106
+ config . loader ( 'external-svgs-1' , ( current ) => ( {
107
+ test : / \. s p r i t e \. s v g $ / ,
108
+ loader : 'external-svg-sprite-loader' ,
109
+ query : {
110
+ test : / \. s v g $ / ,
111
+ name : 'static/svg-sprite.[hash].svg'
112
+ }
113
+ } ) )
114
+
115
+ config . loader ( 'external-svgs-2' , ( current ) => ( {
116
+ test : / \. s p r i t e \. s v g $ / ,
117
+ loader : 'svgo-loader' ,
118
+ query : {
119
+ plugins : [
120
+ { removeViewBox : false } ,
121
+ { removeDimensions : true } ,
122
+ { inlineStyles : { onlyMatchedOnce : false } } ,
123
+ { removeAttrs : { attrs : 'class' } }
124
+ ]
125
+ }
126
+ } ) )
127
+
128
+ current . plugins . push ( new SvgStorePlugin ( ) )
129
+
130
+ // Inline SVGs
131
+ config . loader ( 'inline-svgs-1' , ( ) => ( {
132
+ loader : 'raw-loader' ,
133
+ test : / \. i n l i n e \. s v g $ /
134
+ } ) )
135
+
136
+ config . loader ( 'inline-svgs-2' , ( ) => ( {
137
+ loader : 'svgo-loader' ,
138
+ test : / \. i n l i n e \. s v g $ /
139
+ } ) )
140
+
141
+ config . loader ( 'inline-svgs-3' , ( ) => ( {
142
+ loader : 'svg-css-modules-loader' ,
143
+ test : / \. i n l i n e \. s v g $ / ,
144
+ query : {
145
+ transformId : true
146
+ }
147
+ } ) )
148
+
149
+ // Standard SVGs referenced by a URL
150
+ config . loader ( 'standard-svgs-1' , ( ) => ( {
151
+ test : / \. s v g $ / ,
152
+ exclude : [ / \. s p r i t e \. s v g $ / , / \. i n l i n e \. s v g $ / ] ,
153
+ loader : 'file-loader'
154
+ } ) )
155
+
156
+ config . loader ( 'standard-svgs-2' , ( ) => ( {
157
+ test : / \. s v g $ / ,
158
+ exclude : [ / \. s p r i t e \. s v g $ / , / \. i n l i n e \. s v g $ / ] ,
159
+ loader : 'svgo-loader' ,
160
+ query : {
161
+ plugins : [
162
+ { inlineStyles : { onlyMatchedOnce : false } } ,
163
+ { removeAttrs : { attrs : 'class' } }
164
+ ]
165
+ }
166
+ } ) )
167
+
168
+ // Ensure that the Gatsby's default SVG handling doesn't mess with the previous declarations
169
+ config . loader ( 'url-loader' , ( current ) => ( {
170
+ ...current ,
171
+ exclude : / \. s v g $ /
172
+ } ) )
173
+
174
+ return current
175
+ } )
176
+
177
+ // Do not inline images
178
+ // We have a lot of small images that summed together increase the inital HTML files by a lot
179
+ config . loader ( 'url-loader' , ( current ) => ( {
180
+ ...current ,
181
+ loader : 'file-loader'
182
+ } ) )
183
+
184
+ // Shrink CSS module class names in production
185
+ if ( stage === 'build-css' || stage === 'build-javascript' || stage === 'build-html' ) {
186
+ config . loader ( 'cssModules' , ( current ) => ( {
187
+ ...current ,
188
+ loader : current . loader . replace ( / l o c a l I d e n t N a m e = [ ^ ! ] + / , 'localIdentName=[hash:base64:10]' )
189
+ } ) )
190
+ }
117
191
}
118
192
119
193
exports . createLayouts = ( ) => {
120
194
// Create a layout for each locale, based on a template
121
195
const layoutTemplate = fs . readFileSync ( path . join ( __dirname , 'src/layouts/index.js' ) )
122
196
123
- availableLocalesAcronym . forEach ( ( locale ) => {
124
- const localeLayout = template ( layoutTemplate ) ( { locale } )
125
- . replace ( / L a y o u t Q u e r y / , `LayoutQuery_${ locale } ` )
197
+ availableLocales . forEach ( ( locale ) => {
198
+ const acronym = locale . acronym
199
+ const localeLayout = template ( layoutTemplate ) ( { locale : acronym } )
200
+ . replace ( / L a y o u t Q u e r y / , `LayoutQuery_${ acronym } ` )
126
201
127
- fs . writeFileSync ( path . join ( __dirname , `src/layouts/index-${ locale } .js` ) , localeLayout )
202
+ fs . writeFileSync ( path . join ( __dirname , `src/layouts/index-${ acronym } .js` ) , localeLayout )
128
203
} )
129
204
}
130
205
@@ -134,11 +209,13 @@ exports.onCreatePage = ({ page, boundActionCreators }) => {
134
209
135
210
deletePage ( page )
136
211
137
- availableLocalesAcronym . forEach ( ( locale ) => {
212
+ availableLocales . forEach ( ( locale ) => {
213
+ const acronym = locale . acronym
214
+
138
215
createPage ( {
139
216
...page ,
140
- layout : `index-${ locale } ` ,
141
- path : locale === defaultLocale ? page . path : `/${ locale } ${ page . path } `
217
+ layout : `index-${ acronym } ` ,
218
+ path : acronym === defaultLocale ? page . path : `/${ acronym } ${ page . path } `
142
219
} )
143
220
} )
144
221
}
0 commit comments