forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.extensions.js
96 lines (85 loc) · 2.78 KB
/
webpack.config.extensions.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
/**
**** WARNING: No ES6 modules here. Not transpiled! ****
*/
/* eslint-disable import/no-nodejs-modules, lodash/import-scope */
/**
* External dependencies
*/
const _ = require( 'lodash' );
const fs = require( 'fs' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const getBaseWebpackConfig = require( '@automattic/calypso-build/webpack.config.js' );
const path = require( 'path' );
/**
* Internal dependencies
*/
// const { workerCount } = require( './webpack.common' ); // todo: shard...
/**
* Internal variables
*/
const editorSetup = path.join( __dirname, 'extensions', 'editor' );
const viewSetup = path.join( __dirname, 'extensions', 'view' );
function blockScripts( type, inputDir, presetBlocks ) {
return presetBlocks
.map( block => path.join( inputDir, 'blocks', block, `${ type }.js` ) )
.filter( fs.existsSync );
}
const presetPath = path.join( __dirname, 'extensions', 'index.json' );
const presetIndex = require( presetPath );
const presetBlocks = _.get( presetIndex, [ 'production' ], [] );
const presetBetaBlocks = _.get( presetIndex, [ 'beta' ], [] );
const allPresetBlocks = [ ...presetBlocks, ...presetBetaBlocks ];
// Helps split up each block into its own folder view script
const viewBlocksScripts = allPresetBlocks.reduce( ( viewBlocks, block ) => {
const viewScriptPath = path.join( __dirname, 'extensions', 'blocks', block, 'view.js' );
if ( fs.existsSync( viewScriptPath ) ) {
viewBlocks[ block + '/view' ] = [ viewSetup, ...[ viewScriptPath ] ];
}
return viewBlocks;
}, {} );
// Combines all the different blocks into one editor.js script
const editorScript = [
editorSetup,
...blockScripts( 'editor', path.join( __dirname, 'extensions' ), presetBlocks ),
];
// Combines all the different blocks into one editor-beta.js script
const editorBetaScript = [
editorSetup,
...blockScripts( 'editor', path.join( __dirname, 'extensions' ), allPresetBlocks ),
];
const webpackConfig = getBaseWebpackConfig(
{ WP: true },
{
entry: {
editor: editorScript,
'editor-beta': editorBetaScript,
...viewBlocksScripts,
},
'output-chunk-filename': '[name].[chunkhash].js',
'output-path': path.join( __dirname, '_inc', 'blocks' ),
}
);
const transpileConfig = webpackConfig.module.rules.find( rule =>
rule.use.some( loader => loader.options.presets )
);
module.exports = {
...webpackConfig,
// The `module` override fixes https://github.com/Automattic/jetpack/issues/12511.
// @TODO Remove once there's a fix in `@automattic/calypso-build`
module: {
...webpackConfig.module,
rules: [
{ ...transpileConfig, exclude: /node_modules\/(?!punycode)/ },
..._.without( webpackConfig.module.rules, transpileConfig ),
],
},
plugins: [
...webpackConfig.plugins,
new CopyWebpackPlugin( [
{
from: presetPath,
to: 'index.json',
},
] ),
],
};