这里没有反应,所以我不得不自己寻找答案。 ?
在这里。
我没有得到的是如何使 webpack-dev-server 仅在内存中可用的 bundle.js 文件,在 functions.php 中使用 wp_enqueue_scripts 可供 WordPress 使用。
我的 webpack.config.js(摘录)
const webpack = require('webpack');
module.exports = {
entry: './src/entry.js',
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
},
devServer: {
open: true,
hot: true,
publicPath: '/',
proxy: {
'/': {
target: 'http://wordpress:8888/',
changeOrigin: true
}
}
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
问题是:虽然我通过我的 MAMP 服务器代理了开发服务器,它在 http://wordpress:8888 下运行,build.js 文件在 @ 下的 webpack-dev-server 不可用987654328@ 但在原始 url 下,即http://localhost:8080/build.js。
一旦我发现functions.php 中的条件语句就可以解决问题。
我的functions.php(摘录)
<?php
// Load my JS
if (!defined('WP_ENVIRONMENT') || WP_ENVIRONMENT == "production") {
function reactTheme_enque_scripts() {
wp_enqueue_script(
'react-theme-js',
get_stylesheet_directory_uri() . '/bundle.js',
[], // dependencies could go here
time(), // version for caching
true // loading it within footer
);
}
} else {
function reactTheme_enque_scripts() {
wp_enqueue_script(
'react-theme-js',
'http://localhost:8080' . '/bundle.js',
[], // dependencies could go here
time(), // version for caching
true // loading it within footer
);
}
}
add_action('wp_enqueue_scripts', 'reactTheme_enque_scripts');
?>
所以现在只需在wp-config.php 中添加一行,我就可以在 WordPress 中查找 bundle.js 文件,webpack-dev-server 将它放在其中。
如果缺少这一行,它会从主题目录的根目录加载bundle.js 文件。
我的 wp-config.php(摘录)
define('WP_ENVIRONMENT', 'development');