perf: lazy require dependencies (#8025)

This commit is contained in:
Sukka
2021-08-20 14:05:57 +08:00
committed by GitHub
parent 72dd0193c1
commit 31720bbb1b
3 changed files with 16 additions and 5 deletions

View File

@@ -1,10 +1,11 @@
const entities = require('entities');
const mercury_parser = require('@postlight/mercury-parser');
const cheerio = require('cheerio');
const { simplecc } = require('simplecc-wasm');
const got = require('@/utils/got');
const config = require('@/config').value;
let mercury_parser;
module.exports = async (ctx, next) => {
await next();
@@ -204,6 +205,8 @@ module.exports = async (ctx, next) => {
const parsed_result = await ctx.cache.tryGet(`mercury-cache-${link}`, async () => {
// if parser failed, return default description and not report error
try {
mercury_parser = mercury_parser || require('@postlight/mercury-parser');
const res = await got(link);
const $ = cheerio.load(res.data);
const result = await mercury_parser.parse(link, {