如何将客户端webpack包部署到生产中?

时间:2017-04-27 05:34:41

标签: javascript node.js webpack digital-ocean webpack-dev-server

我目前正在将应用部署到我的数字海洋nodejs环境中,它是一个仅限客户端的捆绑包,与API通信。到目前为止,我一直在使用devserver进行开发,但我猜这不是用于生产,因为我的捆绑包很大(1.8 MB)。

这是我的devserver设置:

var
    WebpackDevServer = require('webpack-dev-server'),
    webpack = require('webpack'),
    config = require('./webpack.config');

const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
    historyApiFallback: true,
    compress: true,
    inline: true,
    hot: true
});

server.listen(8000);

我的webpack配置:

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

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'app.js',
  },
  devServer: {
      historyApiFallback:{
          index:'/index.html'
      },
  },
  resolveLoader: {
    modules: ['node_modules'],
  },
  module: {
    loaders: [
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file',
        query: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.html$/,
        loader: 'wc-loader'
      },
    ],
  },
  devtool: 'eval-source-map'
};

我现在正在尝试减少捆绑包,我想我需要停止使用devserver吗?到目前为止我所拥有的(使用devserver但配置不同):

var
WebpackDevServer = require('webpack-dev-server'),
webpack = require('webpack'),
config = require('./production_webpack.config');

const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
    historyApiFallback: true,
    compress: true,
    inline: true,
    hot: true
});

server.listen(8000);

我的生产配置:

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

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'app.js',
    sourceMapFilename: '[name].map'
  },
  resolveLoader: {
    modules: ['node_modules'],
  },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
    new webpack.optimize.UglifyJsPlugin({
      beautify: false,
      mangle: {
        screw_ie8: true,
        keep_fnames: true
      },
      compress: {
        screw_ie8: true
      },
      comments: false
    })
  ],
  module: {
    loaders: [
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file',
        query: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
        test: /\.html$/,
        loader: 'wc-loader'
      },
    ],
  },
  devtool: 'source-map'
};

0 个答案:

没有答案