From e9013a23139a692fe93b6a2ad4c5cc85b8020192 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 23 Apr 2025 02:42:25 +0800 Subject: [PATCH] chore: minify docker (#18914) * chore: minify docker * chore: bring back minify script --- Dockerfile | 50 ++++++++++++++++++--------------- scripts/docker/minify-docker.js | 32 ++++++++++----------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/Dockerfile b/Dockerfile index bd96cee150..3b0fa1c49f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,9 +17,10 @@ RUN \ pnpm config set registry https://registry.npmmirror.com ; \ fi; -COPY ./tsconfig.json /app/ COPY ./pnpm-lock.yaml /app/ COPY ./package.json /app/ +COPY ./tsconfig.json /app/ +COPY ./tsdown.config.ts /app/ # lazy install Chromium to avoid cache miss, only install production dependencies to minimize the image size RUN \ @@ -40,41 +41,44 @@ WORKDIR /ver COPY ./package.json /app/ RUN \ set -ex && \ - grep -Po '(?<="puppeteer": ")[^\s"]*(?=")' /app/package.json | tee /ver/.puppeteer_version - # grep -Po '(?<="@vercel/nft": ")[^\s"]*(?=")' /app/package.json | tee /ver/.nft_version && \ - # grep -Po '(?<="fs-extra": ")[^\s"]*(?=")' /app/package.json | tee /ver/.fs_extra_version + grep -Po '(?<="puppeteer": ")[^\s"]*(?=")' /app/package.json | tee /ver/.puppeteer_version && \ + grep -Po '(?<="@vercel/nft": ")[^\s"]*(?=")' /app/package.json | tee /ver/.nft_version && \ + grep -Po '(?<="fs-extra": ")[^\s"]*(?=")' /app/package.json | tee /ver/.fs_extra_version # --------------------------------------------------------------------------------------------------------------------- FROM node:22-bookworm-slim AS docker-minifier # The stage is used to further reduce the image size by removing unused files. -WORKDIR /app -# COPY --from=dep-version-parser /ver/* /minifier/ +WORKDIR /minifier +COPY --from=dep-version-parser /ver/* /minifier/ -# ARG USE_CHINA_NPM_REGISTRY=0 -# RUN \ -# set -ex && \ -# if [ "$USE_CHINA_NPM_REGISTRY" = 1 ]; then \ -# npm config set registry https://registry.npmmirror.com && \ -# yarn config set registry https://registry.npmmirror.com && \ -# pnpm config set registry https://registry.npmmirror.com ; \ -# fi; \ -# corepack enable pnpm && \ -# pnpm add @vercel/nft@$(cat .nft_version) fs-extra@$(cat .fs_extra_version) --save-prod +ARG USE_CHINA_NPM_REGISTRY=0 +RUN \ + set -ex && \ + if [ "$USE_CHINA_NPM_REGISTRY" = 1 ]; then \ + npm config set registry https://registry.npmmirror.com && \ + yarn config set registry https://registry.npmmirror.com && \ + pnpm config set registry https://registry.npmmirror.com ; \ + fi; \ + npm install -g corepack@latest && \ + corepack use pnpm@latest-9 && \ + pnpm add @vercel/nft@$(cat .nft_version) fs-extra@$(cat .fs_extra_version) --save-prod COPY . /app COPY --from=dep-builder /app /app +WORKDIR /app RUN \ set -ex && \ - # cp /app/scripts/docker/minify-docker.js /minifier/ && \ - # export PROJECT_ROOT=/app && \ - # node /minifier/minify-docker.js && \ - # rm -rf /app/node_modules /app/scripts && \ - # mv /app/app-minimal/node_modules /app/ && \ - # rm -rf /app/app-minimal && \ - npm run build && \ + pnpm build && \ + find /app/lib -mindepth 1 -not -path "/app/lib/assets*" -exec rm -rf {} \; 2>/dev/null || true && \ + cp /app/scripts/docker/minify-docker.js /minifier/ && \ + export PROJECT_ROOT=/app && \ + node /minifier/minify-docker.js && \ + rm -rf /app/node_modules /app/scripts && \ + mv /app/app-minimal/node_modules /app/ && \ + rm -rf /app/app-minimal && \ ls -la /app && \ du -hd1 /app diff --git a/scripts/docker/minify-docker.js b/scripts/docker/minify-docker.js index f40d1c81e3..7890d45491 100644 --- a/scripts/docker/minify-docker.js +++ b/scripts/docker/minify-docker.js @@ -1,25 +1,23 @@ /* eslint-disable no-console */ -const fs = require('fs-extra'); -const path = require('path'); -const { nodeFileTrace } = require('@vercel/nft'); +import fs from 'fs-extra'; +import path from 'node:path'; +import { nodeFileTrace } from '@vercel/nft'; // !!! if any new dependencies are added, update the Dockerfile !!! -const projectRoot = path.resolve(process.env.PROJECT_ROOT || path.join(__dirname, '../..')); +const projectRoot = path.resolve(process.env.PROJECT_ROOT || path.join(path.dirname(new URL(import.meta.url).pathname), '../..')); const resultFolder = path.join(projectRoot, 'app-minimal'); // no need to resolve, ProjectRoot is always absolute -const files = ['lib/index.ts', 'api/vercel.js'].map((file) => path.join(projectRoot, file)); +const files = ['dist/index.js', 'api/vercel.ts', 'node_modules/cross-env/src/bin/cross-env.js', 'node_modules/.bin/cross-env'].map((file) => path.join(projectRoot, file)); -(async () => { - console.log('Start analyzing, project root:', projectRoot); - const { fileList: fileSet } = await nodeFileTrace(files, { - base: projectRoot, - }); - let fileList = [...fileSet]; - console.log('Total touchable files:', fileList.length); - fileList = fileList.filter((file) => file.startsWith('node_modules/')); // only need node_modules - console.log('Total files need to be copied (touchable files in node_modules/):', fileList.length); - console.log('Start copying files, destination:', resultFolder); - return Promise.all(fileList.map((e) => fs.copy(path.join(projectRoot, e), path.join(resultFolder, e)))); -})().catch((error) => { +console.log('Start analyzing, project root:', projectRoot); +const { fileList: fileSet } = await nodeFileTrace(files, { + base: projectRoot, +}); +let fileList = [...fileSet]; +console.log('Total touchable files:', fileList.length); +fileList = fileList.filter((file) => file.startsWith('node_modules/')); // only need node_modules +console.log('Total files need to be copied (touchable files in node_modules/):', fileList.length); +console.log('Start copying files, destination:', resultFolder); +await Promise.all(fileList.map((e) => fs.copy(path.join(projectRoot, e), path.join(resultFolder, e)))).catch((error) => { // fix unhandled promise rejections console.error(error, error.stack); process.exit(1);