-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathwebpack.config.js
More file actions
119 lines (106 loc) · 4.32 KB
/
webpack.config.js
File metadata and controls
119 lines (106 loc) · 4.32 KB
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
/**
* WARNING: No ES6 modules here. Not transpiled! *
*/
const path = require( 'path' );
const getBaseWebpackConfig = require( '@automattic/calypso-build/webpack.config.js' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const ReadableJsAssetsWebpackPlugin = require( '@wordpress/readable-js-assets-webpack-plugin' );
const webpack = require( 'webpack' );
const FSE_MODULE_PREFIX = 'a8c-fse';
/**
* Internal variables
*/
const isDevelopment = process.env.NODE_ENV !== 'production';
/**
* Return a webpack config object
*
* Arguments to this function replicate webpack's so this config can be used on the command line,
* with individual options overridden by command line args.
*
* @see {@link https://webpack.js.org/configuration/configuration-types/#exporting-a-function}
* @see {@link https://webpack.js.org/api/cli/}
* @param {Object} env environment options
* @param {string} env.source plugin slugs, comma separated list
* @param {Object} argv options map
* @param {string} argv.entry entry path
* @returns {Object} webpack config
*/
function getWebpackConfig( env = { source: '' }, argv = {} ) {
env.WP = true;
// object provides ability to name the entry point
// which enables dynamic file names
const sources = env.source.split( ',' );
// Output path
let packageName;
const entry = sources.reduce( ( accumulator, source ) => {
const sourceSegments = source.split( path.sep );
const scriptName =
sourceSegments.length > 1 ? sourceSegments.slice( 1 ).join( path.sep ) : source;
// All outputs from a particular package must share an output path.
const sourcePackage = sourceSegments[ 0 ];
if ( packageName && packageName !== sourceSegments[ 0 ] ) {
throw new Error( 'FSE can build multiple sources for a single package.' );
}
packageName = sourcePackage;
accumulator[ scriptName ] = path.join( __dirname, 'editing-toolkit-plugin', source );
return accumulator;
}, {} );
const outputPath = path.join( __dirname, 'editing-toolkit-plugin', packageName, 'dist' );
const webpackConfig = getBaseWebpackConfig( env, argv );
return {
...webpackConfig,
entry,
output: {
...webpackConfig.output,
path: outputPath,
// Unfortunately, we can't set the name to `[name].js` for the
// dev env because at runtime we'd also need a way to detect
// if the env was dev or prod, as the file is enqueued in WP
// and there's no way to do that now. The simpler alternative
// is to generate a .min.js for dev and prod, even though the
// file is not really minified in the dev env.
filename: '[name].min.js', // dynamic filename
library: 'EditingToolkit',
},
optimization: {
...webpackConfig.optimization,
// disable module concatenation so that instances of `__()` are not renamed
concatenateModules: false,
},
plugins: [
...webpackConfig.plugins.filter(
( plugin ) => plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
),
new webpack.DefinePlugin( {
__i18n_text_domain__: JSON.stringify( 'full-site-editing' ),
} ),
new DependencyExtractionWebpackPlugin( {
injectPolyfill: true,
outputFilename: '[name].asset.php',
requestToExternal( request ) {
if ( request.startsWith( FSE_MODULE_PREFIX ) ) {
switch ( request ) {
// This is not a real module, it is a placeholder that corresponds to a WordPress script handle registered with the same name.
// This allows us to import the module, declaring the dependency via JavaScript.
// A TypeScript type helps ensure it's used properly. See `./typings/fse`
case 'a8c-fse-common-data-stores':
return request;
default:
throw new Error( `Received unknown module request ${ request }.` );
}
}
// The extraction logic will only extract a package if requestToExternal
// explicitly returns undefined for the given request. Null
// shortcuts the logic such that react-i18n will be bundled.
if ( request === '@wordpress/react-i18n' ) {
return null;
}
},
} ),
new ReadableJsAssetsWebpackPlugin(),
],
devtool: isDevelopment ? 'inline-cheap-source-map' : 'source-map',
stats: 'minimal',
};
}
module.exports = getWebpackConfig;