使用我使用 webpack -p 编译的快速服务器将构建部署到生产环境

Vik*_*kas 2 deployment production config reactjs webpack

我想将我的应用程序部署到生产环境。我不知道在使用 webpack -p 构建我的包后该怎么做。如何使用快速节点服务器在生产中提供此包。

我的 webpack.config.js 文件

var HtmlWebpackPlugin = require('html-webpack-plugin')
var debug = process.env.NODE_ENV !== "production";
var webpack = require("webpack");

var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  devtool: debug ? "inline-sourcemap" : null,
  entry: [
    './app/index.js'
  ],
  output: {
    path: __dirname + '/dist',
    publicPath: '/',
    filename: "index_bundle.js"
  },
  module: {
    loaders: [
      {test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
    ]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  plugins: debug ? [HTMLWebpackPluginConfig] : [
    HTMLWebpackPluginConfig,
    new webpack.optimize.CommonsChunkPlugin('common.js'),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
    new webpack.optimize.AggressiveMergingPlugin()
  ],
};
Run Code Online (Sandbox Code Playgroud)

我的 package.json 文件

{
  "name": "my-web",
  "version": "1.0.0",
  "description": "Practicing react-website",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --hot --port 8080 --host 0.0.0.0 --content-base dist/ --history-api-fallback",
    "prod": "NODE_ENV=production webpack -p",
    "postinstall": "npm start"
  },
  "dependencies": {
    "axios": "^0.15.0",
    "express": "^4.14.0",
    "radium": "^0.18.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-router": "^2.8.1"
  },
  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.5",
    "babel-preset-react": "^6.16.0",
    "html-webpack-plugin": "^2.22.0",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:npm start 在本地主机上运行得很好。所以我使用 webpack -p 在 ./dist 文件夹中创建包。需要从这里开始。

还赞赏关于更好的部署方式的建议。

Ped*_*oso 5

您现在需要使用 express 提供 dist 文件夹的内容,这是一个基本实现,您可以将其用作示例:

在您的 .js 文件中创建一个名为 app.js 的文件。文件夹(在您 dist 文件夹所在的同一文件夹中)

应用程序.js

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

var app = express();

app.use(express.static(path.join(__dirname, '/dist')));

app.get('/*', function(req, res){
  res.sendfile("index.html", {root: path.join(__dirname, '/dist')});
});

app.listen(80, function() {
  console.log("App is running at localhost: 80")
});
Run Code Online (Sandbox Code Playgroud)

然后,运行node app.js,如果出现 EACCES 错误,请改为运行sudo node app.js。这会在http://localhost本地运行您的生产文件。

如果您想将其部署到其他地方(例如,heroku https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction),您必须查看他们的说明。