-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathgutenberg.js
111 lines (96 loc) · 3.26 KB
/
gutenberg.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
/** @format */
/**
* External dependencies
*/
const fs = require( 'fs' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const path = require( 'path' );
const { compact, get } = require( 'lodash' );
const DIRECTORY_DEPTH = '../../'; // Relative path of the extensions to preset directory
function sharedScripts( folderName, inputDir ) {
const sharedPath = path.join( inputDir, folderName );
return fs
.readdirSync( sharedPath )
.map( file => path.join( sharedPath, file ) )
.filter( fullPathToFile => fullPathToFile.endsWith( '.js' ) );
}
function blockScripts( type, inputDir, presetBlocks ) {
return presetBlocks
.map( block => path.join( inputDir, `${ DIRECTORY_DEPTH }${ block }/${ type }.js` ) )
.filter( fs.existsSync );
}
exports.config = ( { argv: { inputDir, outputDir }, getBaseConfig } ) => {
const baseConfig = getBaseConfig( {
cssFilename: '[name].css',
externalizeWordPressPackages: true,
} );
const presetPath = path.join( inputDir, 'index.json' );
let editorScript;
let editorBetaScript;
let viewBlocksScripts;
let viewScriptEntry;
let presetBlocks;
let presetBetaBlocks;
if ( fs.existsSync( presetPath ) ) {
const presetIndex = require( presetPath );
presetBlocks = get( presetIndex, [ 'production' ], [] );
presetBetaBlocks = get( presetIndex, [ 'beta' ], [] );
const allPresetBlocks = [ ...presetBlocks, ...presetBetaBlocks ];
// Find all the shared scripts
const sharedUtilsScripts = sharedScripts( 'shared', inputDir );
// Helps split up each block into its own folder view script
viewBlocksScripts = allPresetBlocks.reduce( ( viewBlocks, block ) => {
const viewScriptPath = path.join( inputDir, `${ DIRECTORY_DEPTH }${ block }/view.js` );
if ( fs.existsSync( viewScriptPath ) ) {
viewBlocks[ block + '/view' ] = [ ...sharedUtilsScripts, ...[ viewScriptPath ] ];
}
return viewBlocks;
}, {} );
// Find all the editor shared scripts
const sharedEditorUtilsScripts = sharedScripts( 'editor-shared', inputDir );
// Combines all the different blocks into one editor.js script
editorScript = [
...sharedUtilsScripts,
...sharedEditorUtilsScripts,
...blockScripts( 'editor', inputDir, presetBlocks ),
];
// Combines all the different blocks into one editor-beta.js script
editorBetaScript = [
...sharedUtilsScripts,
...sharedEditorUtilsScripts,
...blockScripts( 'editor', inputDir, allPresetBlocks ),
];
// We explicitly don't create a view.js bundle since all the views are
// available via the individual folders.
viewScriptEntry = null;
} else {
editorScript = path.join( inputDir, 'editor.js' );
const viewScript = path.join( inputDir, 'view.js' );
viewScriptEntry = fs.existsSync( viewScript ) ? { view: viewScript } : {};
}
return {
...baseConfig,
plugins: compact( [
...baseConfig.plugins,
fs.existsSync( presetPath ) &&
new CopyWebpackPlugin( [
{
from: presetPath,
to: 'index.json',
},
] ),
] ),
entry: {
editor: editorScript,
...( editorBetaScript && { 'editor-beta': editorBetaScript } ),
...viewScriptEntry,
...viewBlocksScripts,
},
output: {
path: outputDir || path.join( inputDir, 'build' ),
filename: '[name].js',
libraryTarget: 'window',
},
externals: [ ...baseConfig.externals, 'lodash' ],
};
};