微信

webpack.prod.conf.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var path = require('path')
  2. var config = require('../config')
  3. var utils = require('./utils')
  4. var webpack = require('webpack')
  5. var merge = require('webpack-merge')
  6. var baseWebpackConfig = require('./webpack.base.conf')
  7. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  8. var HtmlWebpackPlugin = require('html-webpack-plugin')
  9. var CleanPlugin = require('clean-webpack-plugin')//webpack插件,用于清除目录文件
  10. var glob = require('glob');
  11. var env = config.build.env
  12. var webpackConfig = merge(baseWebpackConfig, {
  13. module: {
  14. loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true })
  15. },
  16. devtool: config.build.productionSourceMap ? '#source-map' : false,
  17. output: {
  18. path: config.build.assetsRoot,
  19. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  20. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  21. },
  22. vue: {
  23. loaders: utils.cssLoaders({
  24. sourceMap: config.build.productionSourceMap,
  25. extract: true
  26. })
  27. },
  28. plugins: [
  29. // http://vuejs.github.io/vue-loader/workflow/production.html
  30. new webpack.DefinePlugin({
  31. 'process.env': env
  32. }),
  33. new webpack.optimize.UglifyJsPlugin({
  34. compress: {
  35. warnings: false
  36. }
  37. }),
  38. new CleanPlugin(['../dist']), //清空生成目录
  39. new webpack.optimize.OccurenceOrderPlugin(),
  40. // extract css into its own file
  41. new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),
  42. // generate dist index.html with correct asset hash for caching.
  43. // you can customize output by editing /index.html
  44. // see https://github.com/ampedandwired/html-webpack-plugin
  45. // split vendor js into its own file
  46. new webpack.optimize.CommonsChunkPlugin({
  47. name: 'vendor',
  48. minChunks: function (module, count) {
  49. // any required modules inside node_modules are extracted to vendor
  50. return (
  51. module.resource &&
  52. /\.js$/.test(module.resource) &&
  53. module.resource.indexOf(
  54. path.join(__dirname, '../node_modules')
  55. ) === 0
  56. )
  57. }
  58. }),
  59. // extract webpack runtime and module manifest to its own file in order to
  60. // prevent vendor hash from being updated whenever app bundle is updated
  61. new webpack.optimize.CommonsChunkPlugin({
  62. name: 'manifest',
  63. chunks: ['vendor']
  64. })
  65. ]
  66. })
  67. if (config.build.productionGzip) {
  68. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  69. webpackConfig.plugins.push(
  70. new CompressionWebpackPlugin({
  71. asset: '[path].gz[query]',
  72. algorithm: 'gzip',
  73. test: new RegExp(
  74. '\\.(' +
  75. config.build.productionGzipExtensions.join('|') +
  76. ')$'
  77. ),
  78. threshold: 10240,
  79. minRatio: 0.8
  80. })
  81. )
  82. }
  83. module.exports = webpackConfig
  84. function getEntry(globPath) {
  85. var entries = {},
  86. basename, tmp, pathname;
  87. if (typeof (globPath) != "object") {
  88. globPath = [globPath]
  89. }
  90. globPath.forEach((itemPath) => {
  91. glob.sync(itemPath).forEach(function (entry) {
  92. basename = path.basename(entry, path.extname(entry));
  93. if (entry.split('/').length > 4) {
  94. tmp = entry.split('/').splice(-3);
  95. pathname = tmp.splice(0, 1) + '/' + basename; // 正确输出js和html的路径
  96. entries[pathname] = entry;
  97. } else {
  98. entries[basename] = entry;
  99. }
  100. });
  101. });
  102. return entries;
  103. }
  104. var pages = getEntry(['./src/module/*.html','./src/module/**/*.html']);
  105. for (var pathname in pages) {
  106. // 配置生成的html文件,定义路径等
  107. var conf = {
  108. filename: pathname + '.html',
  109. template: pages[pathname], // 模板路径
  110. inject: true, // js插入位置
  111. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  112. chunksSortMode: 'dependency'
  113. };
  114. if (pathname in module.exports.entry) {
  115. conf.chunks = ['manifest', 'vendor', pathname];
  116. conf.hash = true;
  117. }
  118. module.exports.plugins.push(new HtmlWebpackPlugin(conf));
  119. }