webpack.dev.js.bak 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @Author: HuaChao Chen <CHC>
  3. * @Date: 2017-05-04T23:21:48+08:00
  4. * @Email: chenhuachaoxyz@gmail.com
  5. * @Filename: webpack.dev.js
  6. * @Last modified by: CHC
  7. * @Last modified time: 2017-05-06T22:00:44+08:00
  8. * @License: MIT
  9. * @Copyright: 2017
  10. */
  11. var path = require('path');
  12. var webpack = require('webpack');
  13. var HtmlWebpackPlugin = require('html-webpack-plugin');
  14. var ExtractTextPlugin = require('extract-text-webpack-plugin');
  15. var WebpackMd5Hash = require('webpack-md5-hash');
  16. // 该插件是对“webpack-md5-hash”的改进:在主文件中获取到各异步模块的hash值,然后将这些hash值与主文件的代码内容一同作为计算hash的参数,这样就能保证主文件的hash值会跟随异步模块的修改而修改。
  17. // var WebpackSplitHash = require('webpack-split-hash');
  18. // 压缩css
  19. var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  20. const extractCSS = new ExtractTextPlugin('css/[name].[contenthash:8].css');
  21. module.exports = {
  22. entry: {
  23. index: './src/dev/index.js',
  24. vue: ['vue']
  25. },
  26. output: {
  27. path: path.resolve(__dirname, './dist'),
  28. // publicPath: '/dist/',
  29. filename: 'js/[name].[chunkhash:8].js',
  30. chunkFilename: 'js/[name].[chunkhash:8].js'
  31. },
  32. module: {
  33. rules: [{
  34. test: /\.vue$/,
  35. loader: 'vue-loader'
  36. },
  37. {
  38. test: /\.js$/,
  39. loader: 'babel-loader',
  40. exclude: /node_modules/
  41. },
  42. {
  43. test: /\.(png|jpg|gif)$/,
  44. loader: 'file-loader',
  45. options: { name: '[name].[ext]?[hash]' }
  46. },
  47. { test: /\.(woff|ttf|eot|svg)/, loader: 'file-loader?name=font/[name].[hash:8].[ext]&publicPath=../' },
  48. {
  49. // css代码分割打包
  50. test: /\.css$/,
  51. // exclude: /node_modules/,
  52. use: extractCSS.extract({
  53. fallback: 'style-loader',
  54. use: [
  55. 'css-loader',
  56. {
  57. loader: 'postcss-loader',
  58. options: {
  59. plugins: function() {
  60. return [
  61. // 允许在子中定义要放在最顶层的样式
  62. require('postcss-atroot')({}),
  63. // 允许定义样式函数
  64. require('postcss-mixins')({}),
  65. // import插件
  66. require('postcss-nested-import')({}),
  67. // 类sass-import插件,但是没法嵌套导入
  68. // require('postcss-partial-import')({}),
  69. // 嵌套解析插件
  70. require('postcss-nested')({}),
  71. // 可以通过引用方式引用父/其他样式的属性值
  72. require('postcss-nesting')({}),
  73. // 允许自定义选择器别名
  74. require('postcss-custom-selectors')({}),
  75. // 可自定义属性块别名,后面可扩充
  76. require('postcss-extend')({}),
  77. // 允许类sass的变量定义,for和each语法
  78. require('postcss-advanced-variables')({}),
  79. // 支持颜色函数color
  80. require('postcss-color-function')({}),
  81. // 支持media的变量定义
  82. require('postcss-custom-media')({}),
  83. // 支持属性自定义
  84. require('postcss-custom-properties')({}),
  85. // 支持media的最大最小值定义 可以通过类似@media screen and (width >= 500px) and (width <= 1200px){}来书写
  86. require('postcss-media-minmax')({}),
  87. // 支持通过@引用本属性块的属性
  88. require('postcss-property-lookup')({}),
  89. // maches函数,p:matches(:first-child, .special)解析为p:first-child, p.special
  90. require('postcss-selector-matches')({}),
  91. // 支持not解析,p:not(:first-child, .special)解析为p:not(:first-child), p:not(.special)
  92. require('postcss-selector-not')({})
  93. ];
  94. }
  95. }
  96. }
  97. ]
  98. })
  99. },{
  100. test: /\.styl$/,
  101. loader: 'style-loader!css-loader!stylus-loader'
  102. },{
  103. test: /\.md$/,
  104. loader: 'raw-loader'
  105. }
  106. ]
  107. },
  108. resolve: {
  109. alias: {
  110. 'vue$': 'vue/dist/vue.esm.js'
  111. }
  112. },
  113. devServer: {
  114. historyApiFallback: true,
  115. disableHostCheck: true,
  116. host: '0.0.0.0',
  117. port: '9090'
  118. // hot: true,
  119. // noInfo: true
  120. },
  121. performance: {
  122. hints: false
  123. },
  124. plugins: [
  125. new webpack.optimize.CommonsChunkPlugin({
  126. names: ['vue', 'common'],
  127. filename: 'js/[name].[chunkhash:8].js',
  128. minChunks: Infinity
  129. }),
  130. new HtmlWebpackPlugin({
  131. filename: 'index.html',
  132. template: 'src/dev/index.html',
  133. inject: true,
  134. hash: false,
  135. chunks: ['common', 'vue', 'index']
  136. }),
  137. // new webpack.HotModuleReplacementPlugin(),
  138. // new ExtractTextPlugin("css/[name].[contenthash:8].css"),
  139. extractCSS,
  140. // 分离js可能引入的css的chunkhash计算
  141. new WebpackMd5Hash(),
  142. // 对css文件进行压缩
  143. new OptimizeCssAssetsPlugin({
  144. assetNameRegExp: /\.css$/g,
  145. cssProcessor: require('cssnano'),
  146. cssProcessorOptions: { discardComments: { removeAll: true } },
  147. canPrint: true
  148. })
  149. // new WebpackSplitHash(),
  150. // new ExtractTextPlugin({ filename: 'bundle.css', disable: false, allChunks: true }),
  151. ],
  152. devtool: '#eval-source-map'
  153. }