@@ -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,101 @@ 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
+ // TODO: svg css modules
142
+
143
+ // Standard SVGs referenced by a URL
144
+ config . loader ( 'standard-svgs-1' , ( ) => ( {
145
+ test : / \. s v g $ / ,
146
+ exclude : [ / \. s p r i t e \. s v g $ / , / \. i n l i n e \. s v g $ / ] ,
147
+ loader : 'file-loader'
148
+ } ) )
149
+
150
+ config . loader ( 'standard-svgs-2' , ( ) => ( {
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 : 'svgo-loader' ,
154
+ query : {
155
+ plugins : [
156
+ { inlineStyles : { onlyMatchedOnce : false } } ,
157
+ { removeAttrs : { attrs : 'class' } }
158
+ ]
159
+ }
160
+ } ) )
161
+
162
+ // Ensure that the Gatsby's default SVG handling doesn't mess with the previous declarations
163
+ config . loader ( 'url-loader' , ( current ) => ( {
164
+ ...current ,
165
+ exclude : / \. s v g $ /
166
+ } ) )
167
+
168
+ return current
169
+ } )
170
+
171
+ // Do not inline images
172
+ // We have a lot of small images that summed together increase the inital HTML files by a lot
173
+ config . loader ( 'url-loader' , ( current ) => ( {
174
+ ...current ,
175
+ loader : 'file-loader'
176
+ } ) )
177
+
178
+ // Shrink CSS module class names in production
179
+ if ( stage === 'build-css' || stage === 'build-javascript' || stage === 'build-html' ) {
180
+ config . loader ( 'cssModules' , ( current ) => ( {
181
+ ...current ,
182
+ loader : current . loader . replace ( / l o c a l I d e n t N a m e = [ ^ ! ] + / , 'localIdentName=[hash:base64:10]' )
183
+ } ) )
184
+ }
117
185
}
118
186
119
187
exports . createLayouts = ( ) => {
120
188
// Create a layout for each locale, based on a template
121
189
const layoutTemplate = fs . readFileSync ( path . join ( __dirname , 'src/layouts/index.js' ) )
122
190
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 } ` )
191
+ availableLocales . forEach ( ( locale ) => {
192
+ const acronym = locale . acronym
193
+ const localeLayout = template ( layoutTemplate ) ( { locale : acronym } )
194
+ . replace ( / L a y o u t Q u e r y / , `LayoutQuery_${ acronym } ` )
126
195
127
- fs . writeFileSync ( path . join ( __dirname , `src/layouts/index-${ locale } .js` ) , localeLayout )
196
+ fs . writeFileSync ( path . join ( __dirname , `src/layouts/index-${ acronym } .js` ) , localeLayout )
128
197
} )
129
198
}
130
199
@@ -134,11 +203,13 @@ exports.onCreatePage = ({ page, boundActionCreators }) => {
134
203
135
204
deletePage ( page )
136
205
137
- availableLocalesAcronym . forEach ( ( locale ) => {
206
+ availableLocales . forEach ( ( locale ) => {
207
+ const acronym = locale . acronym
208
+
138
209
createPage ( {
139
210
...page ,
140
- layout : `index-${ locale } ` ,
141
- path : locale === defaultLocale ? page . path : `/${ locale } ${ page . path } `
211
+ layout : `index-${ acronym } ` ,
212
+ path : acronym === defaultLocale ? page . path : `/${ acronym } ${ page . path } `
142
213
} )
143
214
} )
144
215
}
0 commit comments