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
Procfile
app-minimal
assets
coverage
node_modules
test

View File

@@ -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", "--"]

View File

@@ -13,6 +13,17 @@ import { route as testRoute } from '@/routes-new/test/index';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let modules: Record<string, { route: Route } | { namespace: Namespace }> = {};
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,13 +47,7 @@ switch (process.env.NODE_ENV) {
}) as typeof modules;
}
const namespaces: Record<
string,
Namespace & {
routes: Record<string, Route>;
}
> = {};
if (Object.keys(modules).length) {
for (const module in modules) {
const content = modules[module] as
| {
@@ -65,10 +74,17 @@ for (const module in modules) {
}
if (Array.isArray(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 {
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) {
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));
}
};

View File

@@ -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": {

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