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

时间:2019-03-20 10:18:07

标签: typescript webpack vue-cli vue-cli-3

DevOps已要求我们将前端版本的内存限制为〜1GB,以便我们的Jenkins实例不会关闭。我们使用带有TypeScript的标准@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的问题。

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题(尽管就我而言,我想提高而不是降低内存限制)。我可以通过自定义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

答案 1 :(得分:2)

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
            })
    },
   //......
}

答案 2 :(得分:2)

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>;