-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
105 lines (96 loc) · 2.33 KB
/
webpack.config.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
const path = require( 'path' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const fastGlob = require( 'fast-glob' );
const WebpackBar = require( 'webpackbar' );
const ReplaceInFileWebpackPlugin = require( 'replace-in-file-webpack-plugin' );
const CopyPlugin = require( 'copy-webpack-plugin' );
/*
* Matches a block's name in paths in the form
* build-module/<blockName>/view.js
*/
const blockNameRegex = new RegExp( /(?<=blocks\/).*(?=(\/index))/g );
const createBlockEntrypoints = () => {
/*
* 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(
'./blocks/**/index.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,
[ blockName ]: scriptPath,
};
}, {} );
};
const sharedConfig = {
...defaultConfig,
externalsType: 'window',
plugins: [
...defaultConfig.plugins,
new ReplaceInFileWebpackPlugin( [
{
dir: 'dist',
test: [ /\.js/ ],
rules: [
{
search: /TEXTDOMAIN/gi,
replace: 'events-calendar-live-viewer',
},
],
},
] ),
],
};
const blocks = {
...sharedConfig,
entry: createBlockEntrypoints(),
output: {
path: path.resolve( process.cwd(), 'dist', 'blocks' ),
filename: '[name].js',
chunkFilename: '[name].js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Blocks',
color: '#cd1cec',
} ),
],
};
const publicScripts = {
...sharedConfig,
entry: {
'live-player': './assets/scripts/src/live-player.js',
},
output: {
path: path.resolve( process.cwd(), 'dist', 'public' ),
filename: '[name].js',
chunkFilename: '[name].js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Public',
color: '#5aec1c',
} ),
],
};
const externals = {
...sharedConfig,
output: {
path: path.resolve( process.cwd(), 'dist' ),
filename: '[name].js',
chunkFilename: '[name].js',
},
};
module.exports = [ publicScripts, blocks, externals ];