webpack.demo.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 'element-ui': './src/index.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. noInfo: true
  35. },
  36. performance: {
  37. hints: false
  38. },
  39. stats: {
  40. children: false
  41. },
  42. module: {
  43. rules: [
  44. {
  45. enforce: 'pre',
  46. test: /\.(vue|jsx?)$/,
  47. exclude: /node_modules/,
  48. loader: 'eslint-loader'
  49. },
  50. {
  51. test: /\.(jsx?|babel|es6)$/,
  52. include: process.cwd(),
  53. exclude: config.jsexclude,
  54. loader: 'babel-loader'
  55. },
  56. {
  57. test: /\.vue$/,
  58. loader: 'vue-loader',
  59. options: {
  60. compilerOptions: {
  61. preserveWhitespace: false
  62. }
  63. }
  64. },
  65. {
  66. test: /\.(scss|css)$/,
  67. use: [
  68. isProd ? MiniCssExtractPlugin.loader : 'style-loader',
  69. 'css-loader',
  70. 'sass-loader'
  71. ]
  72. },
  73. {
  74. test: /\.md$/,
  75. use: [
  76. {
  77. loader: 'vue-loader',
  78. options: {
  79. compilerOptions: {
  80. preserveWhitespace: false
  81. }
  82. }
  83. },
  84. {
  85. loader: path.resolve(__dirname, './md-loader/index.js')
  86. }
  87. ]
  88. },
  89. {
  90. test: /\.(svg|otf|ttf|woff2?|eot|gif|png|jpe?g)(\?\S*)?$/,
  91. loader: 'url-loader',
  92. // todo: 这种写法有待调整
  93. query: {
  94. limit: 10000,
  95. name: path.posix.join('static', '[name].[hash:7].[ext]')
  96. }
  97. }
  98. ]
  99. },
  100. plugins: [
  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. };
  126. if (isProd) {
  127. webpackConfig.externals = {
  128. vue: 'Vue',
  129. 'vue-router': 'VueRouter',
  130. 'highlight.js': 'hljs'
  131. };
  132. webpackConfig.plugins.push(
  133. new MiniCssExtractPlugin({
  134. filename: '[name].[contenthash:7].css'
  135. })
  136. );
  137. webpackConfig.optimization.minimizer.push(
  138. new UglifyJsPlugin({
  139. cache: true,
  140. parallel: true,
  141. sourceMap: false
  142. }),
  143. new OptimizeCSSAssetsPlugin({})
  144. );
  145. }
  146. module.exports = webpackConfig;