Node.js 16 现已提供预览版。

执行自定义构建步骤

在部署时,您可在启动应用之前通过在 package.json 文件中添加 gcp-build 脚本来执行自定义构建步骤。

执行此脚本时,可以使用 package.json 文件的 dependenciesdevDependencies 字段中的依赖项。执行自定义构建步骤后,App Engine 仅安装 package.json 文件的 dependencies 字段中声明的生产依赖项,以移除并重新生成 node_modules 文件夹。

示例

自定义构建脚本可用于预处理任务,例如预处理 CSS、缩减客户端 JavaScript 大小或者运行工具(如 webpackgulp)。

例如,要将 TypeScript 编译为 JavaScript,您的 package.json 文件可能如下所示。请注意 gcp-build 脚本:

{
  "name": "appengine-typescript",
  "description": "An example TypeScript app running on Google App Engine.",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "14.x.x"
  },
  "scripts": {
    "prepare": "npm run gcp-build",
    "pretest": "npm run gcp-build",
    "test": "mocha test/*.test.js --exit",
    "posttest": "npm run lint",
    "lint": "tslint -p .",
    "start": "node ./index.js",
    "gcp-build": "tsc -p .",
    "deploy": "gcloud app deploy"
  },
  "dependencies": {
    "@types/node": "^14.14.7",
    "express": "^4.16.3",
    "typescript": "^4.0.0"
  },
  "devDependencies": {
    "mocha": "^9.0.0",
    "wait-port": "^0.2.7",
    "@types/express": "^4.16.0",
    "chai": "^4.2.0",
    "tslint": "^6.0.0",
    "typescript": "^4.0.0"
  }
}