forked from WordPress/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
158 lines (142 loc) · 3.53 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
/**
* External dependencies
*/
const path = require( 'path' );
const WebpackBar = require( 'webpackbar' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
/**
* Internal dependencies
*/
const { plugins: standalonePlugins } = require( './plugins.json' );
const {
createPluginZip,
assetDataTransformer,
deleteFileOrDirectory,
generateBuildManifest,
} = require( './tools/webpack/utils' );
/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const defaultBuildConfig = {
entry: {},
output: {
path: path.resolve( __dirname, 'build' ),
},
};
const sharedConfig = {
...defaultConfig,
...defaultBuildConfig,
};
// Store plugins that require build process.
const pluginsWithBuild = [ 'optimization-detective' ];
/**
* Webpack Config: Optimization Detective
*
* @param {*} env Webpack environment
* @return {Object} Webpack configuration
*/
const optimizationDetective = ( env ) => {
if ( env.plugin && env.plugin !== 'optimization-detective' ) {
return defaultBuildConfig;
}
const source = path.resolve( __dirname, 'node_modules/web-vitals' );
const destination = path.resolve(
__dirname,
'plugins/optimization-detective/build'
);
return {
...sharedConfig,
name: 'optimization-detective',
plugins: [
new CopyWebpackPlugin( {
patterns: [
{
from: `${ source }/dist/web-vitals.js`,
to: `${ destination }/web-vitals.js`,
},
{
from: `${ source }/package.json`,
to: `${ destination }/web-vitals.asset.php`,
transform: {
transformer: assetDataTransformer,
cache: false,
},
},
],
} ),
new WebpackBar( {
name: 'Building Optimization Detective Assets',
color: '#2196f3',
} ),
],
};
};
/**
* Webpack configuration for building the plugin for distribution.
* Note: Need to pass plugin name like `--env.plugin=plugin-name` to build particular plugin.
*
* @param {*} env Webpack environment
* @return {Object} Webpack configuration
*/
const buildPlugin = ( env ) => {
if ( ! env.plugin ) {
return defaultBuildConfig;
}
if ( ! standalonePlugins.includes( env.plugin ) ) {
// eslint-disable-next-line no-console
console.error( `Plugin "${ env.plugin }" not found. Aborting.` );
return defaultBuildConfig;
}
const buildDir = path.resolve( __dirname, 'build' );
const to = path.resolve( buildDir, env.plugin );
const from = path.resolve( __dirname, 'plugins', env.plugin );
const dependencies = pluginsWithBuild.includes( env.plugin )
? [ `${ env.plugin }` ]
: [];
return {
...sharedConfig,
name: 'build-plugin',
plugins: [
new CopyWebpackPlugin( {
patterns: [
{
from,
to,
globOptions: {
dot: true,
ignore: [
'**/.wordpress-org',
'**/phpcs.xml.dist',
'**/tests',
'**/*.[Cc]ache',
],
},
},
],
} ),
{
apply: ( compiler ) => {
// Before run, delete the build directory.
compiler.hooks.beforeRun.tap( 'BeforeRunPlugin', () => {
deleteFileOrDirectory( to );
} );
// After emit, generate build manifest.
compiler.hooks.afterEmit.tap( 'AfterEmitPlugin', () => {
generateBuildManifest( env.plugin, from );
// If zip flag is passed, create a zip file.
if ( env.zip ) {
createPluginZip( buildDir, env.plugin );
}
} );
},
},
new WebpackBar( {
name: `Building ${ env.plugin } Plugin`,
color: '#4caf50',
} ),
],
dependencies,
};
};
module.exports = [ optimizationDetective, buildPlugin ];