如何修复Google Cloud Build中的typescript引用错误

nszi6y05  于 2023-05-01  发布在  TypeScript
关注(0)|答案(1)|浏览(144)

我有一个typescript monorepo,其中一个vue 3应用程序从应用程序范围内的ad hoc模块/包导入(自定义)类型和配置常量。e:@myapp/types@myapp/config。这些软件包是私有的,不是从npm注册表导入的。我在本地导入、构建或提供应用程序方面没有问题。但是,在Cloud Build中,我的构建总是失败,并显示以下消息:

这里是tsconfig。json文件从“进口商”仓库,我.e我的Vue应用程序使用类型并通过管道:
tsconfig.json:

{
  "files": [],
  "compilerOptions": {
    "forceConsistentCasingInFileNames": true,
    "strict": true
  },
  "references": [
    {
      "path": "./tsconfig.vite-config.json"
    },
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.vitest.json"
    },
    {
      "path": "../../packages/types/tsconfig.json"
    },
    {
      "path": "../../packages/config/tsconfig.json"
    }
  ]
}

tsconfig.app.json:

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
  "exclude": ["src/**/__tests__/*"],
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "composite": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@myapp/config": ["../../packages/config"],
      "@myapp/types": ["../../packages/types"]
    }
  }
}

下面是monorepo的结构:

  • root(tsconfig.base.json)
  • 应用程序
  • ui〈== consumer(tsconfig.app.json + tsconfig.json)
  • Package
  • types〈= custom types(tsconfig.json)
  • config〈=自定义类型

作为一个参考,这里是tsconfig。json在types模块中可用:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "target": "ESNext",
    "module": "ES6",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true,
    "emitDeclarationOnly": true
  },
  "files": ["index.ts"],
  "display": "Types"
}

这里是tsconfig。base.json位于repo的根目录:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "baseUrl": ".",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "composite": true,
    "declaration": true,
    "declarationMap": true,
    "paths": {
      "@myapp/types": ["./packages/types"],
      "@myapp/config": ["./packages/config"],
      "@myapp/ui": ["./packages/ui"],
      "@myapp/ui-b2b": ["./apps/ui-b2b/src"],
      "@myapp/ui-partners": ["./apps/ui-patners/src"],
      "@myapp/api": ["./apps/api/src"]
    }
  }
}

最后是我的cloudbuild yaml文件:

steps:
  - name: node:16
    entrypoint: npm
    args: ["install"]
  - name: node:16
    dir: "apps/ui-b2b"
    entrypoint: npm
    env:
      - "DEPLOYMENT_TARGET=$_DEPLOYMENT_TARGET"
      - "VITE_RECAPTCHA_SITE_KEY=$_VITE_RECAPTCHA_SITE_KEY"
      - "VITE_DYNAMIC_LINKS_DOMAIN=$_VITE_DYNAMIC_LINKS_DOMAIN"
      - "VITE_API_URL=$_VITE_API_URL"
      - "VITE_DISPOSABLE_API_KEY=$_VITE_DISPOSABLE_API_KEY"
      - "VITE_GCP_AUTH_TENANT_ID=$_VITE_GCP_AUTH_TENANT_ID"
      - "VITE_FIREBASE_CONFIG=$_VITE_FIREBASE_CONFIG"
      - "VITE_BASE_DOMAIN=$_VITE_BASE_DOMAIN"
      - "VITE_LOCAL=$_VITE_LOCAL"
      - "VITE_STRIPE_ALLOW_PROMOTION_CODES=$_VITE_STRIPE_ALLOW_PROMOTION_CODES"
      - "VITE_TOAST_DURATION=$_VITE_TOAST_DURATION"
      - "VITE_SEARCH_API_KEY=$_VITE_SEARCH_API_KEY"
      - "VITE_SEARCH_API_HOST=$_VITE_SEARCH_API_HOST"
      - "VITE_SEARCH_API_PORT=$_VITE_SEARCH_API_PORT"
      - "VITE_SEARCH_API_PROTOCOL=$_VITE_SEARCH_API_PROTOCOL"
    args: ["run", "build:development"]
  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    entrypoint: "bash"
    dir: "apps/ui-b2b"
    args:
      [
        "-c",
        "gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy",
      ]
timeout: "1600s"

我已经尝试了几件事,如更新路径,删除引用等。但所有这些都会导致其它或新的错误。
你知道为什么会这样吗?或者怎么解决?
先谢谢你,

vh0rcniy

vh0rcniy1#

结果发现 typescript 错误是树掩盖了森林。我还不知道为什么会发生这种事,但这就是发生的事情。
看起来,当使用gcloud app deploy命令部署到app engine时,app engine会在实际部署之前默认尝试构建应用程序。最后我得到了以下结果:

  • 步骤1:npm i
  • 步骤2:npm build for dev
  • 第3步:部署(在部署之前与默认npm构建器结合)

我的猜测是,在步骤3中,应用程序所依赖的其他TS模块/包已经被管道丢弃,因为应用程序已经构建好了。
因此,我通过将"gcp-build": ""添加到我的package.json中来静音应用引擎的默认构建行为,如下所示:
https://cloud.google.com/appengine/docs/standard/nodejs/runtime#npm_build_script

构建和部署现在都很好。

相关问题