mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-03 18:48:12 +08:00
feat: lazy load for production
This commit is contained in:
@@ -8,7 +8,6 @@ Dockerfile*
|
||||
LICENSE
|
||||
Procfile
|
||||
app-minimal
|
||||
assets
|
||||
coverage
|
||||
node_modules
|
||||
test
|
||||
|
||||
23
Dockerfile
23
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", "--"]
|
||||
|
||||
|
||||
@@ -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,43 +47,44 @@ switch (process.env.NODE_ENV) {
|
||||
}) as typeof modules;
|
||||
}
|
||||
|
||||
const namespaces: Record<
|
||||
string,
|
||||
Namespace & {
|
||||
routes: Record<string, Route>;
|
||||
}
|
||||
> = {};
|
||||
|
||||
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));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user