forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.extensions.js
168 lines (154 loc) · 5.09 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
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
/**
**** WARNING: No ES6 modules here. Not transpiled! ****
*/
/* eslint-disable 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' );
const webpack = require( 'webpack' );
const StaticSiteGeneratorPlugin = require( 'static-site-generator-webpack-plugin' );
/**
* 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 presetProductionBlocks = _.get( presetIndex, [ 'production' ], [] );
const presetNoPostEditorBlocks = _.get( presetIndex, [ 'no-post-editor' ], [] );
const presetExperimentalBlocks = [
...presetProductionBlocks,
..._.get( presetIndex, [ 'experimental' ], [] ),
];
// Beta Blocks include all blocks: beta, experimental, and production blocks.
const presetBetaBlocks = [ ...presetExperimentalBlocks, ..._.get( presetIndex, [ 'beta' ], [] ) ];
// Helps split up each block into its own folder view script
const viewBlocksScripts = presetBetaBlocks.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 production blocks into one editor.js script
const editorScript = [
editorSetup,
...blockScripts( 'editor', path.join( __dirname, 'extensions' ), presetProductionBlocks ),
];
// Combines all the different Experimental blocks into one editor.js script
const editorExperimentalScript = [
editorSetup,
...blockScripts( 'editor', path.join( __dirname, 'extensions' ), presetExperimentalBlocks ),
];
// Combines all the different blocks into one editor-beta.js script
const editorBetaScript = [
editorSetup,
...blockScripts( 'editor', path.join( __dirname, 'extensions' ), presetBetaBlocks ),
];
const editorNoPostEditorScript = [
editorSetup,
...blockScripts( 'editor', path.join( __dirname, 'extensions' ), presetNoPostEditorBlocks ),
];
const extensionsWebpackConfig = getBaseWebpackConfig(
{ WP: true },
{
entry: {
editor: editorScript,
'editor-experimental': editorExperimentalScript,
'editor-beta': editorBetaScript,
'editor-no-post-editor': editorNoPostEditorScript,
...viewBlocksScripts,
},
'output-chunk-filename': '[name].[chunkhash].js',
'output-path': path.join( __dirname, '_inc', 'blocks' ),
}
);
const componentsWebpackConfig = getBaseWebpackConfig(
{ WP: false },
{
entry: {
components: path.join( __dirname, './extensions/shared/components/index.jsx' ),
},
'output-chunk-filename': '[name].[chunkhash].js',
'output-library-target': 'commonjs2',
'output-path': path.join( __dirname, '_inc', 'blocks' ),
'output-pathinfo': true,
}
);
// We export two configuration files: One for admin.js, and one for components.jsx.
// The latter produces pre-rendered components HTML.
module.exports = [
{
...extensionsWebpackConfig,
plugins: [
...extensionsWebpackConfig.plugins,
new CopyWebpackPlugin( [
{
from: presetPath,
to: 'index.json',
},
] ),
],
},
{
...componentsWebpackConfig,
plugins: [
...componentsWebpackConfig.plugins,
new webpack.NormalModuleReplacementPlugin(
/^@wordpress\/i18n$/,
path.join( __dirname, './extensions/shared/i18n-to-php' )
),
new webpack.NormalModuleReplacementPlugin(
/^\.\/create-interpolate-element$/,
path.join( __dirname, './extensions/shared/element-to-php' )
),
new StaticSiteGeneratorPlugin( {
// The following mocks are required to make `@wordpress/` npm imports work with server-side rendering.
// Hopefully, most of them can be dropped once https://github.com/WordPress/gutenberg/pull/16227 lands.
globals: {
Mousetrap: {
init: _.noop,
prototype: {},
},
document: {
addEventListener: _.noop,
createElement: _.noop,
documentElement: _.noop,
head: { appendChild: _.noop },
},
navigator: {},
window: {
addEventListener: _.noop,
// See https://github.com/WordPress/gutenberg/blob/f3b6379327ce3fb48a97cb52ffb7bf9e00e10130/packages/jest-preset-default/scripts/setup-globals.js
matchMedia: () => ( {
addListener: () => {},
} ),
navigator: { platform: '', userAgent: '' },
Node: {
TEXT_NODE: '',
ELEMENT_NODE: '',
DOCUMENT_POSITION_PRECEDING: '',
DOCUMENT_POSITION_FOLLOWING: '',
},
removeEventListener: _.noop,
URL: {},
},
},
} ),
],
},
];