-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblocks.js
166 lines (155 loc) · 4.5 KB
/
blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* External dependencies
*/
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const { escapeRegExp } = require( 'lodash' );
const { join, sep } = require( 'path' );
const fastGlob = require( 'fast-glob' );
/**
* WordPress dependencies
*/
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
/**
* Internal dependencies
*/
const { baseConfig, plugins, stylesTransform } = require( './shared' );
/*
* Matches a block's name in paths in the form
* build-module/<blockName>/view.js
*/
const blockNameRegex = new RegExp( /(?<=build-module\/).*(?=(\/view))/g );
const createEntrypoints = () => {
/*
* Returns an array of paths to view.js files within the `@wordpress/block-library` package.
* These paths can be matched by the regex `blockNameRegex` in order to extract
* the block's name.
*
* Returns an empty array if no files were found.
*/
const blockViewScriptPaths = fastGlob.sync(
'./packages/block-library/build-module/**/view.js'
);
/*
* Go through the paths found above, in order to define webpack entry points for
* each block's view.js file.
*/
return blockViewScriptPaths.reduce( ( entries, scriptPath ) => {
const [ blockName ] = scriptPath.match( blockNameRegex );
return {
...entries,
[ 'blocks/' + blockName ]: scriptPath,
};
}, {} );
};
module.exports = {
...baseConfig,
name: 'blocks',
entry: createEntrypoints(),
output: {
devtoolNamespace: 'wp',
filename: './build/block-library/[name]/view.min.js',
path: join( __dirname, '..', '..' ),
},
plugins: [
...plugins,
new DependencyExtractionWebpackPlugin( { injectPolyfill: false } ),
new CopyWebpackPlugin( {
patterns: [].concat(
[
'style',
'style-rtl',
'editor',
'editor-rtl',
'theme',
'theme-rtl',
].map( ( filename ) => ( {
from: `./packages/block-library/build-style/*/${ filename }.css`,
to( { absoluteFilename } ) {
const [ , dirname ] = absoluteFilename.match(
new RegExp(
`([\\w-]+)${ escapeRegExp(
sep
) }${ filename }\\.css$`
)
);
return join(
'build/block-library/blocks',
dirname,
filename + '.css'
);
},
transform: stylesTransform,
} ) ),
Object.entries( {
'./packages/block-library/src/':
'build/block-library/blocks/',
'./packages/edit-widgets/src/blocks/':
'build/edit-widgets/blocks/',
'./packages/widgets/src/blocks/': 'build/widgets/blocks/',
} ).flatMap( ( [ from, to ] ) => [
{
from: `${ from }/**/index.php`,
to( { absoluteFilename } ) {
const [ , dirname ] = absoluteFilename.match(
new RegExp(
`([\\w-]+)${ escapeRegExp(
sep
) }index\\.php$`
)
);
return join( to, `${ dirname }.php` );
},
transform: ( content ) => {
content = content.toString();
// Within content, search for any function definitions. For
// each, replace every other reference to it in the file.
return (
Array.from(
content.matchAll(
/^\s*function ([^\(]+)/gm
)
)
.reduce( ( result, [ , functionName ] ) => {
// Prepend the Gutenberg prefix, substituting any
// other core prefix (e.g. "wp_").
return result.replace(
new RegExp( functionName, 'g' ),
( match ) =>
'gutenberg_' +
match.replace( /^wp_/, '' )
);
}, content )
// The core blocks override procedure takes place in
// the init action default priority to ensure that core
// blocks would have been registered already. Since the
// blocks implementations occur at the default priority
// and due to WordPress hooks behavior not considering
// mutations to the same priority during another's
// callback, the Gutenberg build blocks are modified
// to occur at a later priority.
.replace(
/(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
'$1, 20'
)
);
},
noErrorOnMissing: true,
},
{
from: `${ from }/*/block.json`,
to( { absoluteFilename } ) {
const [ , dirname ] = absoluteFilename.match(
new RegExp(
`([\\w-]+)${ escapeRegExp(
sep
) }block\\.json$`
)
);
return join( to, dirname, 'block.json' );
},
},
] )
),
} ),
].filter( Boolean ),
};