-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
463 lines (443 loc) · 20.2 KB
/
build.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unregisterPlugin = exports.unregisterFormat = exports.unregisterBlock = exports.registerPlugin = exports.registerFormat = exports.registerBlock = exports.beforeUpdateBlocks = exports.autoloadPlugins = exports.autoloadFormats = exports.autoloadBlocks = exports.autoload = exports.afterUpdateBlocks = exports._apply_wp_5_4_hmr_patch = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/**
* Provide helper methods to dynamically locate, load & register Blocks & Plugins.
*/
var _window$wp = window.wp,
blocks = _window$wp.blocks,
plugins = _window$wp.plugins,
richText = _window$wp.richText,
hooks = _window$wp.hooks,
data = _window$wp.data;
/**
* No-op function for use as a default argument value.
*/
var noop = function noop() {};
/**
* Require a set of modules and configure them for hot module replacement.
* The consuming function must opt-in to HMR by passing a callback to
* accept updates for the context where this function is used.
*
* The first argument should be a function returning a `require.context()`
* call. All modules loaded from this context are cached, and on each rebuild
* the incoming updated modules are checked against the cache. Updated modules
* which already exist in the cache are unregistered with the provided function,
* then any incoming (new or updated) modules will be registered.
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.register Function to register accepted modules.
* @param {Function} options.unregister Function to unregister replaced modules.
* @param {Function} options.[before] Function to run before updating modules.
* @param {Function} options.[after] Function to run after updating modules.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
var autoload = function autoload(_ref) {
var getContext = _ref.getContext,
register = _ref.register,
unregister = _ref.unregister,
_ref$before = _ref.before,
before = _ref$before === void 0 ? noop : _ref$before,
_ref$after = _ref.after,
after = _ref$after === void 0 ? noop : _ref$after;
var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
var cache = {};
var loadModules = function loadModules() {
before();
var context = getContext();
var changed = [];
context.keys().forEach(function (key) {
var module = context(key);
if (module === cache[key]) {
// Module unchanged: no further action needed.
return;
}
var isHotUpdate = cache[key];
if (isHotUpdate && console.groupCollapsed) {
console.groupCollapsed("hot update: ".concat(key));
}
if (isHotUpdate) {
// Module changed, and prior copy detected: unregister old module.
unregister(cache[key]);
}
// Register new module and update cache.
register(module);
changed.push(module);
cache[key] = module;
if (isHotUpdate && console.groupCollapsed) {
console.groupEnd();
}
});
after(changed);
// Return the context for HMR initialization.
return context;
};
var context = loadModules();
callback(context, loadModules);
};
// Maintain the selected block ID across HMR updates.
exports.autoload = autoload;
var selectedBlockId = null;
/**
* Register a new or updated block, filters, or style variations.
*
* @param {Object} block The exported block module.
* @param {String} block.name Block name. May be included in configuration object.
* @param {Object} [block.settings] Optional block configuration object.
* @param {Object[]} [block.filters] Optional array of filters to bind.
* @param {Object[]} [block.styles] Optional array of block styles to bind.
*/
var registerBlock = function registerBlock(_ref2) {
var name = _ref2.name,
settings = _ref2.settings,
filters = _ref2.filters,
styles = _ref2.styles;
if ((name || settings !== null && settings !== void 0 && settings.name) && settings) {
blocks.registerBlockType(name || (settings === null || settings === void 0 ? void 0 : settings.name), settings);
}
if (filters && Array.isArray(filters)) {
filters.forEach(function (_ref3) {
var hook = _ref3.hook,
namespace = _ref3.namespace,
callback = _ref3.callback;
hooks.addFilter(hook, namespace, callback);
});
}
if (styles && Array.isArray(styles)) {
styles.forEach(function (style) {
return blocks.registerBlockStyle(name, style);
});
}
};
/**
* Unregister an updated or removed block, filters, or style variations.
*
* @param {Object} block The exported block module.
* @param {String} block.name Block name. May be included in configuration object.
* @param {Object} [block.settings] Optional block configuration object.
* @param {Object[]} [block.filters] Optional array of filters to bind.
* @param {Object[]} [block.styles] Optional array of block styles to bind.
*/
exports.registerBlock = registerBlock;
var unregisterBlock = function unregisterBlock(_ref4) {
var name = _ref4.name,
settings = _ref4.settings,
filters = _ref4.filters,
styles = _ref4.styles;
if ((name || settings !== null && settings !== void 0 && settings.name) && settings) {
blocks.unregisterBlockType(name || (settings === null || settings === void 0 ? void 0 : settings.name));
}
if (filters && Array.isArray(filters)) {
filters.forEach(function (_ref5) {
var hook = _ref5.hook,
namespace = _ref5.namespace;
hooks.removeFilter(hook, namespace);
});
}
if (styles && Array.isArray(styles)) {
styles.forEach(function (style) {
return blocks.unregisterBlockStyle(name, style.name);
});
}
};
/**
* Store the selected block to persist selection across block-swaps.
*/
exports.unregisterBlock = unregisterBlock;
var beforeUpdateBlocks = function beforeUpdateBlocks() {
selectedBlockId = data.select('core/block-editor').getSelectedBlockClientId();
data.dispatch('core/block-editor').clearSelectedBlock();
};
/**
* Trigger a re-render on all blocks which have changed.
*
* @param {Object[]} changed Array of changed module objects.
*/
exports.beforeUpdateBlocks = beforeUpdateBlocks;
var afterUpdateBlocks = function afterUpdateBlocks() {
var changed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var changedNames = changed.map(function (module) {
return module.name;
});
if (!changedNames.length) {
return;
}
// Refresh all blocks by iteratively selecting each one that has changed.
data.select('core/block-editor').getBlocks().forEach(function (_ref6) {
var name = _ref6.name,
clientId = _ref6.clientId;
if (changedNames.includes(name)) {
data.dispatch('core/block-editor').selectBlock(clientId);
}
});
// Reselect whatever was selected in the beginning.
if (selectedBlockId) {
data.dispatch('core/block-editor').selectBlock(selectedBlockId);
} else {
data.dispatch('core/block-editor').clearSelectedBlock();
}
selectedBlockId = null;
};
/**
* Require a set of blocks and configure them for hot module replacement.
*
* @see autoload
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.[register] Function to register accepted blocks.
* @param {Function} options.[unregister] Function to unregister replaced blocks.
* @param {Function} options.[before] Function to run before updating blocks.
* @param {Function} options.[after] Function to run after updating blocks.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
exports.afterUpdateBlocks = afterUpdateBlocks;
var autoloadBlocks = function autoloadBlocks(_ref7, callback) {
var getContext = _ref7.getContext,
_ref7$register = _ref7.register,
register = _ref7$register === void 0 ? registerBlock : _ref7$register,
_ref7$unregister = _ref7.unregister,
unregister = _ref7$unregister === void 0 ? unregisterBlock : _ref7$unregister,
_ref7$before = _ref7.before,
before = _ref7$before === void 0 ? beforeUpdateBlocks : _ref7$before,
_ref7$after = _ref7.after,
after = _ref7$after === void 0 ? afterUpdateBlocks : _ref7$after;
autoload({
getContext: getContext,
register: register,
unregister: unregister,
before: before,
after: after
}, callback);
};
/**
* Register a new or updated plugin.
*
* @param {Object} plugin The exported plugin module.
* @param {String} plugin.name Plugin name.
* @param {Object} plugin.settings Plugin configuration object.
* @param {Object[]} [plugin.filters] Optional array of filters to bind.
*/
exports.autoloadBlocks = autoloadBlocks;
var registerPlugin = function registerPlugin(_ref8) {
var name = _ref8.name,
settings = _ref8.settings,
filters = _ref8.filters;
if (name && settings) {
plugins.registerPlugin(name, settings);
}
if (filters && Array.isArray(filters)) {
filters.forEach(function (_ref9) {
var hook = _ref9.hook,
namespace = _ref9.namespace,
callback = _ref9.callback;
hooks.addFilter(hook, namespace, callback);
});
}
};
/**
* Unregister an updated or removed plugin.
*
* @param {Object} plugin The exported plugin module.
* @param {String} plugin.name Plugin name.
* @param {Object} plugin.settings Plugin configuration object.
* @param {Object[]} [plugin.filters] Optional array of filters to bind.
*/
exports.registerPlugin = registerPlugin;
var unregisterPlugin = function unregisterPlugin(_ref10) {
var name = _ref10.name,
settings = _ref10.settings,
filters = _ref10.filters;
if (name && settings) {
plugins.unregisterPlugin(name);
}
if (filters && Array.isArray(filters)) {
filters.forEach(function (_ref11) {
var hook = _ref11.hook,
namespace = _ref11.namespace;
hooks.removeFilter(hook, namespace);
});
}
};
/**
* Require a set of plugins and configure them for hot module replacement.
*
* @see autoload
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.[register] Function to register accepted plugins.
* @param {Function} options.[unregister] Function to unregister replaced plugins.
* @param {Function} options.[before] Function to run before updating plugins.
* @param {Function} options.[after] Function to run after updating plugins.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
exports.unregisterPlugin = unregisterPlugin;
var autoloadPlugins = function autoloadPlugins(_ref12, callback) {
var getContext = _ref12.getContext,
_ref12$register = _ref12.register,
register = _ref12$register === void 0 ? registerPlugin : _ref12$register,
_ref12$unregister = _ref12.unregister,
unregister = _ref12$unregister === void 0 ? unregisterPlugin : _ref12$unregister,
before = _ref12.before,
after = _ref12.after;
autoload({
getContext: getContext,
register: register,
unregister: unregister,
before: before,
after: after
}, callback);
};
/**
* Register a new or updated format type
*
* @param {Object} format The exported format module.
* @param {String} format.name Format type name.
* @param {Object} format.settings Format type configuration object.
*/
exports.autoloadPlugins = autoloadPlugins;
var registerFormat = function registerFormat(_ref13) {
var name = _ref13.name,
settings = _ref13.settings;
if (name && settings) {
richText.registerFormatType(name, settings);
}
};
/**
* Unregister an updated or removed format type.
*
* @param {Object} format The exported format module.
* @param {String} format.name Format type name.
* @param {Object} format.settings Format type configuration object.
*/
exports.registerFormat = registerFormat;
var unregisterFormat = function unregisterFormat(_ref14) {
var name = _ref14.name,
settings = _ref14.settings;
if (name && settings) {
richText.unregisterFormatType(name);
}
};
/**
* Require a set of format types and configure them for hot module replacement.
*
* @see autoload
*
* @param {Object} options Configuration object defining callbacks.
* @param {Function} options.getContext Execute and return a `require.context()` call.
* @param {Function} options.[register] Function to register accepted formats.
* @param {Function} options.[unregister] Function to unregister replaced formats.
* @param {Function} options.[before] Function to run before updating formats.
* @param {Function} options.[after] Function to run after updating formats.
* @param {Function} [callback] A callback function which will be passed the
* generated `context` object and `loadModules`
* function, which can be used to opt-in to HMR.
*/
exports.unregisterFormat = unregisterFormat;
var autoloadFormats = function autoloadFormats(_ref15, callback) {
var getContext = _ref15.getContext,
_ref15$register = _ref15.register,
register = _ref15$register === void 0 ? registerFormat : _ref15$register,
_ref15$unregister = _ref15.unregister,
unregister = _ref15$unregister === void 0 ? unregisterFormat : _ref15$unregister,
before = _ref15.before,
after = _ref15.after;
autoload({
getContext: getContext,
register: register,
unregister: unregister,
before: before,
after: after
}, callback);
};
/* eslint-disable no-underscore-dangle */
/* eslint-disable camelcase */
/**
* Work around a full-page crash in WordPress 5.4 caused by a forced render of
* the BlockListBlock component following the state dispatch triggered upon block
* unregistration.
*
* This function filters the BlockListBlock component to wrap it in an error
* boundary, which catches the error when BlockListBlock tries to access a
* property on the removed block type, suppresses the error by returning null,
* and then schedules the BlockListBlock to try rendering again on the next
* tick (by which point our hot-swapped block type should be available again).
*/
exports.autoloadFormats = autoloadFormats;
var _apply_wp_5_4_hmr_patch = function _apply_wp_5_4_hmr_patch() {
/* eslint-enable */
var React = window.React;
var Component = React.Component,
Fragment = React.Fragment,
createElement = React.createElement;
hooks.addFilter('editor.BlockListBlock', 'block-editor-hmr/prevent-block-swapping-error', function (BlockListBlock) {
var ErrorWrapper = /*#__PURE__*/function (_Component) {
_inherits(ErrorWrapper, _Component);
var _super = _createSuper(ErrorWrapper);
function ErrorWrapper(props) {
var _this;
_classCallCheck(this, ErrorWrapper);
_this = _super.call(this, props);
_this.state = {
hasError: false
};
return _this;
}
// eslint-disable-next-line no-unused-vars
_createClass(ErrorWrapper, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this2 = this;
if (this.state.hasError && this.state.hasError !== prevState.hasError) {
setTimeout(function () {
_this2.setState({
hasError: false
});
});
}
}
}, {
key: "render",
value: function render() {
if (this.state.hasError) {
return null;
}
return createElement(Fragment, null, createElement(BlockListBlock, this.props));
}
}], [{
key: "getDerivedStateFromError",
value: function getDerivedStateFromError(error) {
return {
hasError: true
};
}
}]);
return ErrorWrapper;
}(Component);
return ErrorWrapper;
});
};
exports._apply_wp_5_4_hmr_patch = _apply_wp_5_4_hmr_patch;