feat: lazy load for production

This commit is contained in:
DIYgod
2024-03-09 01:31:42 +08:00
parent e0df7f11ac
commit fe320c1153
5 changed files with 76 additions and 43 deletions

View File

@@ -8,7 +8,6 @@ Dockerfile*
LICENSE LICENSE
Procfile Procfile
app-minimal app-minimal
assets
coverage coverage
node_modules node_modules
test test

View File

@@ -39,17 +39,17 @@ 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:21-bookworm-slim AS docker-minifier FROM node:21-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 /minifier WORKDIR /app
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 \
@@ -73,6 +73,7 @@ RUN \
# rm -rf /app/node_modules /app/scripts && \ # rm -rf /app/node_modules /app/scripts && \
# mv /app/app-minimal/node_modules /app/ && \ # mv /app/app-minimal/node_modules /app/ && \
# rm -rf /app/app-minimal && \ # rm -rf /app/app-minimal && \
npm run build && \
ls -la /app && \ ls -la /app && \
du -hd1 /app du -hd1 /app
@@ -168,6 +169,18 @@ RUN \
COPY --from=docker-minifier /app /app 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 EXPOSE 1200
ENTRYPOINT ["dumb-init", "--"] ENTRYPOINT ["dumb-init", "--"]

View File

@@ -13,6 +13,17 @@ import { route as testRoute } from '@/routes-new/test/index';
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const __dirname = path.dirname(fileURLToPath(import.meta.url));
let modules: Record<string, { route: Route } | { namespace: Namespace }> = {}; let modules: Record<string, { route: Route } | { namespace: Namespace }> = {};
let namespaces: Record<
string,
Namespace & {
routes: Record<
string,
Route & {
location: string;
}
>;
}
> = {};
switch (process.env.NODE_ENV) { switch (process.env.NODE_ENV) {
case 'test': case 'test':
@@ -25,6 +36,10 @@ switch (process.env.NODE_ENV) {
}, },
}; };
break; break;
case 'production':
// eslint-disable-next-line n/no-unpublished-require
namespaces = require('../assets/build/routes.json');
break;
default: default:
modules = directoryImport({ modules = directoryImport({
targetDirectoryPath: path.join(__dirname, './routes-new'), targetDirectoryPath: path.join(__dirname, './routes-new'),
@@ -32,13 +47,7 @@ switch (process.env.NODE_ENV) {
}) as typeof modules; }) as typeof modules;
} }
const namespaces: Record< if (Object.keys(modules).length) {
string,
Namespace & {
routes: Record<string, Route>;
}
> = {};
for (const module in modules) { for (const module in modules) {
const content = modules[module] as const content = modules[module] as
| { | {
@@ -65,10 +74,17 @@ for (const module in modules) {
} }
if (Array.isArray(content.route.path)) { if (Array.isArray(content.route.path)) {
for (const path of content.route.path) { for (const path of content.route.path) {
namespaces[namespace].routes[path] = content.route; namespaces[namespace].routes[path] = {
...content.route,
location: module.split('/').slice(2).join('/'),
};
} }
} else { } else {
namespaces[namespace].routes[content.route.path] = content.route; namespaces[namespace].routes[content.route.path] = {
...content.route,
location: module.split('/').slice(2).join('/'),
};
}
} }
} }
} }
@@ -81,6 +97,10 @@ export default function (app: Hono) {
for (const path in namespaces[namespace].routes) { for (const path in namespaces[namespace].routes) {
const wrapedHandler: Handler = async (ctx) => { const wrapedHandler: Handler = async (ctx) => {
if (!ctx.get('data')) { 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)); ctx.set('data', await namespaces[namespace].routes[path].handler(ctx));
} }
}; };

View File

@@ -31,7 +31,7 @@
"lint": "eslint --cache .", "lint": "eslint --cache .",
"prepare": "husky || true", "prepare": "husky || true",
"profiling": "NODE_ENV=production tsx --prof lib/index.ts", "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" "test": "npm run format:check && npm run vitest:coverage"
}, },
"lint-staged": { "lint-staged": {

View File

@@ -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/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/maintainers.json'), JSON.stringify(maintainers, null, 2));
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.json'), JSON.stringify(namespaces, null, 2));