Vue CLI 的类型检查服务忽略内存限制



devOps已要求我们将前端构建限制为〜1GB RAM,以使我们的Jenkins实例不关闭。我们使用标准的@vue/cli项目,带有打字稿。但是,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的内置webpack.config来修改ForkTsCheckerWebpackPlugin的配置:

// 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;
                // in vue-cli shuld args[0]['typescript'].memoryLimit
                args[0].memoryLimit = allowUseMem;
                return args
            })
    },
   //......
}

node_modules/fork-ts-checker-webpack-plugin/lib/index.js

declare class ForkTsCheckerWebpackPlugin {
    static readonly DEFAULT_MEMORY_LIMIT = 4096;
    static readonly ONE_CPU = 1;
    static readonly ALL_CPUS: number;
    static readonly ONE_CPU_FREE: number;
    static readonly TWO_CPUS_FREE: number;
    readonly options: Partial<Options>;

最新更新