是否可以在子文件夹中 运行 打字稿编译器?

Is it possible to run he typescript compiler in a child folder?

我有一个具有以下文件结构的项目

builds
-dev
--public
--private
-production
--public
--private

src
-server
-client

tsconfig
package.json 

我正在使用 parcel-bundler 来编译和捆绑客户端文件夹。而我想使用typescript编译器或tsc来编译node js server文件夹。是否可以从特定目录 运行 tsc?

  "scripts": {
    "dev": "tsc (directory name goes here so the server directory) && parcel ./src/client/index.html --open --out-dir ./builds/development/public"
  }

root tsconfig

 {
      "compilerOptions": {
        /* Basic Options */
        "target": "ES5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
        "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
        "lib": ["DOM", "ES2018"],                             /* Specify library files to be included in the compilation. */
        "allowJs": false,                       /* Allow javascript files to be compiled. */
        "checkJs": false,                       /* Report errors in .js files. */
        "sourceMap": true,                     /* Generates corresponding '.map' file. */
        "removeComments": true,                /* Do not emit comments to output. */
        "noEmit": true,                        /* Do not emit outputs. */
        "declaration": true,

        /* Strict Type-Checking Options */
        "strict": true,                           /* Enable all strict type-checking options. */
        "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
        "strictNullChecks": true,              /* Enable strict null checks. */
        "strictFunctionTypes": true,           /* Enable strict checking of function types. */
        "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
        "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
        "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
        "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

        /* Additional Checks */
        "noUnusedLocals": true,                /* Report errors on unused locals. */
        "noUnusedParameters": true,            /* Report errors on unused parameters. */
        "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
        "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

        /* Module Resolution Options */
        "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
        // "paths": {
        //   "~*": ["./*"]
        // },
        "typeRoots": ["node_modules/@types"],                       /* List of folders to include type definitions from. */
        "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
        "forceConsistentCasingInFileNames": true,  /* Disallow inconsistently-cased references to the same file. */
        "experimentalDecorators": true
      }
    }

服务器 tsconfig

{
    "extends": "../../tsconfig",
    "compilerOptions": {
        "outDir": "../../builds/development/private",
        "rootDir": "."
    }
}

创建一个扩展您的根 tsconfig 的 tsconfig,并将其放在您要构建的文件夹中。在该 tsconfig 中包含该文件夹中的文件。然后在脚本中指定 tsconfig。

具体的 tsconfig.json 应该是这样的:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../builds/development/private",
    "noEmit": false
  },
  "include": [
    "./**/*.ts"
  ],
  "exclude": [
    "./**/*.spec.ts"
  ]
}

我最后用 Parcel 和 运行 他们同时做机器人

"dev": "concurrently --kill-others \"npm run watch-client\" \"npm run watch-server\" \"npm run serve\"",
"watch-client": "parcel ./src/client/index.html --open --out-dir ./builds/development/public",
"watch-server": "parcel ./src/server/index.ts --out-dir ./builds/development/private --target node",
"serve": "nodemon ./builds/development/private/index.js",