You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.7 KiB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const config = require('../config')
  5. const webpack = require('webpack')
  6. const pkg = require('../package.json');
  7. const vueLoaderConfig = require('./vue-loader.conf')
  8. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  9. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  10. const threadLoader = require('thread-loader');
  11. threadLoader.warmup({
  12. // pool options, like passed to loader options
  13. // must match loader options to boot the correct pool
  14. }, [
  15. // modules to load
  16. // can be any module, i. e.
  17. 'vue-loader',
  18. 'css-loader',
  19. 'babel-loader',
  20. 'babel-preset-es2015',
  21. 'sass-loader',
  22. ]);
  23. var threadLoaderConfig={
  24. loader: "thread-loader",
  25. // 有同样配置的 loader 会共享一个 worker 池(worker pool)
  26. options: {
  27. // 产生的 worker 的数量,默认是 cpu 的核心数
  28. //workers: 8,
  29. // 一个 worker 进程中并行执行工作的数量
  30. // 默认为 20
  31. //workerParallelJobs: 20,
  32. // 额外的 node.js 参数
  33. //workerNodeArgs: ['--max-old-space-size', '1024'],
  34. // 闲置时定时删除 worker 进程
  35. // 默认为 500ms
  36. // 可以设置为无穷大, 这样在监视模式(--watch)下可以保持 worker 持续存在
  37. //poolTimeout: 2000,
  38. // 池(pool)分配给 worker 的工作数量
  39. // 默认为 200
  40. // 降低这个数值会降低总体的效率,但是会提升工作分布更均一
  41. //poolParallelJobs: 200,
  42. // 池(pool)的名称
  43. // 可以修改名称来创建其余选项都一样的池(pool)
  44. name: "my-pool"
  45. }
  46. }
  47. const publicCssLoaders=process.env.NODE_ENV === 'production'?[{loader:MiniCssExtractPlugin.loader,options:{publicPath:'../'}},'css-loader']:[ 'style-loader','css-loader']
  48. function resolve(dir) {
  49. return path.join(__dirname, '..', dir)
  50. }
  51. const createLintingRule = () => ({
  52. test: /\.(js|vue)$/,
  53. loader: 'eslint-loader',
  54. enforce: 'pre',
  55. include: [resolve('src'), resolve('test')],
  56. options: {
  57. formatter: require('eslint-friendly-formatter'),
  58. emitWarning: !config.dev.showEslintErrorsInOverlay
  59. }
  60. })
  61. module.exports = {
  62. context: path.resolve(__dirname, '../'),
  63. entry: {
  64. app: './src/main.js'
  65. },
  66. output: {
  67. clean:true,
  68. path: config.build.assetsRoot,
  69. filename: 'js/[name].[contenthash].js',
  70. pathinfo: false,
  71. publicPath: process.env.NODE_ENV === 'production'
  72. ? config.build.assetsPublicPath
  73. : config.dev.assetsPublicPath
  74. },
  75. resolve: {
  76. extensions: ['.js', '.vue', '.json'],
  77. alias: {
  78. 'vue$': 'vue/dist/vue.esm.js',
  79. '@': resolve('src'),
  80. }
  81. },
  82. // 加载器
  83. module: {
  84. // https://doc.webpack-china.org/guides/migrating/#module-loaders-module-rules
  85. rules: [
  86. //...(config.dev.useEslint ? [createLintingRule()] : []),
  87. {
  88. test: /\.vue$/,
  89. //include: resolve('src'),
  90. use:[
  91. threadLoaderConfig,
  92. {
  93. loader: 'vue-loader',
  94. /**
  95. options:vueLoaderConfig,
  96. */
  97. options: {
  98. postcss: [require('postcss-cssnext')()],
  99. loaders: {
  100. js: [
  101. { loader: 'cache-loader' },
  102. { loader: 'babel-loader', options: { presets: ['env'] } }
  103. ]
  104. },
  105. extractCSS: true,
  106. hotReload:true,
  107. },
  108. }
  109. ]
  110. },
  111. {
  112. test: /\.css$/,
  113. use: publicCssLoaders,
  114. },
  115. {
  116. test: /\.(sa|sc)ss$/,
  117. use: publicCssLoaders.concat([
  118. // 将 Sass 编译成 CSS
  119. 'sass-loader',
  120. ]),
  121. },
  122. {
  123. test: /\.less$/,
  124. use: publicCssLoaders.concat([
  125. // 将 Sass 编译成 CSS
  126. 'less-loader',
  127. ]),
  128. },
  129. {
  130. test: /\.(stylus|styl)$/,
  131. use: publicCssLoaders.concat([
  132. // 将 Sass 编译成 CSS
  133. 'stylus-loader',
  134. ]),
  135. },
  136. { // 配置Babel将ES6+ 转换为ES5
  137. test: /\.js$/,
  138. use:[
  139. threadLoaderConfig,
  140. {
  141. loader: 'babel-loader',
  142. options: {
  143. presets: ['env'],
  144. plugins: ['transform-runtime']
  145. },
  146. },
  147. ],
  148. include: resolve('src'),
  149. },
  150. {
  151. test: /\.svg$/,
  152. loader: 'svg-sprite-loader',
  153. include: [resolve('src/icons')],
  154. options: {
  155. symbolId: 'icon-[name]'
  156. }
  157. },
  158. {
  159. test: /\.(png|jpe?g|gif|tif?f|bmp|webp|svg)(\?.*)?$/,
  160. exclude: [resolve('src/icons')],
  161. type: 'asset',
  162. generator: {
  163. filename: 'img/[hash][ext][query]'
  164. }
  165. },
  166. {
  167. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  168. type: 'asset/resource',
  169. generator: {
  170. filename: 'media/[hash][ext][query]'
  171. }
  172. },
  173. {
  174. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  175. type: 'asset/resource',
  176. generator: {
  177. filename: 'fonts/[hash][ext][query]'
  178. }
  179. }
  180. ]
  181. },
  182. plugins: [
  183. new VueLoaderPlugin(),
  184. ],
  185. optimization: {
  186. splitChunks: {
  187. chunks: 'all',
  188. minSize: 200000,
  189. enforceSizeThreshold: 400000,
  190. cacheGroups: {
  191. defaultVendors: {
  192. test: /[\\/]node_modules[\\/]/,
  193. priority: -10,
  194. reuseExistingChunk: true,
  195. enforceSizeThreshold: 400000,
  196. },
  197. default: {
  198. minChunks: 2,
  199. priority: -20,
  200. reuseExistingChunk: true,
  201. enforceSizeThreshold: 400000,
  202. },
  203. },
  204. },
  205. },
  206. }