-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
175 lines (163 loc) · 5.12 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
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
167
168
169
170
171
172
173
174
175
/**
* Custom Webpack Config
*
* Extends the WordPress WP-Scripts configuration for our local use.
*
* WP-Scripts webpack config documentation:
* https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#default-webpack-config
*/
const { resolve, extname } = require( 'path' );
const { sync: glob } = require( 'fast-glob' );
const pkg = require( './package.json' );
const BrowserSyncPlugin = require( 'browser-sync-v3-webpack-plugin' );
const browserSyncOpts = require( './browsersync.config' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
/**
* General theme scripts & styles entry points
*
* The entry point names are prefixed with `assets` to direct their output into
* an assets subdirectory in the root dist folder.
*/
const assetEntryPoints = () => {
return {
'assets/admin': resolve(
pkg.config.coreThemeDir,
'assets',
'admin.js'
),
'assets/editor': resolve(
pkg.config.coreThemeDir,
'assets',
'editor.js'
),
'assets/theme': resolve(
pkg.config.coreThemeDir,
'assets',
'theme.js'
),
'assets/login': resolve(
pkg.config.coreThemeDir,
'assets',
'login.pcss'
),
'assets/print': resolve(
pkg.config.coreThemeDir,
'assets',
'print.pcss'
),
};
};
/**
* Auto-find and load any block-based entry points.
*
* This is a simplified version of WP-Scripts' blocks.json entry point loader.
* We want to support block.json entry points within subdirectories of the theme
* blocks directory, and we can safely ignore other legacy entry point formats.
*
* @return {{}|undefined} An object of block entry points or undefined of there are none.
*/
const blockEntryPoints = () => {
/**
* Create files array using index.js, editor.js and view.js files
*
* We use `index.js` instead of `block.json` for glob b/c core blocks
* won't contain a `block.json` file (they are already registered),
* but they still may contain scripts or styles which should be processed.
*/
const files = [
...glob( `${ pkg.config.coreThemeBlocksDir }/**/index.js`, {
absolute: true,
} ),
...glob( `${ pkg.config.coreThemeBlocksDir }/**/editor.js`, {
absolute: true,
} ),
...glob( `${ pkg.config.coreThemeBlocksDir }/**/view.js`, {
absolute: true,
} ),
];
if ( ! files.length ) {
return;
}
const entryPoints = {};
files.forEach( ( entryFilePath ) => {
const entryName = entryFilePath
.replace( extname( entryFilePath ), '' )
.replace( `${ resolve( pkg.config.coreThemeDir ) }/`, '' );
entryPoints[ entryName ] = entryFilePath;
} );
return entryPoints;
};
/**
* The configuration for copying block.json files from the src to dist folder
* doesn't work with our namespaced nested blocks structure. Thus, we have to
* find the plugin's config in the greater config object and explicitly set
* the destination location (`to:`) for the coped file(s).
*/
const copyPluginIndex = defaultConfig.plugins.findIndex(
( plugin ) => plugin.patterns
);
if ( copyPluginIndex > -1 ) {
defaultConfig.plugins[ copyPluginIndex ].patterns.forEach(
( pattern, index ) => {
if ( pattern.from === '**/block.json' ) {
defaultConfig.plugins[ copyPluginIndex ].patterns[ index ] = {
...defaultConfig.plugins[ copyPluginIndex ].patterns[
index
],
context: resolve( pkg.config.coreThemeDir, 'blocks/' ),
to: resolve( pkg.config.coreThemeDir, 'dist/blocks/' ),
};
} else if ( pattern.from === '**/*.php' ) {
defaultConfig.plugins[ copyPluginIndex ].patterns[ index ] = {
from: '**/*.php',
noErrorOnMissing: true,
context: resolve( pkg.config.coreThemeDir, 'blocks/tribe' ),
to: resolve( pkg.config.coreThemeDir, 'dist/blocks/tribe' ),
};
}
}
);
}
module.exports = {
...defaultConfig,
resolve: {
...defaultConfig.resolve,
alias: {
...defaultConfig.resolve.alias,
utils: resolve( './wp-content/themes/core/assets/js/utils' ),
common: resolve( './wp-content/themes/core/assets/js/common' ),
config: resolve( './wp-content/themes/core/assets/js/config' ),
blocks: resolve( './wp-content/themes/core/blocks' ),
},
},
entry: {
...assetEntryPoints(),
...blockEntryPoints(),
},
output: {
...defaultConfig.output,
path: resolve( pkg.config.coreThemeDir, 'dist' ), // Change the output path to `dist` instead of `build`
},
plugins: [
...defaultConfig.plugins,
/**
* Remove empty auto-generated index.js files
*
* Webpack auto-generates an empty index.js file for every entry point.
* When we create a styles-only entry point such as print.pcss
* this plugin deletes that empty index.js file after it is built.
*/
new RemoveEmptyScriptsPlugin( {
stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
} ),
/**
* Add browsersync so any file changes auto-reload the browser.
*
* WP-Scripts does make use of Webpack hot module reloading,
* but it doesn't support non-JS entry points at this time.
* For our purposes, broswersync is more helpful.
*/
new BrowserSyncPlugin( browserSyncOpts, { reload: false } ), // Add browser-sync for dev reloads
],
};