【问题标题】:How to ignore `node_modules` folder during TypeScript build in VSCode如何在 VSCode 中构建 TypeScript 期间忽略“node_modules”文件夹
【发布时间】:2015-07-30 14:00:13
【问题描述】:

我正在使用 Visual Studio Code IDE 和 typescript,如何让它在构建过程中忽略 node_modules 文件夹?还是让它在保存时构建.ts 文件?它显示了很多错误,因为它正在尝试编译 node_modules tsd。

目前我的 tasks.json 是

{
    "version": "0.1.0",

    // The command is tsc.
    "command": "tsc",

    // Show the output window only if unrecognized errors occur. 
    "showOutput": "silent",

    // Under windows use tsc.exe. This ensures we don't need a shell.
    "windows": {
        "command": "tsc.exe"
    },

    "isShellCommand": true,

    // args is the HelloWorld program to compile.
    "args": [],



    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
}

【问题讨论】:

    标签: typescript visual-studio-code


    【解决方案1】:

    在 0.5 版中,您可以hide files and folders

    打开 Files->Preferences->User Settings 并添加类似

    {
            "files.exclude": {
                "**/.git": true,
                "**/.DS_Store": true,
                "jspm_packages" : true,
                "node_modules" : true
            }
    }
    

    【讨论】:

    • 情况并非如此,这仅排除隐藏文件夹的视图,它不会将其隐藏在 tsc 编译器中。
    • 是的,不是问题的答案,而是我正在寻找的!也许你应该把它作为一个新问题发布;)
    • 这应该是一个答案。如此处所述:github.com/Microsoft/vscode/issues/22289#issuecomment-380611244 files.exclude 不仅会隐藏文件/文件夹,还会将它们从problems 窗口中删除
    【解决方案2】:

    您现在可以在 tsconfig.json 文件中使用 exclude

    {
        "exclude": [
            "node_modules",
        ],
        "compilerOptions": { 
            ...
        }
    }
    

    https://github.com/Microsoft/TypeScript/wiki/tsconfig.json

    请注意,它是 compilerOptions 的兄弟,而不是其子。

    【讨论】:

    • 错误 TS5023:未知的编译器选项“排除”。不再是答案!
    • 链接可能无效。这是新的文档链接:typescriptlang.org/docs/handbook/tsconfig-json.html 你确定你的 tsconfig 是正确的吗?排除是顶级字段
    • @VladoPandžić 和其他人,这个选项是顶级的;它不会进入compilerOptions。我编辑了答案以澄清(等待同行评审)。感谢 Carbosound1 的解释。
    • 我的exclude 选项的配置完全 与此处演示的相同,但 VSCode 仍然用错误曲线标记依赖项源文件的几乎每一行。你知道为什么吗?我也尝试过启用skipLibCheck,但无济于事:-(
    【解决方案3】:

    如果您不提供files 列表,VSCode 将编译所有内容。

    {
        "compilerOptions": {
            "target": "ES5"
        }
    }
    

    您可以通过提供要编译的文件列表来更改此设置,例如:

    {
        "compilerOptions": {
            "target": "ES6"
        },
        "files": [
            "app.ts",
            "other.ts",
            "more.ts"
        ]
    }
    

    【讨论】:

    • 否:错误 TS5023:未知的编译器选项“文件”。
    • @GrimmTheOpiner 这不是编译器选项,而是顶级选项。
    【解决方案4】:

    这是一种解决方法,暂时不要使用 tsconfig.json,它不支持您需要的排除项。我想要的只是使用 tasks.json 使用选项编译您所在的文件。现在你必须 CTRL+SHIFT+B 来构建,还没有很好的保存方式来构建。

    {
    
    "version": "0.1.0",
    
    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "tsc",
    
    // The command is a shell script
    "isShellCommand": true,
    
    // Show the output window only if unrecognized errors occur. 
    "showOutput": "always",
    
    // args is the HelloWorld program to compile.
    "args": ["${file}", "--module", "amd", "--target", "ES5"],
    
    
    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-17
      • 1970-01-01
      • 2019-01-09
      • 2020-05-28
      • 2021-09-21
      • 2015-07-01
      • 2018-01-12
      • 2014-01-25
      • 1970-01-01
      相关资源
      最近更新 更多