如何在tsconfig. js之外编译根级.ts文件.json设置?



几个月来,我一直在处理我的。TS和。JS文件并排在每个文件夹内,因为应用程序在生产中,从JS到TS的转换造成了这种不便,同时仍然提供了更多的好处。

我得到了文件夹结构编译正确后,一点修补和阅读文档,但有一个文件,我不能包括不破坏结构,它是根级服务器。ts文件。

项目结构(当前编译):

'problem'文件显示为📛

/📁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文件

如何使用路径编译器选项用tsc编译一个特定的文件

将typescript文件分组到多个outFile

我如何在我的tsconfig.json中指定多个源文件夹?

理想情况我运行concurrently如下:

/* package.json */
"scripts": {
"start": "tsc && node server.js",
⭐"serve": "concurrently "nodemon run server.js" "tsc -w""
},

我希望能够添加一个额外的tsc命令来监视单个文件

我可以添加另一个tsconfig.json文件在IF在原始tsconfig.json文件中没有更简单的解决方案。

值得注意的并发症

文件夹/文件结构

我需要在📁server中保持文件夹结构📁server/📁routes/📁api/📁v1/*和相对的server.js文件,因为CI/CD管道和这已经在生产中的事实。

使用>server.jsserver.ts

当试图排除server.ts时,会弹出大量的import/esmodule错误。server.js最终是控制包括路由、守护进程和权限在内的所有内容的文件。这是一个小文件,但它控制了很多东西,所以在整个服务器上server.ts和许多其他.ts文件之间有很多连接。

对于可能遇到这个问题的其他人,这里有一些我发现的解决方案:

  1. [BEST SOLUTION]重构文件夹结构📁routes/被重命名为📁dist/,然后是server。ts从/移到src,输出文件设置为📁dist/

这需要对我的CI/CD管道进行一些修改,但总的来说,它为我节省了大量的时间。

  1. 添加filestsconfig.json文件:
/* tsconfig.json */
{
"compilerOptions": {
"target": "es6",
...
},
"exclude": ["node_modules"],
"include": [
"src/api/v1/index.ts",
"src/api/v1/**/*.ts"
],
"files": ["server.ts", "api/v1/index.ts"]
}
  1. 如果可能的话,把tsconfig.json往下移一级——生产应用程序是从TypeScript编译的,所以当tsconfig.json📁src/server.ts没有进入构建时,如果它稍微不太适合生产也没关系。他们只是为了发展。

我的文件夹结构设置如下:

📁project
tsconfig.json (include server, exclude client)
📁server
📁client

最新更新