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

xkv*_*vcr 5 typescript webpack vue-cli vue-cli-3

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
Run Code Online (Sandbox Code Playgroud)

请注意,我不知道这些内存限制如何工作,因为我对Node内部的了解有限。但是,尽管有这些,类型检查服务始终以2048 MB的限制开始。

我不确定这是否是Vue CLI如何配置Webpack / TS的问题。

小智 11

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
            })
    },
   //......
}
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案对我有用。需要明确的是,我的项目中的这段代码前面是“configureWebpack{}” (3认同)

Nat*_*end 7

我遇到了同样的问题(尽管就我而言,我想提高内存限制而不是降低内存限制)。我可以ForkTsCheckerWebpackPlugin通过自定义Vue CLI的内置功能来修改的配置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));
  },
};
Run Code Online (Sandbox Code Playgroud)

现在,当我运行构建时,在输出中将看到以下内容:

-  Building for production...
Starting type checking service...
Using 1 worker with 8192MB memory limit
Run Code Online (Sandbox Code Playgroud)

有关此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