diff --git a/.dockerignore b/.dockerignore index a57f9f33c9..8092015d13 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,7 +8,6 @@ Dockerfile* LICENSE Procfile app-minimal -assets coverage node_modules test diff --git a/Dockerfile b/Dockerfile index 6698a4a522..284f73a124 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,17 +39,17 @@ 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:21-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 \ @@ -73,6 +73,7 @@ RUN \ # 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 @@ -168,6 +169,18 @@ RUN \ COPY --from=docker-minifier /app /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 && \ +# ls -la /app && \ +# du -hd1 /app + EXPOSE 1200 ENTRYPOINT ["dumb-init", "--"] diff --git a/lib/registry.ts b/lib/registry.ts index c6f388273f..1185b2d2c2 100644 --- a/lib/registry.ts +++ b/lib/registry.ts @@ -13,6 +13,17 @@ import { route as testRoute } from '@/routes-new/test/index'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); let modules: Record = {}; +let namespaces: Record< + string, + Namespace & { + routes: Record< + string, + Route & { + location: string; + } + >; + } +> = {}; switch (process.env.NODE_ENV) { case 'test': @@ -25,6 +36,10 @@ switch (process.env.NODE_ENV) { }, }; break; + case 'production': + // eslint-disable-next-line n/no-unpublished-require + namespaces = require('../assets/build/routes.json'); + break; default: modules = directoryImport({ targetDirectoryPath: path.join(__dirname, './routes-new'), @@ -32,43 +47,44 @@ switch (process.env.NODE_ENV) { }) as typeof modules; } -const namespaces: Record< - string, - Namespace & { - routes: Record; - } -> = {}; - -for (const module in modules) { - const content = modules[module] as - | { - route: Route; - } - | { - namespace: Namespace; - }; - const namespace = module.split('/')[1]; - if ('namespace' in content) { - namespaces[namespace] = Object.assign( - { - routes: {}, - }, - namespaces[namespace], - content.namespace - ); - } else if ('route' in content) { - if (!namespaces[namespace]) { - namespaces[namespace] = { - name: namespace, - routes: {}, - }; - } - if (Array.isArray(content.route.path)) { - for (const path of content.route.path) { - namespaces[namespace].routes[path] = content.route; +if (Object.keys(modules).length) { + for (const module in modules) { + const content = modules[module] as + | { + route: Route; + } + | { + namespace: Namespace; + }; + const namespace = module.split('/')[1]; + if ('namespace' in content) { + namespaces[namespace] = Object.assign( + { + routes: {}, + }, + namespaces[namespace], + content.namespace + ); + } else if ('route' in content) { + if (!namespaces[namespace]) { + namespaces[namespace] = { + name: namespace, + routes: {}, + }; + } + if (Array.isArray(content.route.path)) { + for (const path of content.route.path) { + namespaces[namespace].routes[path] = { + ...content.route, + location: module.split('/').slice(2).join('/'), + }; + } + } else { + namespaces[namespace].routes[content.route.path] = { + ...content.route, + location: module.split('/').slice(2).join('/'), + }; } - } else { - namespaces[namespace].routes[content.route.path] = content.route; } } } @@ -81,6 +97,10 @@ export default function (app: Hono) { for (const path in namespaces[namespace].routes) { const wrapedHandler: Handler = async (ctx) => { if (!ctx.get('data')) { + if (typeof namespaces[namespace].routes[path].handler !== 'function') { + const { route } = await import(`./routes-new/${namespace}/${namespaces[namespace].routes[path].location}`); + namespaces[namespace].routes[path].handler = route.handler; + } ctx.set('data', await namespaces[namespace].routes[path].handler(ctx)); } }; diff --git a/package.json b/package.json index a53e3880e2..5d1b32eb09 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "lint": "eslint --cache .", "prepare": "husky || true", "profiling": "NODE_ENV=production tsx --prof lib/index.ts", - "start": "tsx lib/index.ts", + "start": "cross-env NODE_ENV=production tsx lib/index.ts", "test": "npm run format:check && npm run vitest:coverage" }, "lint-staged": { diff --git a/scripts/workflow/build-routes.ts b/scripts/workflow/build-routes.ts index cdbe77870f..3ec8c3732b 100644 --- a/scripts/workflow/build-routes.ts +++ b/scripts/workflow/build-routes.ts @@ -54,3 +54,4 @@ for (const namespace in namespaces) { fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.json'), JSON.stringify(radar, null, 2)); fs.writeFileSync(path.join(__dirname, '../../assets/build/maintainers.json'), JSON.stringify(maintainers, null, 2)); +fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.json'), JSON.stringify(namespaces, null, 2));