Skip to content

Commit

Permalink
test: use @vercel/ncc to bundle and eliminate dead code for smaller c…
Browse files Browse the repository at this point in the history
…ontainers (#513)
  • Loading branch information
aabmass authored Jan 19, 2023
1 parent 6f3fa8b commit 4230ea2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.git
**/node_modules/
**/Dockerfile
**/dist/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ node_modules/
npm-debug.log
.nyc_output/
build/
dist/

#backup files
*--
Expand Down
19 changes: 10 additions & 9 deletions cloudbuild-e2e-cloud-functions-gen2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ steps:
# bootstrap only the packages needed for e2e-test-server
npx lerna bootstrap --scope=@google-cloud/e2e-test-server --include-dependencies
# build tarballs for the dependencies to include in the zip
dependencies=$(
npx lerna list --scope=@google-cloud/e2e-test-server --include-dependencies --json | jq '.[].location' | xargs
)
# builds a small self contained bundle with @vercel/ncc under dist/
cd e2e-test-server
npm pack $dependencies
# install them to point package.json at the local tarballs
npm install --save *.tgz
# --external will avoid bundling functions-framework. Needed so we only have one copy of
# global variables in that package when GCF executes it.
npm run bundle -- --external @google-cloud/functions-framework
# zip it up into a source zip for Cloud Functions, excluding node_modules
# create a new fake package.json in dist/ and zip it up into a source zip for Cloud
# Functions
cd dist
npm init -y
npm install --save @google-cloud/functions-framework
zip -r function-source.zip . -x "node_modules*"
# Run the test
Expand All @@ -43,7 +44,7 @@ steps:
env: ["PROJECT_ID=$PROJECT_ID"]
args:
- cloud-functions-gen2
- --functionsource=/workspace/e2e-test-server/function-source.zip
- --functionsource=/workspace/e2e-test-server/dist/function-source.zip
- --runtime=nodejs16
- --entrypoint=cloudFunctionHandler

Expand Down
12 changes: 4 additions & 8 deletions e2e-test-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ RUN npm install --ignore-scripts
COPY . ./
# bootstrap only the packages needed for e2e test server
RUN npx lerna@latest bootstrap --scope=@google-cloud/e2e-test-server --include-dependencies
# dereference lerna symlinks so we don't need to copy out the whole monorepo
RUN TMPDIR=$(mktemp -d) && \
cp --dereference --recursive e2e-test-server/node_modules/@google-cloud $TMPDIR/ && \
rm -rf e2e-test-server/node_modules/@google-cloud && \
mv $TMPDIR/@google-cloud e2e-test-server/node_modules/@google-cloud
RUN npm prune --omit=dev
# builds a small self contained bundle with @vercel/ncc under dist/
RUN cd e2e-test-server && npm run bundle

FROM node-base
USER node
COPY --from=build-base --chown=node:node $SRC/e2e-test-server ./
ENTRYPOINT ["node", "build/src/index.js"]
COPY --from=build-base --chown=node:node $SRC/e2e-test-server/dist ./
ENTRYPOINT ["node", "index.js"]
16 changes: 16 additions & 0 deletions e2e-test-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion e2e-test-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
"pretest": "npm run compile",
"posttest": "npm run lint",
"server": "node build/src/index.js",
"gcp-build": "npm run compile"
"bundle": "ncc build src/index.ts"
},
"devDependencies": {
"@types/express": "^4.17.15",
"@types/node": "14.18.36",
"@vercel/ncc": "^0.36.0",
"gts": "3.1.1",
"typescript": "4.9.4"
},
Expand Down

0 comments on commit 4230ea2

Please sign in to comment.