webpack.demo.js 4.3 KB

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