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

问题描述 投票:0回答:2

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

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

src
-server
-client

tsconfig
package.json 

我正在使用包裹捆绑程序来编译和捆绑客户端文件夹。我想使用打字稿编译器或tsc来编译节点js服务器文件夹。是否可以从特定目录运行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": "."
    }
}
typescript tsc
2个回答
0
投票

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

特定的tsconfig.json应该看起来像这样:

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


0
投票

我最终使用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",
© www.soinside.com 2019 - 2024. All rights reserved.