webpack.prod.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @author: Yuki Takei <yuki@weseek.co.jp>
  3. */
  4. const helpers = require('./helpers');
  5. const webpack = require('webpack');
  6. const webpackMerge = require('webpack-merge'); // used to merge webpack configs
  7. const commonConfig = require('./webpack.common.js'); // the settings that are common to prod and dev
  8. /**
  9. * Webpack Plugins
  10. */
  11. const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
  12. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  13. const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
  14. const OptimizeJsPlugin = require('optimize-js-plugin');
  15. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  16. /**
  17. * Webpack Constants
  18. */
  19. const ANALYZE = process.env.ANALYZE;
  20. const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
  21. const HOST = process.env.HOST || 'localhost';
  22. const PORT = process.env.PORT || 3000;
  23. module.exports = function (env) {
  24. return webpackMerge(commonConfig({ env: ENV }), {
  25. devtool: undefined,
  26. output: {
  27. path: helpers.root('public/js'),
  28. publicPath: '/js/',
  29. filename: '[name].[chunkhash].bundle.js',
  30. sourceMapFilename: '[name].[chunkhash].bundle.map',
  31. chunkFilename: '[id].[chunkhash].chunk.js'
  32. },
  33. module: {
  34. rules: [
  35. {
  36. test: /\.scss$/,
  37. use: ExtractTextPlugin.extract({
  38. fallback: 'style-loader',
  39. use: [
  40. { loader: 'css-loader', options: {
  41. sourceMap: false,
  42. minimize: true
  43. } },
  44. { loader: 'postcss-loader', options: {
  45. sourceMap: false,
  46. plugins: (loader) => [
  47. require('autoprefixer')()
  48. ]
  49. } },
  50. { loader: 'sass-loader', options: { sourceMap: false } }
  51. ]
  52. }),
  53. include: [helpers.root('resource/styles/scss')]
  54. }
  55. ]
  56. },
  57. plugins: [
  58. new webpack.DefinePlugin({
  59. 'process.env': {
  60. NODE_ENV: JSON.stringify(ENV),
  61. }
  62. }),
  63. new ExtractTextPlugin('[name].[contenthash].css'),
  64. new OptimizeJsPlugin({
  65. sourceMap: false
  66. }),
  67. new UglifyJsPlugin({
  68. // beautify: true, //debug
  69. // mangle: false, //debug
  70. // dead_code: false, //debug
  71. // unused: false, //debug
  72. // deadCode: false, //debug
  73. // compress: {
  74. // screw_ie8: true,
  75. // keep_fnames: true,
  76. // drop_debugger: false,
  77. // dead_code: false,
  78. // unused: false
  79. // }, // debug
  80. // comments: true, //debug
  81. beautify: false, //prod
  82. output: {
  83. comments: false
  84. }, //prod
  85. mangle: {
  86. screw_ie8: true
  87. }, //prod
  88. compress: {
  89. screw_ie8: true,
  90. warnings: false,
  91. conditionals: true,
  92. unused: true,
  93. comparisons: true,
  94. sequences: true,
  95. dead_code: true,
  96. evaluate: true,
  97. if_return: true,
  98. join_vars: true,
  99. negate_iife: false // we need this for lazy v8
  100. },
  101. }),
  102. new BundleAnalyzerPlugin({
  103. analyzerMode: ANALYZE ? 'static' : 'disabled',
  104. reportFilename: helpers.root('report/bundle-analyzer.html'),
  105. openAnalyzer: false,
  106. }),
  107. ],
  108. });
  109. }