1. const path = require('path');
    2. const webpack = require('webpack');
    3. const ExtractTextPlugin = require('extract-text-webpack-plugin');
    4. const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
    5. // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
    6. const utils = require('./utils');
    7. const vueConfig = require('./vue-loader.config');
    8. const { ENV_CONFIG } = require('./definition.js');
    9. const isProd = process.env.NODE_ENV === 'production';
    10. module.exports = {
    11. cache: true,
    12. // https://www.cnblogs.com/jkr666666/p/11067189.html
    13. // eval
    14. // 如上打包后的代码,每一个打包后的模块后面都增加了包含sourceURL的注释,sourceURL的值是压缩前存放的代码的位置,这样就通过sourceURL关联了压缩前后的代码。并没有为每一个模块生成相对应的sourcemap。
    15. //优点是:打包速度非常快,因为不需要生成sourcemap文件。
    16. //缺点是:由于会映射到转换后的代码,而不是映射到原始代码,所以不能正确的显示行数
    17. devtool: isProd ? false : 'eval',
    18. output: {
    19. path: path.resolve(__dirname, '../dist'),
    20. publicPath: '/dist/',
    21. filename: '[name].[chunkhash].js',
    22. // 将包导出为库。 output.library 就是库的名字
    23. pathinfo: true
    24. },
    25. resolve: {
    26. alias: {
    27. vue: 'vue/dist/vue.js',
    28. public: path.resolve(__dirname, '../public'),
    29. '@': path.resolve(__dirname, '../src'),
    30. '@views': path.resolve(__dirname, '../src/views'),
    31. '@components': path.resolve(__dirname, '../src/components'),
    32. '@util': path.resolve(__dirname, '../src/util'),
    33. '@mixins': path.resolve(__dirname, '../src/mixins'),
    34. '@config': path.resolve(__dirname, '../src/config')
    35. }
    36. },
    37. module: {
    38. noParse: [/es6-promise\.js$/, /es6-shim\.js$/], // avoid webpack shimming process
    39. rules: [
    40. {
    41. test: /\.vue$/,
    42. loader: 'vue-loader',
    43. options: vueConfig
    44. },
    45. {
    46. test: /\.js$/,
    47. loader: 'babel-loader',
    48. exclude: /node_modules/,
    49. },
    50. {
    51. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
    52. loader: 'url-loader',
    53. options: {
    54. limit: 10000,
    55. name: utils.assetsPath('img/[name].[ext]?[hash]')
    56. }
    57. },
    58. {
    59. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    60. loader: 'url-loader',
    61. options: {
    62. limit: 10000,
    63. name: utils.assetsPath('fonts/[name].[hash].[ext]')
    64. }
    65. }
    66. ]
    67. },
    68. performance: {
    69. // 入口最大体积限制重置
    70. maxEntrypointSize: 300000,
    71. // 创建超过250kb的资源,没有提示警告或错误显示
    72. hints: isProd ? 'warning' : false
    73. },
    74. plugins: isProd
    75. ? [
    76. new webpack.DefinePlugin({
    77. ...ENV_CONFIG,
    78. prd: true
    79. }),
    80. new webpack.optimize.UglifyJsPlugin({
    81. //UglifyJS里的mangle选项可以设置boolean和object,直观的感觉是,mangle选项让你决定是否把代码里面的变量名进行优化,比如有些变量名很长的,它直接一个字母就替代了,极大的缩短了变量名长度带来的代码量问题,只要在同一个作用域里面,不被外部调用,这种替换非常安全。这一招直接让你的代码缩减巨大,即使你把compress选项设置为false,也能看到显著效果。它还提供更详细的设置选项,你可以选择是否替换toplevel的变量名(默认不替换),也可以有其他选项,具体可以看文档。
    82. mangle: true,
    83. compress: {
    84. // 允许过滤uglify警告
    85. warnings: false,
    86. //默认是 false. 如果你传入true,UglifyJS会假设对象属性的引用(例如foo.bar 或 foo["bar"])没有函数副作用
    87. pure_getters: true,
    88. // 使用 "unsafe"转换
    89. unsafe: true,
    90. 保留< <=不被换成 > >=。假如某些运算对象是用get valueOfobject得出的时候,转换可能会不安全,可能会引起运算对象的改变。此选项只有当 comparisonsunsafe_comps 都设为true时才会启用。
    91. unsafe_comps: true,
    92. // 是否要支持IE6/7/8。UglifyJS默认不兼容IE。
    93. screw_ie8: true
    94. },
    95. output: { comments: false }
    96. }),
    97. // 这个没查到是干嘛的?更具侵略的合并?
    98. new webpack.optimize.AggressiveMergingPlugin(),
    99. // 这个插件的作用是按照chunk引用次数来安排出现顺序,因为这让经常引用的模块和chunk拥有更小的id。使ids可预测,减小文件大小,推荐使用
    100. // 可是我看dist目录下的id长度都一样啊?难道是开发环境的原因?
    101. new webpack.optimize.OccurrenceOrderPlugin(),
    102. // 开启 ScopeHoisting
    103. new webpack.optimize.ModuleConcatenationPlugin(),
    104. // new BundleAnalyzerPlugin(),
    105. // 将css模块和js模块分开打包,换句话说把css代码从js文件中抽离出来,单独出一个模块。
    106. new ExtractTextPlugin({
    107. filename: utils.assetsPath('css/[name].[contenthash].css')
    108. })
    109. // new webpack.NormalModuleReplacementPlugin(/element-ui[\/\\]lib[\/\\]locale[\/\\]lang[\/\\]zh-CN/, 'element-ui/lib/locale/lang/en'),
    110. ]
    111. : [
    112. new FriendlyErrorsPlugin(),
    113. // new webpack.NormalModuleReplacementPlugin(/element-ui[\/\\]lib[\/\\]locale[\/\\]lang[\/\\]zh-CN/, 'element-ui/lib/locale/lang/en'),
    114. new webpack.DefinePlugin({
    115. ...ENV_CONFIG,
    116. prd: false
    117. })
    118. ]
    119. };
    1. module.exports = {
    2. // // 会把vue中的样式文件提取出来
    3. extractCSS: process.env.NODE_ENV === 'production',
    4. preserveWhitespace: false,
    5. postcss: [
    6. require('autoprefixer')({
    7. browsers: ['last 3 versions']
    8. })
    9. ]
    10. }