【发布时间】:2021-03-05 18:21:35
【问题描述】:
几个月来,我一直在处理将我的 .ts 和 .js 文件并排放置在每个文件夹中的问题,因为该应用正在生产中,从 JS 到 TS 的转换造成了这种不便,但总体上仍然提供了更多好处。
经过一些修改和阅读文档后,我得到了正确编译的文件夹结构,但有 1 个文件在不破坏结构的情况下无法包含,它是根级 server.ts 文件。
项目结构(当前编译):
“问题”文件显示为 ????
/????server (root)
????server.js
????server.ts (problem file)
/????routes (dist folder)
/????api
/????v1
index.js
/????accountDataFunctions
duplicates.js
notations.js
/????accountImportFunctions
dataIntegrity.js
/????dataFormatting
addressValidation.js
emailValidation.js
...
/????sqlQueryGeneration
selectQuery.js
updateQuery.js
/????src (dev folder)
/????api
/????v1
index.ts
/????accountDataFunctions
duplicates.ts
notations.ts
/????accountImportFunctions
dataIntegrity.ts
/????dataFormatting
addressValidation.ts
emailValidation.ts
...
/????sqlQueryGeneration
selectQuery.ts
updateQuery.ts
如果我在tsconfig.json 中包含server.ts
它会这样编译:
不良/其他编译:
/????routes (dist folder)
/????src/????api/????v1
[...folder structure]
server.js
/????api/????v1
(empty)
当前配置正确执行所有操作,只是它忽略了 /????server 文件夹中根级别的 server.ts。
tsconfig.json:
/* tsconfig.json */
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"outDir": "./routes", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"resolveJsonModule": true,
"removeComments": true, /* Do not emit comments to output. */
"strict": false, /* Enable all strict type-checking options. */
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
"rootDirs": ["./","./src"], /* List of root folders whose combined content represents the structure of the project at runtime. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": ["node_modules"],
"include": [
"src/api/v1/index.ts",
"src/api/v1/**/*.ts"
]
}
我已经引用过的 StackOverflow 链接:
其中一些帮助我达到了这一点,但它们的不同之处足以让我不知道如何适应它们。
tsconfig.json file outside of project directory
How to compile a specific file with tsc using the paths compiler option
group typescript files into more than one outFile
How can I specify multiple source folders in my tsconfig.json?
理想情况
我运行concurrently如下:
/* package.json */
"scripts": {
"start": "tsc && node server.js",
⭐"serve": "concurrently \"nodemon run server.js\" \"tsc -w\""
},
最好我希望能够添加一个额外的 tsc 命令来观看该单个文件
或
我可以在 IF 中添加另一个 tsconfig.json 文件,原来的 tsconfig.json 文件中没有更简单的解决方案。
值得注意的并发症
文件夹/文件结构
我需要将文件夹结构????server/????routes/????api/????v1/* 与????server 中的相对server.js 文件一起保留,因为CI/CD 管道以及它已经投入生产的事实。
使用 server.js 代替 server.ts
当尝试排除 server.ts 时,会弹出大量导入/esmodule 错误。 server.js 最终是控制一切的文件,包括路由、守护进程和权限。这是一个小文件,但它控制着很多东西,所以server.ts 和整个服务器中的许多其他.ts 文件之间有很多连接。
【问题讨论】:
标签: javascript node.js typescript express compilation