Skip to content

Commit 662165a

Browse files
committed
fix: importNodes
1 parent 3b36c43 commit 662165a

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

docs/.vitepress/plugins/mirror.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const useMirror = process.env.MIRROR == `ON`;
1515

1616
const mirrorBaseUrl = `https://registry.npmmirror.com/@gkd-kit/docs/${selfPkg.version}/files/.vitepress/dist`;
1717

18+
const includesDynamicImport = /\bimport\s*\(/;
19+
1820
export const mirror = (): Plugin | undefined => {
1921
if (!useMirror) return;
2022
return {
@@ -36,7 +38,8 @@ export const mirror = (): Plugin | undefined => {
3638
if (
3739
chunk.type == 'chunk' &&
3840
chunk.fileName.endsWith(`.js`) &&
39-
(chunk.code.includes('/assets/') || chunk.code.match(/\bimport\s\(/))
41+
(chunk.code.includes('/assets/') ||
42+
chunk.code.match(includesDynamicImport))
4043
) {
4144
const ast = this.parse(chunk.code);
4245
const importNodes: ImportExpression[] = [];
@@ -54,19 +57,24 @@ export const mirror = (): Plugin | undefined => {
5457
}
5558
},
5659
});
60+
if (importNodes.length == 0) {
61+
return;
62+
}
5763
const ms = new MagicString(chunk.code);
58-
importNodes.forEach((node) => {
59-
const start = node.source.start;
60-
const end = node.source.end;
61-
const code = chunk.code.slice(start, end);
62-
ms.overwrite(
63-
start,
64-
end,
65-
`((u)=>{if(u.startsWith('/')){return${JSON.stringify(
66-
mirrorBaseUrl,
67-
)}+u}return u})(${code})`,
68-
);
69-
});
64+
importNodes
65+
.map((v) => v.source)
66+
.forEach((node) => {
67+
const start = node.start;
68+
const end = node.end;
69+
const code = chunk.code.slice(start, end);
70+
ms.overwrite(
71+
start,
72+
end,
73+
`((u)=>{if(u.startsWith('/')){return${JSON.stringify(
74+
mirrorBaseUrl,
75+
)}+u}return u})(${code})`,
76+
);
77+
});
7078
// literalNodes.forEach((n) => {
7179
// ms.overwrite(
7280
// n.start,

0 commit comments

Comments
 (0)