mirror of
https://github.com/gkd-kit/docs.git
synced 2024-11-16 11:52:20 +08:00
perf: use acorn type
This commit is contained in:
parent
207b4811db
commit
86bf93c973
1
.npmrc
1
.npmrc
|
@ -6,3 +6,4 @@ public-hoist-pattern[]=vue
|
||||||
public-hoist-pattern[]=vite
|
public-hoist-pattern[]=vite
|
||||||
public-hoist-pattern[]=rollup
|
public-hoist-pattern[]=rollup
|
||||||
public-hoist-pattern[]=@vueuse/core
|
public-hoist-pattern[]=@vueuse/core
|
||||||
|
public-hoist-pattern[]=acorn
|
||||||
|
|
|
@ -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 jsdom from 'jsdom';
|
||||||
import MagicString from 'magic-string';
|
import MagicString from 'magic-string';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
|
@ -40,28 +41,30 @@ export const mirror = (): Plugin | undefined => {
|
||||||
chunk.code.match(includesDynamicImport)
|
chunk.code.match(includesDynamicImport)
|
||||||
) {
|
) {
|
||||||
const ast = this.parse(chunk.code);
|
const ast = this.parse(chunk.code);
|
||||||
const nodes: any[] = [];
|
const nodes: ImportExpression[] = [];
|
||||||
walk.simple(ast, {
|
simple(ast, {
|
||||||
ImportExpression(node) {
|
ImportExpression(node) {
|
||||||
nodes.push(node.source);
|
nodes.push(node);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (nodes.length == 0) {
|
if (nodes.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const ms = new MagicString(chunk.code);
|
const ms = new MagicString(chunk.code);
|
||||||
nodes.forEach((node) => {
|
nodes
|
||||||
const start = node.start;
|
.map((v) => v.source)
|
||||||
const end = node.end;
|
.forEach((node) => {
|
||||||
const code = chunk.code.slice(start, end);
|
const start = node.start;
|
||||||
ms.overwrite(
|
const end = node.end;
|
||||||
start,
|
const code = chunk.code.slice(start, end);
|
||||||
end,
|
ms.overwrite(
|
||||||
`((u)=>{if(u.startsWith('/')){return${JSON.stringify(
|
start,
|
||||||
mirrorBaseUrl,
|
end,
|
||||||
)}+u}return u})(${code})`,
|
`((u)=>{if(u.startsWith('/')){return${JSON.stringify(
|
||||||
);
|
mirrorBaseUrl,
|
||||||
});
|
)}+u}return u})(${code})`,
|
||||||
|
);
|
||||||
|
});
|
||||||
chunk.code = ms.toString();
|
chunk.code = ms.toString();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user