[问与答] 如何降低 webpack 打包时的内存占用

webpack 打包时单个 node 能飙到 3G+ 内存( top 观察还会有另外一个 node 进程,占 1G 左右)

机器内存较小,构建经常失败。请问如何能降低 webpack 构建内存占用?

SF 上的方法说: 设置 `NODE_OPTIONS=–-max-old-space-size=2048` ,测试无效。

附上 package.json

{ "name": "", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "export NODE_OPTIONS=–-max-old-space-size=2048 && vue-cli-service build", "lint": "vue-cli-service lint" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^6.1.1", "@fortawesome/free-brands-svg-icons": "^6.1.1", "@fortawesome/free-regular-svg-icons": "^6.1.1", "@fortawesome/free-solid-svg-icons": "^6.1.1", "@fortawesome/vue-fontawesome": "^2.0.6", "@stoplight/json-schema-ref-parser": "^9.2.2", "@stoplight/json-schema-sampler": "^0.2.2", "axios": "^0.24.0", "core-js": "^3.8.3", "echarts": "^5.2.2", "element-ui": "^2.15.9", "lodash": "^4.17.21", "marked": "^4.0.13", "moment": "^2.29.4", "monaco-editor": "^0.33.0", "monaco-editor-webpack-plugin": "^7.0.1", "url": "^0.11.0", "util": "^0.12.4", "uuid": "^8.3.2", "vue": "^2.6.14", "vue-codemirror": "^4.0.6", "vue-contextmenujs": "^1.3.13", "vue-echarts": "^6.0.0", "vue-grid-layout": "^2.3.12", "vue-multipane": "^0.9.5", "vue-router": "^3.5.1", "vuex": "^3.6.2" }, "devDependencies": { "@babel/core": "^7.17.8", "@babel/eslint-parser": "^7.17.0", "@vue/cli-plugin-babel": "~5.0.0", "@vue/cli-plugin-eslint": "~5.0.0", "@vue/cli-plugin-router": "~5.0.0", "@vue/cli-plugin-vuex": "~5.0.0", "@vue/cli-service": "~5.0.0", "@vue/composition-api": "^1.4.9", "eslint": "^8.12.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^4.0.0", "eslint-plugin-vue": "^8.6.0", "node-sass": "^7.0.1", "prettier": "^2.6.2", "process": "^0.11.10", "sass-loader": "^12.6.0", "vue-template-compiler": "^2.6.14" }, "browserslist": [ "> 1%", "last 2 versions", "not dead" ]
}