node.js - 如何在生产环境中使用 nginx 服务 webpack 构建文件

标签 node.js nginx webpack vuejs2

我写了一个 vue + webpack 项目,它在 webpack-dev-middleware 中运行良好。现在我想用 nginx 部署它。我所做的是编写一个 webpack.build.config.js 并将所有文件捆绑到一个 dist 文件夹中。然后我只需将 dist 文件夹复制到 nginx html 文件夹并在 nginx.conf 中分配索引。但是,它有一个错误说:

[Vue warn]: Failed to mount component: template or render function not defined. (found in root instance)

我是开发运维/后端的新手,对整个构建或部署过程非常困惑。生产环境还需要webpack-dev-server或者nodejs吗?我的生产环境后端是nginx/PHP和IIS/.Net,现在完全没有安装node。

我的nginx.conf是

location / {
    root   html/dist;
    index  index.html index.htm;
}

而 webpack.build.config.js 是

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var public_dir = "components";
//var ModernizrWebpackPlugin = require('modernizr-webpack-plugin');

module.exports = {
    entry: [
        path.join(__dirname,'./index.js')
    ],
    output: {
        path: path.join(__dirname, '/dist/'),
        filename: '[name].js',
        publicPath: '/'
    },
    devtool: 'eval-source-map',
    plugins: [
        new webpack.optimize.CommonsChunkPlugin('common.js'),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.UglifyJsPlugin(),
        new webpack.optimize.AggressiveMergingPlugin(),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.resolve(__dirname, 'index.html'),
            inject: true
        })
    ],
    resolve: {
        root: [path.resolve('./components')],
        extensions: ['', '.js', '.css']
    },
    module: {
        loaders: [
        ]
    }
};

构建时运行

webpack -p --config ./webpack.build.config.js

最佳答案

我正在使用 vue-cli初始化 vuejs webpack 项目。并且该项目已经有构建脚本,您可以引用它:

require('./check-versions')()

process.env.NODE_ENV = 'production'

var ora = require('ora')
var rm = require('rimraf')
var path = require('path')
var chalk = require('chalk')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')

var spinner = ora('building for production...')
spinner.start()

rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  if (err) throw err
  webpack(webpackConfig, function (err, stats) {
    spinner.stop()
    if (err) throw err
    process.stdout.write(stats.toString({
      colors: true,
      modules: false,
      children: false,
      chunks: false,
      chunkModules: false
    }) + '\n\n')

    console.log(chalk.cyan('  Build complete.\n'))
    console.log(chalk.yellow(
      '  Tip: built files are meant to be served over an HTTP server.\n' +
      '  Opening index.html over file:// won\'t work.\n'
    ))
  })
})

构建后,我们将有一个dist 文件夹。将里面的所有文件上传到 Nginx 的 html 文件夹(默认) 配置根路径以使用这样的完整路径:

listen      80;
server_name mydomain www.mydomain;
root /var/www/html;

关于node.js - 如何在生产环境中使用 nginx 服务 webpack 构建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41358094/

相关文章:

NGINX 使用客户端证书 (ssl_verify_client)

javascript - 无法使用 webpack 4 和 babel 7 导入 soap-npm 模块

facebook 和 socket.io node.js

javascript - 外部 js 文件在 Angular 2 组件内不起作用

javascript - 使用函数式方法进行对象转换

reactjs - CRA : Extract particularly big modules into separate chunks?

javascript - Webpack 如何使用 $translatePartialLoader 缓存 bust angular-translate?

angularjs - Firebase - 查询链

c# - 如何在 Ubuntu 上使用 NginX 提供 Blazor 应用程序

nginx - Nginx 上的 send_timeout 选项有什么作用?