[Vue CLI 3] 配置解析之 parallel
阿新 • • 發佈:2018-12-18
官方文件中介紹過在 vue.config.js
檔案中可以配置 parallel
,作用如下:
是否為 Babel 或 TypeScript 使用 thread-loader。
該選項在系統的 CPU 有多於一個核心時自動啟用,僅作用於生產構建
我們看一下原始碼
部分:
parallel
接受 boolean
值:
parallel: joi.boolean()
預設設定如下:
parallel: hasMultipleCores()
依賴了函式 hasMultipleCores
in some cases cpus() returns undefined, and may simply throw in the future
詳情見:https://github.com/nodejs/nod...
通過核心包 os
的 cpus
函式
function hasMultipleCores () {
try {
return require('os').cpus().length > 1
} catch (e) {
return false
}
}
那它會影響什麼呢?
babel 部分
在 @vue/cli-plugin-babel/README.md 也提到了:
thread-loader is enabled by default when the machine has more than 1 CPU cores. This can be turned off by setting parallel: false
vue.config.js
.
我們來看一下原始碼:
在線上環境和 vue.config.js
中的配置 parallel
:
const useThreads = process.env.NODE_ENV === 'production' && options.parallel
然後如果 useThreads
為 true
,會 use
外掛 thread-loader
if (useThreads) {
jsRule
.use('thread-loader')
.loader('thread-loader')
}
所以大家應該知道,如果你在某個專案裡面看到 vue.config.js 配置了:
parallel: require('os').cpus().length > 1
其實是多餘
的,而且不保險
來源:https://segmentfault.com/a/1190000016247395