【问题标题】:How to compile a root level .ts file outside of the tsconfig.json settings?如何在 tsconfig.json 设置之外编译根级 .ts 文件?
【发布时间】: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


    【解决方案1】:

    对于可能遇到此问题的其他人,以下是我找到的一些解决方案:

    1. [BEST SOLUTION] 重构文件夹结构 ?routes/ 重命名为 ?dist/ 然后 server.ts 从 / 移动到 src 并且 outfile 设置为 ?dist/

    这需要对我的 CI/CD 管道进行一些修改,但总体而言,它为我节省了大量时间来咬紧牙关。

    1. files 添加到tsconfig.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 下移 1 级 - 生产应用程序是从 TypeScript 编译的,因此当 tsconfig.json?src/server.ts 不是生产应用程序时,它是否稍微不适合生产并不重要使其进入构建。它们只是为了开发。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2017-09-05
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多