DevOps要求我们将前端版本限制为~1GB的RAM,以便我们的Jenkins实例不会关闭。我们使用标准的@vue/cli
项目,使用TypeScript。但是,TS类型检查服务忽略所有限制其内存使用量的尝试,该操作始终为2048 MB。
我试过禁用它并依赖fork-ts-checker-webpack-plugin
,但这引入了其他问题。
基于我发现的,这应该工作:
$ NODE_OPTIONS=--max_old_space_size=1024 \
NODE_ENV=production \
node \
--max_old_space_size=1024 \
--max-old-space-size=1024 \
node_modules/.bin/vue-cli-service build
请注意,我不知道这些内存限制是如何工作的,因为我对Node内部的了解有限。但尽管如此,类型检查服务始终以2048 MB的限制开始。
我不确定这是否是特定于Vue CLI如何配置Webpack / TS的问题。
我遇到了同样的问题(虽然在我的情况下,我想提高内存限制而不是降低内存限制)。我可以通过自定义Vue CLI的内置ForkTsCheckerWebpackPlugin
来修改webpack.config
的配置:
// in vue.config.js
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
module.exports = {
configureWebpack: config => {
// get a reference to the existing ForkTsCheckerWebpackPlugin
const existingForkTsChecker = config.plugins.filter(
p => p instanceof ForkTsCheckerWebpackPlugin,
)[0];
// remove the existing ForkTsCheckerWebpackPlugin
// so that we can replace it with our modified version
config.plugins = config.plugins.filter(
p => !(p instanceof ForkTsCheckerWebpackPlugin),
);
// copy the options from the original ForkTsCheckerWebpackPlugin
// instance and add the memoryLimit property
const forkTsCheckerOptions = existingForkTsChecker.options;
forkTsCheckerOptions.memoryLimit = 8192;
config.plugins.push(new ForkTsCheckerWebpackPlugin(forkTsCheckerOptions));
},
};
现在当我运行我的构建时,我在输出中看到了这个:
- Building for production...
Starting type checking service...
Using 1 worker with 8192MB memory limit
有关configureWebpack
选项的更多信息,请访问:https://cli.vuejs.org/config/#configurewebpack
要查看Vue CLI使用的默认Webpack配置,可以通过运行vue inspect
来检查它:https://cli.vuejs.org/guide/webpack.html#inspecting-the-project-s-webpack-config
在vue.config.js
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const os=require('os');
module.exports = {
//......,
chainWebpack: config => {
config
.plugin('fork-ts-checker')
.tap(args => {
let totalmem=Math.floor(os.totalmem()/1024/1024); //get OS mem size
let allowUseMem= totalmem>2500? 2048:1000;
args[0].memoryLimit = allowUseMem;
return args
})
},
//......
}