如何让Webpack,Wordpress和BrowserSync协同工作?

时间:2016-12-20 06:41:58

标签: javascript wordpress reactjs webpack mamp

我已经参加了大约一个星期了,而且我还没能让这三个人一起工作。如果有人能帮助我,我会永远感激,我浪费了这么多时间。

问题:
如果我代理myserver.dev热重装404s。更改publicPath什么都不做。我将网址附加到webpack-hot-middleware / client,它修复了路径,但是hmr文件在控制台中没有信息时出现“GET”错误。如果我保持HTML并忽略任何php / MAMP,热重新加载工作正常。我总体上很困惑,我可能错过了一个简单的概念。

我想要共同努力:
- 用于REST API的Wordpress
- 对意见和ui的反应 - MAMP for localhost& MySQL的
- 用于跨设备测试的BrowserSync
- 用于编译和热重新加载的Webpack

这是我使用的样板: https://github.com/Browsersync/recipes/tree/master/recipes/webpack.react-hot-loader

主题目录结构:
- / INC
- / src目录
- /组件
- /容器
- /风格
--app.js
-bundle.js
-functions.php
-index.php
-package.json
-server.js
-style.css
-webpack.config.js

我已尝试了一百万种配置,所以为了简单起见,我为了下面的代码而掏空了这些代码。

webpack.config.js:

var webpack = require('webpack');
var path = require('path');

module.exports = {
  context: path.join(__dirname, 'src'),

  entry: [
    'webpack/hot/dev-server',
    'webpack-hot-middleware/client',
    './app'
  ],

  output: {
    path: __dirname,
    publicPath: __dirname,
    filename: 'bundle.js'
  },

  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],

  module: {
    loaders: [
      { test: /\.jsx?$/, exclude: /node_modules/, loaders: ['react-hot', 'babel'] }
    ]
  }
};

server.js:

/**
 * Require Browsersync along with webpack and middleware for it
 */
var browserSync = require('browser-sync');
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');

/**
 * Require ./webpack.config.js and make a bundler from it
 */
var webpackConfig = require('./webpack.config');
var bundler = webpack(webpackConfig);

/**
 * Run Browsersync and use middleware for Hot Module Replacement
 */
browserSync({
    proxy: {
      target: 'http://myserver.dev',
      middleware: [
        webpackDevMiddleware(bundler, {
          // IMPORTANT: dev middleware can't access config, so we should
          // provide publicPath by ourselves
          publicPath: webpackConfig.output.publicPath,

          // pretty colored output
          stats: { colors: true }

          // for other settings see
          // http://webpack.github.io/docs/webpack-dev-middleware.html
        }),

        // bundler should be the same as above
        webpackHotMiddleware(bundler)
      ]
    },

    // prevent opening a new window.
    open: false,

    // no need to watch '*.js' here, webpack will take care of it for us,
    // including full page reloads if HMR won't work
    files: [

    ]
});

的package.json:

{
  "main": "server.js",
  "scripts": {
    "build": "webpack",
    "start": "node ."
  },
  "dependencies": {
    "babel-core": "^5.8.9",
    "babel-loader": "^5.3.2",
    "browser-sync": "^2.8.0",
    "react": "^0.13.3",
    "react-hot-loader": "^1.2.8",
    "webpack": "^1.10.5",
    "webpack-dev-middleware": "^1.2.0",
    "webpack-hot-middleware": "^1.1.0"
  }
}

3 个答案:

答案 0 :(得分:5)

到2018年中期,Webpack和BrowserSync for Wordpress的新版本可能已经发生了变化 - 但是我有一个非常简单,现代的Webpack和BrowserSync配方,适用于Word,有JS,CSS,的实时重载和 PHP 。这使用React,但不是特定于React设置,只是ES6模块导入/导出。

文件夹结构:

theme
⊢⊸ api
  ⊢⊸ models
  ⊢⊸ controllers
  ⊢⊸  index.php
⊢⊸ frontend
  ⊢⊸ src
    ⊢⊸ App.js
    ⊢⊸ App.css
    ⊢⊸ index.js
  ⊢⊸ .babelrc
  ⊢⊸ package.json
  ⊢⊸ postcss.config.js
  ⊢⊸ webpack.config.js
  ⊢⊸ yarn.lock
⊢⊸ main.js
⊢⊸ functions.php
⊢⊸ index.php
⊢⊸ style.css

的package.json:

"scripts": {
  "start": "webpack --mode development --watch",
  "build": "webpack --mode production"
},
"devDependencies": {
  "autoprefixer": "^8.5.0",
  "babel-core": "^6.26.3",
  "babel-loader": "^7.1.4",
  "babel-preset-env": "^1.7.0",
  "babel-preset-react": "^6.24.1",
  "browser-sync": "^2.24.4",
  "browser-sync-webpack-plugin": "^2.2.2",
  "css-loader": "^0.28.11",
  "extract-text-webpack-plugin": "^4.0.0-beta.0",
  "postcss-loader": "^2.1.5",
  "react": "^16.4.0",
  "react-dom": "^16.4.0",
  "style-loader": "^0.21.0",
  "webpack": "^4.8.3",
  "webpack-cli": "^2.1.3",
  "webpack-dev-server": "^3.1.4"
}

Webpack.config.json

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');

module.exports = {
  entry: { main: './src/index.js' },
  output: {
    path: path.resolve(__dirname, './../'),
    filename: 'main.js'
  },
  devtool: 'inline-source-map',
  devServer: {
    openPage: '',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract(
          {
            fallback: 'style-loader',
            use: ['css-loader', 'postcss-loader']
          }
        )
      }
    ]
  },
  plugins: [ 
    new ExtractTextPlugin({filename: 'style.css'}),
    new BrowserSyncPlugin({
      files: [
        './../',
        './../api/**/*.php',
        './../api/*.php',
        './', 
        '!./node_modules',
        '!./yarn-error.log',
        '!./package.json',
        '!./style.css.map',
        '!./app.js.map'
      ],
      reloadDelay: 0
    })
  ]
};

Postcss.config.js

module.exports = {
  plugins: [
    require('autoprefixer')
  ]
}

.babelrc

{
  "presets": ["env", "react"]
}

其他建议包括Prettier预先提交(使用Lint-Staged和Husky)用于所有JS项目的代码格式化,适用的.gitignore用法(适用于此处)和ACF Builder用于WP开发者。

答案 1 :(得分:2)

嗯,这不是一个精心设计的答案,但我在Gutenberg Boilerplate中有一个非常基本的Webpack设置,可以帮助您开始使用WordPress中的ESNext,React,Webpack。

查看Block #02及其configuration

答案 2 :(得分:1)

我想通过链接为您解答: https://css-tricks.com/combine-webpack-gulp-4/

本文介绍了解决问题所需的一切。对我来说很棒。它确实使用gulp,但你可以简单地将其从配置中删除并稍微破解。设置的基础知识都在那里。