chore: minify docker (#18914)

* chore: minify docker

* chore: bring back minify script
This commit is contained in:
Tony
2025-04-23 02:42:25 +08:00
committed by GitHub
parent 51d4b4fe04
commit e9013a2313
2 changed files with 42 additions and 40 deletions

View File

@@ -17,9 +17,10 @@ RUN \
pnpm config set registry https://registry.npmmirror.com ; \ pnpm config set registry https://registry.npmmirror.com ; \
fi; fi;
COPY ./tsconfig.json /app/
COPY ./pnpm-lock.yaml /app/ COPY ./pnpm-lock.yaml /app/
COPY ./package.json /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 # lazy install Chromium to avoid cache miss, only install production dependencies to minimize the image size
RUN \ RUN \
@@ -40,41 +41,44 @@ WORKDIR /ver
COPY ./package.json /app/ COPY ./package.json /app/
RUN \ RUN \
set -ex && \ set -ex && \
grep -Po '(?<="puppeteer": ")[^\s"]*(?=")' /app/package.json | tee /ver/.puppeteer_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 '(?<="@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 '(?<="fs-extra": ")[^\s"]*(?=")' /app/package.json | tee /ver/.fs_extra_version
# --------------------------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------------------------
FROM node:22-bookworm-slim AS docker-minifier FROM node:22-bookworm-slim AS docker-minifier
# The stage is used to further reduce the image size by removing unused files. # The stage is used to further reduce the image size by removing unused files.
WORKDIR /app WORKDIR /minifier
# COPY --from=dep-version-parser /ver/* /minifier/ COPY --from=dep-version-parser /ver/* /minifier/
# ARG USE_CHINA_NPM_REGISTRY=0 ARG USE_CHINA_NPM_REGISTRY=0
# RUN \ RUN \
# set -ex && \ set -ex && \
# if [ "$USE_CHINA_NPM_REGISTRY" = 1 ]; then \ if [ "$USE_CHINA_NPM_REGISTRY" = 1 ]; then \
# npm config set registry https://registry.npmmirror.com && \ npm config set registry https://registry.npmmirror.com && \
# yarn config set registry https://registry.npmmirror.com && \ yarn config set registry https://registry.npmmirror.com && \
# pnpm config set registry https://registry.npmmirror.com ; \ pnpm config set registry https://registry.npmmirror.com ; \
# fi; \ fi; \
# corepack enable pnpm && \ npm install -g corepack@latest && \
# pnpm add @vercel/nft@$(cat .nft_version) fs-extra@$(cat .fs_extra_version) --save-prod corepack use pnpm@latest-9 && \
pnpm add @vercel/nft@$(cat .nft_version) fs-extra@$(cat .fs_extra_version) --save-prod
COPY . /app COPY . /app
COPY --from=dep-builder /app /app COPY --from=dep-builder /app /app
WORKDIR /app
RUN \ RUN \
set -ex && \ set -ex && \
# cp /app/scripts/docker/minify-docker.js /minifier/ && \ pnpm build && \
# export PROJECT_ROOT=/app && \ find /app/lib -mindepth 1 -not -path "/app/lib/assets*" -exec rm -rf {} \; 2>/dev/null || true && \
# node /minifier/minify-docker.js && \ cp /app/scripts/docker/minify-docker.js /minifier/ && \
# rm -rf /app/node_modules /app/scripts && \ export PROJECT_ROOT=/app && \
# mv /app/app-minimal/node_modules /app/ && \ node /minifier/minify-docker.js && \
# rm -rf /app/app-minimal && \ rm -rf /app/node_modules /app/scripts && \
npm run build && \ mv /app/app-minimal/node_modules /app/ && \
rm -rf /app/app-minimal && \
ls -la /app && \ ls -la /app && \
du -hd1 /app du -hd1 /app

View File

@@ -1,25 +1,23 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
const fs = require('fs-extra'); import fs from 'fs-extra';
const path = require('path'); import path from 'node:path';
const { nodeFileTrace } = require('@vercel/nft'); import { nodeFileTrace } from '@vercel/nft';
// !!! if any new dependencies are added, update the Dockerfile !!! // !!! 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 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);
console.log('Start analyzing, project root:', projectRoot); const { fileList: fileSet } = await nodeFileTrace(files, {
const { fileList: fileSet } = await nodeFileTrace(files, {
base: projectRoot, base: projectRoot,
}); });
let fileList = [...fileSet]; let fileList = [...fileSet];
console.log('Total touchable files:', fileList.length); console.log('Total touchable files:', fileList.length);
fileList = fileList.filter((file) => file.startsWith('node_modules/')); // only need node_modules 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('Total files need to be copied (touchable files in node_modules/):', fileList.length);
console.log('Start copying files, destination:', resultFolder); console.log('Start copying files, destination:', resultFolder);
return Promise.all(fileList.map((e) => fs.copy(path.join(projectRoot, e), path.join(resultFolder, e)))); await Promise.all(fileList.map((e) => fs.copy(path.join(projectRoot, e), path.join(resultFolder, e)))).catch((error) => {
})().catch((error) => {
// fix unhandled promise rejections // fix unhandled promise rejections
console.error(error, error.stack); console.error(error, error.stack);
process.exit(1); process.exit(1);