Skip to content

Commit

Permalink
perf: use acorn type
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 15, 2024
1 parent 207b481 commit 86bf93c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 19 additions & 16 deletions docs/.vitepress/plugins/mirror.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
}
});
Expand Down

0 comments on commit 86bf93c

Please sign in to comment.