【问题标题】:Why need exclude=["node_modules"] when I just have include=["src"] in my tsconfig.json?当我的 tsconfig.json 中只有 include=["src"] 时,为什么需要 exclude=["node_modules"]?
【发布时间】:2020-08-10 14:22:23
【问题描述】:

我只想编译“src”文件夹。因此我做了include:["src"],并且node_modules 仅在src 之外找到,那么为什么我需要像许多站点所建议的exclude: ["node_modules"] 一样排除它们?

例如他们还建议我们包含 src 然后排除 node_modules - https://www.javatpoint.com/typescript-compilation-context

{  
    "compilerOptions": {  
        "module": "system",  
        "noImplicitAny": true,  
        "removeComments": true,  
        "preserveConstEnums": true,  
        "outFile": "../../built/local/tsc.js",  
        "sourceMap": true  
    },  
    "include": [  
        "src/**/*"  
    ],  
    "exclude": [  
        "node_modules",  
        "**/*.spec.ts"  
    ]  
}  

我不应该在我的 tsconfig.json 中写任何排除,对吧?

【问题讨论】:

    标签: typescript node-modules tsconfig


    【解决方案1】:

    首先,您不必在 exclude 选项中指定 node_modules,因为默认情况下 TypeScript 会自动排除它。

    如果exclude 被禁用,TypeScript 会将这些目录包含为排除。

    - node_modules
    - bower_components
    - jspm_packages
    - the files <outDir> option specifies
    

    exclude 选项仅用作include 选项的过滤。所以只能在使用include 选项时指定。

    通过此处的示例查看更多详细信息。

    https://medium.com/javascript-in-plain-english/typescript-configuration-options-tsconfig-json-561d4a2ad4b

    【讨论】:

      【解决方案2】:

      你理解错了。 include 数组中的值表示将包含在程序中的文件夹/文件。 exclude 然后指定哪些文件夹/文件必须从包含的文件夹/文件中排除。

      换句话说,如果你写include: ['src'],然后写exclude: ['*.spec.ts'],那么你排除了src文件夹下找到的每个*.spec.ts文件。

      您必须排除 node_modules,即使该文件夹位于 src 文件夹之外。这是因为这些包被导入(使用import)放在包含的src 文件夹内的文件(也称为模块)中。如果你不这样做,TypeScript 也会检查导入的包,这不是你想要的。

      由于建议始终排除 node_modules,TypeScript 决定将其添加为默认值。因此,如果您只想排除 node_modules,则可以省略整个 exclude 规范。

      由于您还想排除 *.spec.ts 文件(不是默认),因此您需要按照您的方式指定它。

      还有更多文件夹已作为默认值添加到数组中["node_modules", "bower_components", "jspm_packages"]

      您可以阅读有关此主题的更多信息in the docs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 2021-01-20
        • 2021-04-03
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多