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.

219 lines
6.6 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
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','postcss-loader']:[ 'style-loader','css-loader','postcss-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. use:[
  90. threadLoaderConfig,
  91. {
  92. loader: 'vue-loader',
  93. /**
  94. options:vueLoaderConfig,
  95. */
  96. options: {
  97. postcss: [require('postcss-cssnext')()],
  98. loaders: {
  99. js: [
  100. { loader: 'cache-loader' },
  101. { loader: 'babel-loader', options: { presets: ['env'] } }
  102. ]
  103. },
  104. extractCSS: true,
  105. hotReload:true,
  106. },
  107. }
  108. ]
  109. },
  110. {
  111. test: /\.css$/,
  112. use: publicCssLoaders,
  113. },
  114. {
  115. test: /\.(sa|sc)ss$/,
  116. use: publicCssLoaders.concat([
  117. // 将 Sass 编译成 CSS
  118. 'sass-loader',
  119. ]),
  120. },
  121. {
  122. test: /\.less$/,
  123. use: publicCssLoaders.concat([
  124. // 将 Sass 编译成 CSS
  125. 'less-loader',
  126. ]),
  127. },
  128. {
  129. test: /\.(stylus|styl)$/,
  130. use: publicCssLoaders.concat([
  131. // 将 Sass 编译成 CSS
  132. 'stylus-loader',
  133. ]),
  134. },
  135. { // 配置Babel将ES6+ 转换为ES5
  136. test: /\.js$/,
  137. use:[
  138. threadLoaderConfig,
  139. {
  140. loader: 'babel-loader',
  141. options: {
  142. presets: ['env'],
  143. plugins: ['transform-runtime']
  144. },
  145. },
  146. ],
  147. include: resolve('src'),
  148. },
  149. {
  150. test: /\.svg$/,
  151. loader: 'svg-sprite-loader',
  152. include: [resolve('src/icons')],
  153. options: {
  154. symbolId: 'icon-[name]'
  155. }
  156. },
  157. {
  158. test: /\.(png|jpe?g|gif|tif?f|bmp|webp|svg)(\?.*)?$/,
  159. exclude: [resolve('src/icons')],
  160. type: 'asset',
  161. generator: {
  162. filename: 'img/[hash][ext][query]'
  163. }
  164. },
  165. {
  166. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  167. type: 'asset/resource',
  168. generator: {
  169. filename: 'media/[hash][ext][query]'
  170. }
  171. },
  172. {
  173. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  174. type: 'asset/resource',
  175. generator: {
  176. filename: 'fonts/[hash][ext][query]'
  177. }
  178. }
  179. ]
  180. },
  181. plugins: [
  182. new VueLoaderPlugin(),
  183. ],
  184. optimization: {
  185. splitChunks: {
  186. chunks: 'all',
  187. cacheGroups: {
  188. defaultVendors: {
  189. test: /[\\/]node_modules[\\/]/,
  190. priority: -10,
  191. reuseExistingChunk: true,
  192. },
  193. default: {
  194. minChunks: 2,
  195. priority: -20,
  196. reuseExistingChunk: true,
  197. },
  198. },
  199. },
  200. },
  201. }