node.js - webpack 与 nodejs 一起使用

标签 node.js reactjs webpack

我是 reactjs 的新手。我刚开始学习 reactjs。我在 nodejs 中使用 webpack 时遇到问题。我想创建将运行 webpack 文件的 Node 服务器。我有 webpack 文件:

const {resolve} = require('path');
const webpack = require('webpack');
const validate = require('webpack-validator');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
  const {ifProd, ifNotProd} = getIfUtils(env)

  return validate({
    entry: './index.js',
    context: __dirname,
    output: {
      path: resolve(__dirname, './build'),
      filename: 'bundle.js',
      publicPath: '/build/',
      pathinfo: ifNotProd(),
    },
    devtool: ifProd('source-map', 'eval'),
    devServer: {
      port: 8080,
      historyApiFallback: true
    },
    module: {
      loaders: [
        {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
        {test: /\.css$/, loader: 'style-loader!css-loader'},
        {test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
      ],
    },
    plugins: removeEmpty([
      ifProd(new webpack.optimize.DedupePlugin()),
      ifProd(new webpack.LoaderOptionsPlugin({
        minimize: true,
        debug: false,
        quiet: true,
      })),
      ifProd(new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: '"production"',
        },
      })),
      ifProd(new webpack.optimize.UglifyJsPlugin({
        sourceMap: true,
        compress: {
          screw_ie8: true, // eslint-disable-line
          warnings: false,
        },
      })),
    ])
  });
};

我如何将此配置与 nodejs 一起使用。请帮忙

最佳答案

首先,您的 webpack 配置将不会在 webpack 2+ 上运行,因为 webpack-validator 已被弃用,所以我已将其删除。我建议您全局安装 npm install webpack-dev-server -g 并将其用作您的 React 开发中的服务器。这是您可以修改配置以使用它的方式 (webpack.config.js):

const path = require("path");
const webpack = require('webpack');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
  const {ifProd, ifNotProd} = getIfUtils(env)

  return {
    entry: [
      "webpack-dev-server/client?http://localhost:3003",
      "webpack/hot/only-dev-server",
      "react-hot-loader/patch"
    ],
    context: __dirname,
    output: {
      path: path.join(__dirname, './build'),
      filename: 'bundle.js',
      publicPath: '/build/',
      pathinfo: ifNotProd(),
    },
    devtool: ifProd('source-map', 'eval'),
    devServer: {
        contentBase: path.join(__dirname, "src"),
        // enable HMR
        hot: true,
        // embed the webpack-dev-server runtime into the bundle
        inline: true,
        // serve index.html in place of 404 responses to allow HTML5 history
        historyApiFallback: true,
        port: 3003
    },
    module: {
      loaders: [
        {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
        {test: /\.css$/, loader: 'style-loader!css-loader'},
        {test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
      ],
    },
    plugins: removeEmpty([
    //...
    // same as before
    //...
    ])
  };
};

package.json :

{
  ...
  "dependencies": {},
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "react-hot-loader": "^3.1.1",
    "webpack": "^3.8.1",
    "webpack-config-utils": "^2.3.0",
    "webpack-dev-server": "^2.9.4",
  }
}

在 webpack.config.js 所在的同一个文件夹中创建一个文件 webpack.development.js,它只会设置环境:

var config = require('./webpack.config.js')

module.exports = config("development"); // can be "production" or "development"

文件结构:

root
    build
        bundle.js
    src
        index.html
    index.js
    package.json
    webpack.config.js
    webpack.development.js

最后运行它: webpack-dev-server --config webpack.development.js --progress -p --hot -w

--hot - 将运行服务器, -w - 观看文件

关于node.js - webpack 与 nodejs 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45672066/

相关文章:

php - 登录、 session 、用户管理、日志记录模块

node.js - 如何在node.js环境中将查询语句传递给bigquery

jquery - 在 Reactjs 中使用 RESTful 服务

json - 有什么方法可以在运行时使用 webpack 加载资源?

javascript - JS 脚本在 Pug 和 Jade 文件中的位置重要吗?

reactjs - 使用 React 的 Azure Bot Framework 存在问题

javascript - 渲染没有返回任何内容。这通常意味着缺少返回语句。或者,不渲染任何内容,返回 null

javascript - 如何使用 Webpack 在 Vue.js 单文件组件中导入 json 文件

webpack - 为什么我无法使用 Webpack 编译 SASS?

node.js - Node.js/Express/Passport 是否有像 CanCan for Rails 这样的授权模块?