mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
blocks.js
146 lines (137 loc) · 3.67 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
/**
* External dependencies
*/
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
/**
* Internal dependencies
*/
const {
baseDir,
getBaseConfig,
normalizeJoin,
stylesTransform,
} = require( './shared' );
const {
isDynamic,
toDirectoryName,
getStableBlocksMetadata,
} = require( '../release/sync-stable-blocks' );
module.exports = function (
env = { environment: 'production', watch: false, buildTarget: false }
) {
const mode = env.environment;
const suffix = mode === 'production' ? '.min' : '';
let buildTarget = env.buildTarget
? env.buildTarget
: mode === 'production'
? 'build'
: 'src';
buildTarget = buildTarget + '/wp-includes';
const blocks = getStableBlocksMetadata();
const dynamicBlockFolders = blocks
.filter( isDynamic )
.map( toDirectoryName );
const blockFolders = blocks.map( toDirectoryName );
const blockPHPFiles = {
'widgets/src/blocks/legacy-widget/index.php':
'wp-includes/blocks/legacy-widget.php',
'widgets/src/blocks/widget-group/index.php':
'wp-includes/blocks/widget-group.php',
...dynamicBlockFolders.reduce( ( files, blockName ) => {
files[
`block-library/src/${ blockName }/index.php`
] = `wp-includes/blocks/${ blockName }.php`;
return files;
}, {} ),
};
const blockMetadataFiles = {
'widgets/src/blocks/legacy-widget/block.json':
'wp-includes/blocks/legacy-widget/block.json',
'widgets/src/blocks/widget-group/block.json':
'wp-includes/blocks/widget-group/block.json',
...blockFolders.reduce( ( files, blockName ) => {
files[
`block-library/src/${ blockName }/block.json`
] = `wp-includes/blocks/${ blockName }/block.json`;
return files;
}, {} ),
};
const blockPHPCopies = Object.keys( blockPHPFiles ).map( ( filename ) => ( {
from: normalizeJoin( baseDir, `node_modules/@wordpress/${ filename }` ),
to: normalizeJoin( baseDir, `src/${ blockPHPFiles[ filename ] }` ),
} ) );
const blockMetadataCopies = Object.keys( blockMetadataFiles ).map(
( filename ) => ( {
from: normalizeJoin(
baseDir,
`node_modules/@wordpress/${ filename }`
),
to: normalizeJoin(
baseDir,
`src/${ blockMetadataFiles[ filename ] }`
),
} )
);
const blockStylesheetCopies = blockFolders.map( ( blockName ) => ( {
from: normalizeJoin(
baseDir,
`node_modules/@wordpress/block-library/build-style/${ blockName }/*.css`
),
to: normalizeJoin(
baseDir,
`${ buildTarget }/blocks/${ blockName }/[name]${ suffix }.css`
),
transform: stylesTransform( mode ),
noErrorOnMissing: true,
} ) );
// Todo: This list need of entry points need to be automatically fetched from the package
// We shouldn't have to maintain it manually.
const interactiveBlocks = [
'navigation',
'image',
'query',
'file',
'search',
];
const baseConfig = getBaseConfig( env );
const config = {
...baseConfig,
entry: interactiveBlocks.reduce(( memo, blockName ) => {
memo[ blockName ] = {
import: normalizeJoin(
baseDir,
`node_modules/@wordpress/block-library/build-module/${ blockName }/view`
),
};
return memo;
}, {}),
experiments: {
outputModule: true,
},
output: {
devtoolNamespace: 'wp',
filename: `./blocks/[name]/view${ suffix }.js`,
path: normalizeJoin( baseDir, buildTarget ),
library: {
type: 'module',
},
environment: { module: true },
},
externalsType: 'module',
externals: {
'@wordpress/interactivity': '@wordpress/interactivity',
'@wordpress/interactivity-router': 'import @wordpress/interactivity-router',
},
plugins: [
...baseConfig.plugins,
new CopyWebpackPlugin( {
patterns: [
...blockPHPCopies,
...blockMetadataCopies,
...blockStylesheetCopies,
],
} ),
],
};
return config;
};