wordpress - 在进行 Webpack-Dev-Server HMR 时在 WordPress 中排队脚本
问题描述
我正在努力成为一个优秀的 WordPress 公民,并以正确的方式包含我的 JavaScript,通过wp_enqueue_scripts
在我的functions.php
.
但是,这样做我不会通过webpack-dev-server进行热模块重新加载(hmr)工作。
任何人都可以给我一个提示或指向我一些文档吗?
解决方案
这里没有反应,所以我不得不自己寻找答案。这里是。
我没有得到的是如何使webpack-dev-server仅在内存中可用的bundle.js
文件在.wp_enqueue_scripts
functions.php
我的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-server 代理 dev-sever,但在文件下运行http://wordpress:8888
的build.js
webpack -dev-server不能在原始 url下http://wordpress:8888/build.js
但在原始 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
所以现在只需在I can WordPress make 中添加一行来查找bundle.js
文件,webpack-dev-server将它放在其中。如果缺少此行,它将bundle.js
从主题目录的根目录加载文件。
我的wp-config.php(提取)
define('WP_ENVIRONMENT', 'development');
推荐阅读
- python - 如何在使用 YAML(无服务器)部署具有胶水版本 1.0 的 AWS 胶水作业时选择 python 版本 3
- php - 看懂C源码,rand的PHP实现
- jquery - 在边缘浏览器中的 asp.net mvc 验证错误中使用 jQuery datepicker
- python - 如何在不使用事件的情况下使用 Discord.py 发表评论?
- r - 编写自定义函数时将列名传递给 dplyr::coalesce()
- java - 如何在 Java 中将每个标签与其 Textfield 旁边的对齐?
- python - AttributeError:模块“asyncio.coroutines”没有属性“_is_coroutine”
- ios - Core Data 使用 Canvas 预览的要求是什么 - SwiftUI
- c# - Newtonsoft.Json - 反序列化自定义类型时抛出 DeserializeObject:将值“somestring”转换为类型 CustomType 时出错
- sql - 在数据库“天气”上执行 SQL 查询时出错:“(”附近:语法错误