diff --git a/Dockerfile b/Dockerfile index b5770c960..b9be5db6f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,41 +40,44 @@ WORKDIR /ver COPY ./package.json /app/ RUN \ set -ex && \ - grep -Po '(?<="rebrowser-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 '(?<="rebrowser-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 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 && \ - # 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/package.json b/package.json index a648dbe79..c41afe723 100644 --- a/package.json +++ b/package.json @@ -153,7 +153,6 @@ "@types/etag": "1.8.4", "@types/fs-extra": "11.0.4", "@types/html-to-text": "9.0.4", - "@types/imapflow": "1.0.189", "@types/js-beautify": "1.14.3", "@types/jsdom": "27.0.0", "@types/json-bigint": "1.0.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1b6030471..4f1f230d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -336,9 +336,6 @@ importers: '@types/html-to-text': specifier: 9.0.4 version: 9.0.4 - '@types/imapflow': - specifier: 1.0.189 - version: 1.0.189 '@types/js-beautify': specifier: 1.14.3 version: 1.14.3 @@ -2364,10 +2361,6 @@ packages: '@types/http-cache-semantics@4.0.4': resolution: {integrity: sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA==} - '@types/imapflow@1.0.189': - resolution: {integrity: sha512-3I/9/rKYd4+p4tAkQ3sRtTu2EazHzvruzwgpjpM6ZLx8YHjUyqPv3rBEg5wabVWuuX7/+yHULYROQz5cgALX0g==} - deprecated: This is a stub types definition. imapflow provides its own type definitions, so you do not need this installed. - '@types/js-beautify@1.14.3': resolution: {integrity: sha512-FMbQHz+qd9DoGvgLHxeqqVPaNRffpIu5ZjozwV8hf9JAGpIOzuAf4wGbRSo8LNITHqGjmmVjaMggTT5P4v4IHg==} @@ -8155,10 +8148,6 @@ snapshots: '@types/http-cache-semantics@4.0.4': {} - '@types/imapflow@1.0.189': - dependencies: - imapflow: 1.0.199 - '@types/js-beautify@1.14.3': {} '@types/jsdom@27.0.0': diff --git a/scripts/docker/minify-docker.js b/scripts/docker/minify-docker.js index f40d1c81e..f741342c0 100644 --- a/scripts/docker/minify-docker.js +++ b/scripts/docker/minify-docker.js @@ -1,25 +1,24 @@ /* 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'; +const __dirname = import.meta.dirname; // !!! if any new dependencies are added, update the Dockerfile !!! 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 = ['lib/index.ts', 'api/vercel.js'].map((file) => path.join(projectRoot, file)); +const files = ['dist/index.js', 'node_modules/cross-env/dist/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);