首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Serverless:并非所有的typescript文件都在同一目录中编译

Serverless:并非所有的typescript文件都在同一目录中编译
EN

Stack Overflow用户
提问于 2018-07-26 22:38:52
回答 1查看 1.1K关注 0票数 1

最奇怪的事情正在发生。我正在使用typescript,但并不是所有的typescript文件都在一个目录中编译,也就是说,在我的源码树中有一个目录,其中一些.ts文件正在被编译成.js,而其他的则没有。

当我运行一个简单的tsc时,所有的文件都被编译了,但是当我运行‘serverless offline start’时,并不是所有的文件都被编译。

有人知道怎么修吗?

我的serverless.yml基础知识:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
  "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"
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2019-09-06 05:04:37

serverless-plugin-typescript插件仅从从无中断函数处理程序中获取的根文件开始编译ts,在本例中为src/raphql.handler,根文件为src/raphql.ts。所有未在依存关系链中引用的文件将被忽略。

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51541513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档