1
1
const path = require ( 'path' ) ;
2
2
const defaultConfig = require ( '@wordpress/scripts/config/webpack.config' ) ;
3
+ const RemovePlugin = require ( 'remove-files-webpack-plugin' ) ;
3
4
const CopyPlugin = require ( 'copy-webpack-plugin' ) ;
4
5
const SVGSpritemapPlugin = require ( 'svg-spritemap-webpack-plugin' ) ;
5
6
const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
6
7
const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
7
8
const ESLintPlugin = require ( 'eslint-webpack-plugin' ) ;
8
9
const StylelintPlugin = require ( 'stylelint-webpack-plugin' ) ;
9
- const { glob } = require ( 'glob' ) ;
10
+ const glob = require ( 'glob' ) ;
10
11
const postcssRTL = require ( 'postcss-rtl' ) ;
11
12
12
13
// Dynamically generate entry points for each file inside 'assets/scss/blocks'
@@ -21,17 +22,58 @@ const coreBlockEntryPaths = glob
21
22
return acc ;
22
23
} , { } ) ;
23
24
25
+ // Dynamically generate entry points for each block
26
+ const blockEntryPaths = glob
27
+ . sync ( './assets/blocks/**/index.js' , { posix : true , dotRelative : true } )
28
+ . reduce ( ( acc , filePath ) => {
29
+ const entryKey = filePath
30
+ . replace ( './assets/blocks/' , '' )
31
+ . replace ( '/index.js' , '' ) ;
32
+ acc [ `../blocks/${ entryKey } ` ] = filePath ;
33
+ return acc ;
34
+ } , { } ) ;
35
+
36
+ const blockScssPaths = glob
37
+ . sync ( './assets/blocks/**/style.scss' , { posix : true , dotRelative : true } )
38
+ . reduce ( ( acc , filePath ) => {
39
+ const entryKey = filePath
40
+ . replace ( './assets/blocks/' , '' )
41
+ . replace ( '/style.scss' , '' ) ;
42
+ acc [ `../blocks/${ entryKey } ` ] = filePath ;
43
+ return acc ;
44
+ } , { } ) ;
45
+
46
+ const styleScssPaths = glob
47
+ . sync ( './assets/scss/index.scss' , { posix : true , dotRelative : true } )
48
+ . reduce ( ( acc , filePath ) => {
49
+ const entryKey = 'style' ;
50
+ acc [ `css/${ entryKey } ` ] = filePath ;
51
+ return acc ;
52
+ } , { } ) ;
53
+
24
54
module . exports = {
25
55
...defaultConfig ,
26
56
entry : {
27
- style : './assets/scss/index.scss' ,
28
57
index : './assets/js/index.js' ,
29
58
variations : './assets/js/block-variations/index.js' ,
30
59
filters : './assets/js/block-filters/index.js' ,
60
+ ...styleScssPaths ,
61
+ ...blockEntryPaths ,
62
+ ...blockScssPaths ,
31
63
...coreBlockEntryPaths ,
32
64
} ,
33
65
output : {
34
- filename : 'js/[name].js' ,
66
+ filename : ( pathData ) => {
67
+ const entryName = pathData . chunk . name ;
68
+ if (
69
+ entryName . includes ( 'css/blocks' ) ||
70
+ blockEntryPaths [ entryName ] ||
71
+ blockScssPaths [ entryName ]
72
+ ) {
73
+ return '[name].js' ;
74
+ }
75
+ return 'js/[name].js' ;
76
+ } ,
35
77
path : path . resolve ( __dirname , 'build' ) ,
36
78
} ,
37
79
module : {
@@ -83,13 +125,36 @@ module.exports = {
83
125
filename : 'fonts/[name][ext]' ,
84
126
} ,
85
127
} ,
128
+ {
129
+ test : / \. j s $ / ,
130
+ exclude : / n o d e _ m o d u l e s / ,
131
+ use : {
132
+ loader : 'babel-loader' ,
133
+ options : {
134
+ presets : [ '@babel/preset-env' , '@babel/preset-react' ] ,
135
+ } ,
136
+ } ,
137
+ } ,
86
138
] ,
87
139
} ,
88
140
plugins : [
89
141
...defaultConfig . plugins ,
90
142
91
143
new MiniCssExtractPlugin ( {
92
- filename : '[name].css' , // Output LTR styles to build/css directory
144
+ filename : ( pathData ) => {
145
+ const entryName = pathData . chunk . name ;
146
+ if (
147
+ entryName . includes ( 'css/blocks' ) ||
148
+ blockEntryPaths [ entryName ] ||
149
+ blockScssPaths [ entryName ]
150
+ ) {
151
+ return '[name].css' ;
152
+ }
153
+ if ( entryName === 'css/style' ) {
154
+ return 'css/style.css' ;
155
+ }
156
+ return '[name].css' ;
157
+ } ,
93
158
} ) ,
94
159
95
160
new CopyPlugin ( {
@@ -124,6 +189,30 @@ module.exports = {
124
189
} ,
125
190
} ) ,
126
191
192
+ new RemovePlugin ( {
193
+ after : {
194
+ log : false ,
195
+ test : [
196
+ {
197
+ folder : path . resolve ( __dirname , 'build' ) ,
198
+ method : ( absoluteItemPath ) => {
199
+ return new RegExp ( / \. j s / , 'm' ) . test (
200
+ absoluteItemPath
201
+ ) ;
202
+ } ,
203
+ } ,
204
+ {
205
+ folder : path . resolve ( __dirname , 'build' ) ,
206
+ method : ( absoluteItemPath ) => {
207
+ return new RegExp ( / \. p h p $ / , 'm' ) . test (
208
+ absoluteItemPath
209
+ ) ;
210
+ } ,
211
+ } ,
212
+ ] ,
213
+ } ,
214
+ } ) ,
215
+
127
216
new CleanWebpackPlugin ( ) ,
128
217
new ESLintPlugin ( ) ,
129
218
new StylelintPlugin ( ) ,
0 commit comments