'', '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($asset_path, 'runtime') || strstr($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)/', $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 = []) { ?>
Failed to render
Unable to find React asset manifest. react-wp-scripts was unable to find either a development or production asset manifest. Run npm start to start the development server or npm run build to build a production bundle.