webpack.build.js.bak 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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:16+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].css');
  21. module.exports = {
  22. entry: {
  23. index: path.resolve(__dirname, '../src/index.js')
  24. },
  25. output: {
  26. path: path.resolve(__dirname, '../dist'),
  27. // publicPath: '/dist/',
  28. filename: 'mavon-editor.js',
  29. library: 'mavon-editor',
  30. libraryTarget: 'umd',
  31. umdNamedDefine: true
  32. },
  33. externals: {
  34. vue: {
  35. root: 'Vue',
  36. commonjs: 'vue',
  37. commonjs2: 'vue',
  38. amd: 'vue'
  39. }
  40. },
  41. module: {
  42. rules: [{
  43. test: /\.vue$/,
  44. loader: 'vue-loader'
  45. },
  46. {
  47. test: /\.js$/,
  48. loader: 'babel-loader',
  49. exclude: /node_modules/
  50. },
  51. {
  52. test: /\.(png|jpg|gif)$/,
  53. loader: 'file-loader',
  54. options: { name: '[name].[ext]?[hash]' }
  55. },
  56. { test: /\.(woff|ttf|eot|svg)/, loader: 'file-loader?name=font/[name].[ext]&publicPath=../' },
  57. {
  58. // css代码分割打包
  59. test: /\.css$/,
  60. // exclude: /node_modules/,
  61. use: extractCSS.extract({
  62. fallback: 'style-loader',
  63. use: [
  64. 'css-loader',
  65. {
  66. loader: 'postcss-loader',
  67. options: {
  68. plugins: function() {
  69. return [
  70. // 允许在子中定义要放在最顶层的样式
  71. require('postcss-atroot')({}),
  72. // 允许定义样式函数
  73. require('postcss-mixins')({}),
  74. // import插件
  75. require('postcss-nested-import')({}),
  76. // 类sass-import插件,但是没法嵌套导入
  77. // require('postcss-partial-import')({}),
  78. // 嵌套解析插件
  79. require('postcss-nested')({}),
  80. // 可以通过引用方式引用父/其他样式的属性值
  81. require('postcss-nesting')({}),
  82. // 允许自定义选择器别名
  83. require('postcss-custom-selectors')({}),
  84. // 可自定义属性块别名,后面可扩充
  85. require('postcss-extend')({}),
  86. // 允许类sass的变量定义,for和each语法
  87. require('postcss-advanced-variables')({}),
  88. // 支持颜色函数color
  89. require('postcss-color-function')({}),
  90. // 支持media的变量定义
  91. require('postcss-custom-media')({}),
  92. // 支持属性自定义
  93. require('postcss-custom-properties')({}),
  94. // 支持media的最大最小值定义 可以通过类似@media screen and (width >= 500px) and (width <= 1200px){}来书写
  95. require('postcss-media-minmax')({}),
  96. // 支持通过@引用本属性块的属性
  97. require('postcss-property-lookup')({}),
  98. // maches函数,p:matches(:first-child, .special)解析为p:first-child, p.special
  99. require('postcss-selector-matches')({}),
  100. // 支持not解析,p:not(:first-child, .special)解析为p:not(:first-child), p:not(.special)
  101. require('postcss-selector-not')({})
  102. ];
  103. }
  104. }
  105. }
  106. ]
  107. })
  108. },{
  109. test: /\.styl$/,
  110. loader: 'style-loader!css-loader!stylus-loader'
  111. },{
  112. test: /\.md$/,
  113. loader: 'raw-loader'
  114. }
  115. ]
  116. },
  117. resolve: {
  118. alias: {
  119. 'vue$': 'vue/dist/vue.esm.js'
  120. }
  121. },
  122. devServer: {
  123. historyApiFallback: true,
  124. disableHostCheck: true,
  125. host: '0.0.0.0',
  126. port: '9090'
  127. // hot: true,
  128. // noInfo: true
  129. },
  130. performance: {
  131. hints: false
  132. },
  133. plugins: [
  134. // new webpack.HotModuleReplacementPlugin(),
  135. // new ExtractTextPlugin("css/[name].[contenthash:8].css"),
  136. extractCSS,
  137. // 分离js可能引入的css的chunkhash计算
  138. new WebpackMd5Hash(),
  139. // 对css文件进行压缩
  140. new OptimizeCssAssetsPlugin({
  141. assetNameRegExp: /\.css$/g,
  142. cssProcessor: require('cssnano'),
  143. cssProcessorOptions: { discardComments: { removeAll: true } },
  144. canPrint: true
  145. }),
  146. new webpack.optimize.UglifyJsPlugin({
  147. compress: {
  148. warnings: false
  149. }
  150. })
  151. // new WebpackSplitHash(),
  152. // new ExtractTextPlugin({ filename: 'bundle.css', disable: false, allChunks: true }),
  153. ]
  154. }