diff --git a/Dockerfile b/Dockerfile index 3b0fa1c49f..bd96cee150 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,10 +17,9 @@ 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 \ @@ -41,44 +40,41 @@ 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 /minifier -COPY --from=dep-version-parser /ver/* /minifier/ +WORKDIR /app +# 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; \ - 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 +# 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 COPY . /app COPY --from=dep-builder /app /app -WORKDIR /app RUN \ set -ex && \ - 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 && \ + # 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 && \ ls -la /app && \ du -hd1 /app diff --git a/scripts/docker/minify-docker.js b/scripts/docker/minify-docker.js index 7890d45491..f40d1c81e3 100644 --- a/scripts/docker/minify-docker.js +++ b/scripts/docker/minify-docker.js @@ -1,23 +1,25 @@ /* eslint-disable no-console */ -import fs from 'fs-extra'; -import path from 'node:path'; -import { nodeFileTrace } from '@vercel/nft'; +const fs = require('fs-extra'); +const path = require('path'); +const { nodeFileTrace } = require('@vercel/nft'); // !!! if any new dependencies are added, update the Dockerfile !!! -const projectRoot = path.resolve(process.env.PROJECT_ROOT || path.join(path.dirname(new URL(import.meta.url).pathname), '../..')); +const projectRoot = path.resolve(process.env.PROJECT_ROOT || path.join(__dirname, '../..')); const resultFolder = path.join(projectRoot, 'app-minimal'); // no need to resolve, ProjectRoot is always absolute -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)); +const files = ['lib/index.ts', 'api/vercel.js'].map((file) => path.join(projectRoot, file)); -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) => { +(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) => { // fix unhandled promise rejections console.error(error, error.stack); process.exit(1);