-
Notifications
You must be signed in to change notification settings - Fork 515
/
react-wp-scripts.php
404 lines (346 loc) · 10.4 KB
/
react-wp-scripts.php
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
<?php
/**
* Entrypoint for the theme.
*/
namespace ReactWPScripts;
/**
* Is this a development environment?
*
* @return bool
*/
function is_development()
{
if (defined('WPMDB_REACT_SCRIPTS_IS_DEV')) {
return WPMDB_REACT_SCRIPTS_IS_DEV;
}
$env = isset($_ENV['MDB_IS_DEV']) ? (bool)$_ENV['MDB_IS_DEV'] : false;
return apply_filters('reactwpscripts.is_development', $env);
}
function switch_slashes_for_windows($path)
{
return str_replace('\\', '/', $path);
}
function get_build_folder_name()
{
if (defined('WPMDB_PRO') && WPMDB_PRO) {
return 'build';
}
return 'build-free';
}
/**
* Attempt to load a file at the specified path and parse its contents as JSON.
*
* @param string $path The path to the JSON file to load.
*
* @return array|null;
*/
function load_asset_file($path)
{
if (!file_exists($path)) {
return null;
}
$contents = file_get_contents($path);
if (empty($contents)) {
return null;
}
return json_decode($contents, true);
}
/**
* Check a directory for a root or build asset manifest file, and attempt to
* decode and return the asset list JSON if found.
*
* @param string $directory Root directory containing `src` and `build` directory.
*
* @return array|null;
*/
function get_assets_list($directory, $base_url)
{
$directory = trailingslashit($directory);
$build_folder = get_build_folder_name();
if (is_development()) {
$dev_assets = load_asset_file($directory . 'asset-manifest.json');
// Fall back to build directory if there is any error loading the development manifest.
if (!empty($dev_assets)) {
return filter_assets_list($dev_assets);
}
}
$production_assets = load_asset_file($directory . $build_folder . '/asset-manifest.json');
if (!empty($production_assets)) {
// Prepend "build/" to all build-directory array paths.
$list = filter_assets_list(
array_map(
function ($asset_path) use ($directory, $base_url, $build_folder) {
// Use realpath to remove default relative path and confirm file exists.
$real_path = realpath($directory . $build_folder . '/' . $asset_path);
// Remove path to plugins dir to avoid problems where site path includes build folder name.
$str = str_replace(WP_PLUGIN_DIR, '', $real_path);
// Get things into a format we can enqueue.
$str = substr($str, strpos($str, $build_folder . DIRECTORY_SEPARATOR));
$str = switch_slashes_for_windows($str);// Windows fix
$formatted = $base_url . '/' . $str;
return $formatted;
},
$production_assets
)
);
return $list;
}
return null;
}
/**
* Filter the assets to remove all async chunks, the service worker and precache manifest.
*
* @param array $assets
*
* @return array
*/
function filter_assets_list($assets)
{
return array_filter(
$assets,
function ($asset_path) {
return !preg_match('/precache-manifest|service-worker/', $asset_path);
}
);
}
/**
* Infer a base web URL for a file system path.
*
* @param string $path Filesystem path for which to return a URL.
*
* @return string|null
*/
function infer_base_url($path)
{
$path = wp_normalize_path($path);
$stylesheet_directory = wp_normalize_path(get_stylesheet_directory());
if (strpos($path, $stylesheet_directory) === 0) {
return get_theme_file_uri(substr($path, strlen($stylesheet_directory)));
}
$template_directory = wp_normalize_path(get_template_directory());
if (strpos($path, $template_directory) === 0) {
return get_theme_file_uri(substr($path, strlen($template_directory)));
}
// Any path not known to exist within a theme is treated as a plugin path.
$plugin_path = get_plugin_basedir_path();
if (strpos($path, $plugin_path) === 0) {
return plugin_dir_url(__FILE__) . substr($path, strlen($plugin_path) + 1);
}
return '';
}
/**
* Return the path of the plugin basedir.
*
* @return string
*/
function get_plugin_basedir_path()
{
$plugin_dir_path = wp_normalize_path(plugin_dir_path(__FILE__));
$plugins_dir_path = wp_normalize_path(trailingslashit(WP_PLUGIN_DIR));
return substr($plugin_dir_path, 0, strpos($plugin_dir_path, '/', strlen($plugins_dir_path) + 1));
}
/**
* Return web URIs or convert relative filesystem paths to absolute paths.
*
* @param string $asset_path A relative filesystem path or full resource URI.
* @param string $base_url A base URL to prepend to relative bundle URIs.
*
* @return string
*/
function get_asset_uri($asset_path, $base_url)
{
// If it has a URL scheme, or is a relative URL as defined via WP_CONTENT_DIR or similar.
if (strpos($asset_path, '://') !== false || plugins_url() === substr($asset_path, 0, strlen(plugins_url()))) {
return $asset_path;
}
return trailingslashit($base_url) . $asset_path;
}
/**
* @param string $directory Root directory containing `src` and `build` directory.
* @param array $opts {
*
* @type string $base_url Root URL containing `src` and `build` directory. Only needed for production.
* @type string $handle Style/script handle. (Default is last part of directory name.)
* @type array $scripts Script dependencies.
* @type array $styles Style dependencies.
* }
*/
function enqueue_assets($directory, $opts = [])
{
$defaults = [
'base_url' => '',
'handle' => basename($directory),
'scripts' => [
'wp-date',
],
'styles' => [],
'key' => '',
];
$opts = wp_parse_args($opts, $defaults);
$base_url = $opts['base_url'];
if (empty($base_url)) {
$base_url = infer_base_url($directory);
}
$assets = get_assets_list($directory, $base_url);
if (empty($assets)) {
if (WP_DEBUG) {
handle_assets_error();
}
trigger_error('React WP Scripts Error: Unable to find React asset manifest', E_USER_WARNING);
return;
}
// Make runtime / bundle first up.
uksort(
$assets,
function ($asset_path) {
if (strstr(basename($asset_path), 'runtime') || strstr(basename($asset_path), 'bundle')) {
return -1;
}
return 1;
}
);
// There will be at most one JS and one CSS file in vanilla Create React App manifests.
$has_css = false;
foreach ($assets as $asset_path) {
$is_js = preg_match('/\.js$/', $asset_path);
$is_css = preg_match('/\.css$/', $asset_path);
$is_runtime = preg_match('/(runtime|bundle)/', basename($asset_path));
if (!$is_js && !$is_css) {
// Assets such as source maps and images are also listed; ignore these.
continue;
}
// Set a dynamic handle as we can have more than one JS entry point.
// Treats the runtime file as primary to make setting dependencies easier.
$handle = $opts['handle'] . ($is_runtime ? '' : '-' . sanitize_key(basename($asset_path)));
if ($is_js) {
wp_enqueue_script(
$handle,
get_asset_uri($asset_path, $base_url),
$opts['scripts'],
null,
true
);
} elseif ($is_css) {
$has_css = true;
wp_enqueue_style(
$handle,
get_asset_uri($asset_path, $base_url),
$opts['styles']
);
}
}
$build_folder = get_build_folder_name();
// Add the generated public path to the build directory.
wp_add_inline_script(
$opts['handle'],
sprintf('var reactpluginBuildURL%s = %s;', $opts['key'], wp_json_encode($base_url . "/{$build_folder}/")),
'before'
);
// Ensure CSS dependencies are always loaded, even when using CSS-in-JS in
// development.
if (!$has_css) {
wp_register_style(
$opts['handle'],
null,
$opts['styles']
);
wp_enqueue_style($opts['handle']);
}
}
/**
* Display an overlay error when the React bundle cannot be loaded. It also stops the execution.
*
* @param array $details
*/
function handle_assets_error($details = [])
{
?>
<style>
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* @flow */
html, body {
overflow: hidden;
}
.error-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: none;
z-index: 1000;
--black: #293238;
--dark-gray: #878e91;
--red: #ce1126;
--red-transparent: rgba(206, 17, 38, 0.05);
--light-red: #fccfcf;
--yellow: #fbf5b4;
--yellow-transparent: rgba(251, 245, 180, 0.3);
--white: #ffffff;
}
.error-overlay .wrapper {
width: 100%;
height: 100%;
box-sizing: border-box;
text-align: center;
background-color: var(--white);
}
.primaryErrorStyle {
background-color: var(--red-transparent);
}
.error-overlay .overlay {
position: relative;
display: inline-flex;
flex-direction: column;
height: 100%;
width: 1024px;
max-width: 100%;
overflow-x: hidden;
overflow-y: auto;
padding: 0.5rem;
box-sizing: border-box;
text-align: left;
font-family: Consolas, Menlo, monospace;
font-size: 13px;
line-height: 2;
color: var(--black);
}
.header {
font-size: 2em;
font-family: sans-serif;
color: var(--red);
white-space: pre-wrap;
margin: 0 2rem 0.75rem 0;
flex: 0 0 auto;
max-height: 50%;
overflow: auto;
}
.error-content {
padding: 1rem;
}
code {
background-color: rgba(27, 31, 35, .05);
margin: 0;
padding: .2em .4em;
}
</style>
<div class="error-overlay">
<div class="wrapper primaryErrorStyle">
<div class="overlay">
<div class="header">Failed to render</div>
<div class="error-content primaryErrorStyle">
Unable to find React asset manifest.
<code>react-wp-scripts</code> was unable to find either a development or production asset manifest.
Run <code>npm start</code> to start the development server or <code>npm run build</code> to build a production bundle.
</div>
</div>
</div>
</div>
<?php
die();
}