webpack.demo.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const path = require('path');
  2. const webpack = require('webpack');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. const CopyWebpackPlugin = require('copy-webpack-plugin');
  5. const HtmlWebpackPlugin = require('html-webpack-plugin');
  6. const ProgressBarPlugin = require('progress-bar-webpack-plugin');
  7. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  8. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  9. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  10. const config = require('./config');
  11. const isProd = process.env.NODE_ENV === 'production';
  12. const isPlay = !!process.env.PLAY_ENV;
  13. const webpackConfig = {
  14. mode: process.env.NODE_ENV,
  15. entry: isProd ? {
  16. docs: './examples/entry.js'
  17. } : (isPlay ? './examples/play.js' : './examples/entry.js'),
  18. output: {
  19. path: path.resolve(process.cwd(), './examples/element-ui/'),
  20. publicPath: process.env.CI_ENV || '',
  21. filename: '[name].[hash:7].js',
  22. chunkFilename: isProd ? '[name].[hash:7].js' : '[name].js'
  23. },
  24. resolve: {
  25. extensions: ['.js', '.vue', '.json'],
  26. alias: config.alias,
  27. modules: ['node_modules']
  28. },
  29. devServer: {
  30. host: '0.0.0.0',
  31. port: 8085,
  32. publicPath: '/',
  33. hot: true
  34. },
  35. performance: {
  36. hints: false
  37. },
  38. stats: {
  39. children: false
  40. },
  41. module: {
  42. rules: [
  43. {
  44. enforce: 'pre',
  45. test: /\.(vue|jsx?)$/,
  46. exclude: /node_modules/,
  47. loader: 'eslint-loader'
  48. },
  49. {
  50. test: /\.(jsx?|babel|es6)$/,
  51. include: process.cwd(),
  52. exclude: config.jsexclude,
  53. loader: 'babel-loader'
  54. },
  55. {
  56. test: /\.vue$/,
  57. loader: 'vue-loader',
  58. options: {
  59. compilerOptions: {
  60. preserveWhitespace: false
  61. }
  62. }
  63. },
  64. {
  65. test: /\.(scss|css)$/,
  66. use: [
  67. isProd ? MiniCssExtractPlugin.loader : 'style-loader',
  68. 'css-loader',
  69. 'sass-loader'
  70. ]
  71. },
  72. {
  73. test: /\.md$/,
  74. use: [
  75. {
  76. loader: 'vue-loader',
  77. options: {
  78. compilerOptions: {
  79. preserveWhitespace: false
  80. }
  81. }
  82. },
  83. {
  84. loader: path.resolve(__dirname, './md-loader/index.js')
  85. }
  86. ]
  87. },
  88. {
  89. test: /\.(svg|otf|ttf|woff2?|eot|gif|png|jpe?g)(\?\S*)?$/,
  90. loader: 'url-loader',
  91. // todo: 这种写法有待调整
  92. query: {
  93. limit: 10000,
  94. name: path.posix.join('static', '[name].[hash:7].[ext]')
  95. }
  96. }
  97. ]
  98. },
  99. plugins: [
  100. new webpack.HotModuleReplacementPlugin(),
  101. new HtmlWebpackPlugin({
  102. template: './examples/index.tpl',
  103. filename: './index.html',
  104. favicon: './examples/favicon.ico'
  105. }),
  106. new CopyWebpackPlugin([
  107. { from: 'examples/versions.json' }
  108. ]),
  109. new ProgressBarPlugin(),
  110. new VueLoaderPlugin(),
  111. new webpack.DefinePlugin({
  112. 'process.env.FAAS_ENV': JSON.stringify(process.env.FAAS_ENV)
  113. }),
  114. new webpack.LoaderOptionsPlugin({
  115. vue: {
  116. compilerOptions: {
  117. preserveWhitespace: false
  118. }
  119. }
  120. })
  121. ],
  122. optimization: {
  123. minimizer: []
  124. },
  125. devtool: '#eval-source-map'
  126. };
  127. if (isProd) {
  128. webpackConfig.externals = {
  129. vue: 'Vue',
  130. 'vue-router': 'VueRouter',
  131. 'highlight.js': 'hljs'
  132. };
  133. webpackConfig.plugins.push(
  134. new MiniCssExtractPlugin({
  135. filename: '[name].[contenthash:7].css'
  136. })
  137. );
  138. webpackConfig.optimization.minimizer.push(
  139. new UglifyJsPlugin({
  140. cache: true,
  141. parallel: true,
  142. sourceMap: false
  143. }),
  144. new OptimizeCSSAssetsPlugin({})
  145. );
  146. // https://webpack.js.org/configuration/optimization/#optimizationsplitchunks
  147. webpackConfig.optimization.splitChunks = {
  148. cacheGroups: {
  149. vendor: {
  150. test: /\/src\//,
  151. name: 'element-ui',
  152. chunks: 'all'
  153. }
  154. }
  155. };
  156. webpackConfig.devtool = false;
  157. }
  158. module.exports = webpackConfig;