forked from humanmade/react-wp-scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloader.php
300 lines (262 loc) · 7.35 KB
/
loader.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
<?php
/**
* Entrypoint for the theme.
*/
namespace %%NAMESPACE%%;
/**
* Is this a development environment?
*
* @return bool
*/
function is_development() {
return apply_filters( 'reactwpscripts.is_development', WP_DEBUG );
}
/**
* 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( string $directory ) {
$directory = trailingslashit( $directory );
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 array_values( $dev_assets );
}
}
$production_assets = load_asset_file( $directory . 'build/asset-manifest.json' );
if ( ! empty( $production_assets ) ) {
// Prepend "build/" to all build-directory array paths.
return array_map(
function( $asset_path ) { return 'build/' . $asset_path; },
array_values( $production_assets )
);
}
return null;
}
/**
* 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( string $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( string $asset_path, string $base_url ) {
if ( strpos( $asset_path, '://' ) !== false ) {
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 ),
];
$opts = wp_parse_args( $opts, $defaults );
$assets = get_assets_list( $directory );
$base_url = $opts['base_url'];
if ( empty( $base_url ) ) {
$base_url = infer_base_url( $directory );
}
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;
}
// 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_chunk = preg_match( '/\.chunk\./', $asset_path );
if ( ( ! $is_js && ! $is_css ) || $is_chunk ) {
// Assets such as source maps and images are also listed; ignore these.
continue;
}
if ( $is_js ) {
wp_enqueue_script(
$opts['handle'],
get_asset_uri( $asset_path, $base_url ),
$opts['scripts'],
null,
true
);
} else if ( $is_css ) {
$has_css = true;
wp_enqueue_style(
$opts['handle'],
get_asset_uri( $asset_path, $base_url ),
$opts['styles']
);
}
}
// 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();
}