如何配置tsc让routes目录被包含到编译输出目录中?
解决TypeScript编译时routes目录未生成到输出目录的问题
问题根源
你的tsconfig.json中include字段配置为[".."],这会尝试包含上级目录的所有文件,但由于routes目录和tsconfig.json处于同一层级,该配置无法正确识别当前目录下的routes/index.tsx,导致tsc编译时忽略了该文件。
修复步骤
1. 修正include配置
将include字段改为明确包含当前目录下所有.ts和.tsx文件,确保routes目录被覆盖:
"include": ["**/*.ts", "**/*.tsx"],
如果需要更精确的控制,也可以直接指定routes目录:
"include": ["./routes/**/*.tsx", "./**/*.ts"],
2. 保留合理的exclude配置
当前的exclude配置已经排除了node_modules和测试文件,无需修改:
"exclude": ["node_modules", "**/*.spec.ts"]
修改后的完整tsconfig.json示例
{ "compilerOptions": { /* Language and Environment */ "target": "ES2022", "experimentalDecorators": true, /* Modules */ "module": "CommonJS", "moduleResolution":"node", "outDir": ".randoms", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, "baseUrl": ".", "paths": { "@/*": ["./*"] } }, "include": ["**/*.ts", "**/*.tsx"], "exclude": ["node_modules", "**/*.spec.ts"] }
验证编译
修改配置后重新执行tsc命令,routes/index.tsx会被编译并输出到.randoms/routes/index.js目录下。
内容的提问来源于stack exchange,提问作者zain ul din
相关产品推荐
相关产品推荐

