perf: use acorn type

This commit is contained in:
lisonge 2024-07-15 10:21:39 +08:00
parent 207b4811db
commit 86bf93c973
2 changed files with 20 additions and 16 deletions

1
.npmrc
View File

@ -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

View File

@ -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,17 +41,19 @@ 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) => {
nodes
.map((v) => v.source)
.forEach((node) => {
const start = node.start;
const end = node.end;
const code = chunk.code.slice(start, end);