无服务器:并非所有打字稿文件都编译在同一目录中

Kar*_*ela 2 typescript serverless-framework

最奇怪的事情正在发生。我\xe2\x80\x99m 使用打字稿,并不是单个目录中的所有打字稿文件都被编译,即我的源代码树中有一个目录\xe2\x80\x99s,其中一些 .ts 文件被编译为 . js 和其他的是\xe2\x80\x99t。

\n\n

当我运行简单的 tsc 时,所有文件都会被编译,但是当我运行 \xe2\x80\x98serverless offline start\xe2\x80\x99 时,并非所有文件都会被编译。

\n\n

有人知道如何修复吗?

\n\n

我的 serverless.yml 的基础知识:

\n\n
package:\n  include:\n    - src/**/*\n\nfunctions:\n  graphql:\n    handler: src/graphql.handler\n    events:\n      - http:\n          path: graphql\n          method: post\n          cors: true\n\nplugins:\n  - serverless-plugin-typescript\n  - serverless-offline\n
Run Code Online (Sandbox Code Playgroud)\n\n

tsconfig.json:

\n\n
{\n  "compilerOptions": {\n    /* Basic Options */\n    "target": "es2016", /* Specify ECMAScript target version: \'ES3\' (default), \'ES5\', \'ES2015\', \'ES2016\', \'ES2017\',\'ES2018\' or \'ESNEXT\'. */\n    "module": "commonjs", /* Specify module code generation: \'none\', \'commonjs\', \'amd\', \'system\', \'umd\', \'es2015\', or \'ESNext\'. */\n    "lib": [\n      "es2016",\n      "dom",\n      "esnext.asynciterable"\n    ],\n    "removeComments": true, /* Do not emit comments to output. */\n    "strict": true, /* Enable all strict type-checking options. */\n    "noImplicitAny": true, /* Raise error on expressions and declarations with an implied \'any\' type. */\n    "strictNullChecks": true, /* Enable strict null checks. */\n    "strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */\n    "noUnusedLocals": true, /* Report errors on unused locals. */\n    "noUnusedParameters": false, /* Report errors on unused parameters. */\n    "moduleResolution": "node", /* Specify module resolution strategy: \'node\' (Node.js) or \'classic\' (TypeScript pre-1.6). */\n    "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies \'allowSyntheticDefaultImports\'. */\n    "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */\n    "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */\n  },\n  "include": [\n    "src/**/*"\n  ],\n  "types": [\n    "typePatches",\n    "nodes"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

x3m*_*mka 6

serverless-plugin-typescript插件仅从从无服务器函数处理程序获取的根文件(在您的情况下为 src/graphql.handler)和根文件 src/graphql.ts 开始编译 ts。依赖关系链中未引用的所有文件都将被忽略。

无服务器package.include选项只是额外复制指定位置,而无需 ts 编译。

因此,如果您错过了 .build 目录中的某些文件,请确保将其导入处理程序中的某个位置。当一些库从配置的 glob 动态加载文件时,我遇到了类似的问题./some_dir/*.ts(据我记得 typeorm 实体或迁移)。解决方案是制作./some_dir/index.ts并导出该目录中的所有文件。然后就import * as all_some_dir from './some_dir'在你的处理程序中。