最奇怪的事情正在发生。我正在使用 typescript ,但并不是我在一个目录中的所有 typescript 文件都得到编译,即我的源代码树中有一个目录,其中一些 .ts 文件被编译为 .js,而其他文件则没有。
当我运行一个简单的 tsc 时,所有文件都被编译,但是当我运行“serverless offline start”时,并不是所有文件都被编译。
有人知道怎么解决吗?
我的 serverless.yml 的基础知识:
package:
include:
- src/**/*
functions:
graphql:
handler: src/graphql.handler
events:
- http:
path: graphql
method: post
cors: true
plugins:
- serverless-plugin-typescript
- serverless-offline
tsconfig.json:
{
"compilerOptions": {
/* Basic Options */
"target": "es2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [
"es2016",
"dom",
"esnext.asynciterable"
],
"removeComments": true, /* Do not emit comments to output. */
"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. */
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": false, /* Report errors on unused parameters. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": [
"src/**/*"
],
"types": [
"typePatches",
"nodes"
]
}
最佳答案
serverless-plugin-typescript
插件仅从从无服务器函数处理程序中获取的根文件开始编译 ts,在您的例子中是 src/graphql.handler 和根文件 src/graphql.ts。依赖链中未引用的所有文件都将被忽略。
serverless package.include
选项只是在没有 ts 编译的情况下额外复制指定的位置。
因此,如果您在 .build 目录中遗漏了某些文件,请确保将其导入处理程序中的某个位置。当一些库从配置的 glob 动态加载文件时,我遇到了类似的问题,比如 ./some_dir/*.ts
(我记得 typeorm 实体或迁移)。解决方案是制作 ./some_dir/index.ts
并导出该目录中的所有文件。然后只需在您的处理程序中 import * as all_some_dir from './some_dir'
即可。
关于typescript - 无服务器 : Not all typescript files compiled within the same directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51541513/