From 86bf93c9730f536f2e885f38bd92a6dc2cd807c4 Mon Sep 17 00:00:00 2001 From: lisonge Date: Mon, 15 Jul 2024 10:21:39 +0800 Subject: [PATCH] perf: use acorn type --- .npmrc | 1 + docs/.vitepress/plugins/mirror.ts | 35 +++++++++++++++++-------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/.npmrc b/.npmrc index 71653cc..388fe5d 100644 --- a/.npmrc +++ b/.npmrc @@ -6,3 +6,4 @@ public-hoist-pattern[]=vue public-hoist-pattern[]=vite public-hoist-pattern[]=rollup public-hoist-pattern[]=@vueuse/core +public-hoist-pattern[]=acorn diff --git a/docs/.vitepress/plugins/mirror.ts b/docs/.vitepress/plugins/mirror.ts index d5d80e7..1358402 100644 --- a/docs/.vitepress/plugins/mirror.ts +++ b/docs/.vitepress/plugins/mirror.ts @@ -1,4 +1,5 @@ -import * as walk from 'acorn-walk'; +import { simple } from 'acorn-walk'; +import type { ImportExpression } from 'acorn'; import jsdom from 'jsdom'; import MagicString from 'magic-string'; import fs from 'node:fs/promises'; @@ -40,28 +41,30 @@ export const mirror = (): Plugin | undefined => { chunk.code.match(includesDynamicImport) ) { const ast = this.parse(chunk.code); - const nodes: any[] = []; - walk.simple(ast, { + const nodes: ImportExpression[] = []; + simple(ast, { ImportExpression(node) { - nodes.push(node.source); + nodes.push(node); }, }); if (nodes.length == 0) { return; } const ms = new MagicString(chunk.code); - nodes.forEach((node) => { - const start = node.start; - const end = node.end; - const code = chunk.code.slice(start, end); - ms.overwrite( - start, - end, - `((u)=>{if(u.startsWith('/')){return${JSON.stringify( - mirrorBaseUrl, - )}+u}return u})(${code})`, - ); - }); + nodes + .map((v) => v.source) + .forEach((node) => { + const start = node.start; + const end = node.end; + const code = chunk.code.slice(start, end); + ms.overwrite( + start, + end, + `((u)=>{if(u.startsWith('/')){return${JSON.stringify( + mirrorBaseUrl, + )}+u}return u})(${code})`, + ); + }); chunk.code = ms.toString(); } });