Skip to content

Commit f475876

Browse files
committed
modified: src/index.ts
1 parent 8939405 commit f475876

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/index.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ import { useStyleTag } from "@vueuse/core";
1717
import hash from "hash-sum";
1818
import { transform } from "sucrase";
1919

20-
const log = (msgs: (CompilerError | Error | string)[]) => {
21-
msgs.forEach((msg) => {
22-
console.log(msg);
23-
});
24-
};
20+
const fetchText = async (url: string) => {
21+
try {
22+
const response = await fetch(url);
23+
return response.ok ? response : new Response("");
24+
} catch {
25+
return new Response("");
26+
}
27+
},
28+
log = (msgs: (CompilerError | Error | string)[]) => {
29+
msgs.forEach((msg) => {
30+
console.log(msg);
31+
});
32+
};
2533
const addStyle = async (
2634
id: string,
2735
{ filename }: SFCDescriptor,
@@ -32,7 +40,7 @@ const addStyle = async (
3240
id,
3341
modules: !!module,
3442
scoped,
35-
source: src ? await (await fetch(src)).text() : content,
43+
source: src ? await (await fetchText(src)).text() : content,
3644
});
3745
log(errors);
3846
useStyleTag(code, scoped ? { id } : undefined);
@@ -42,9 +50,12 @@ const addStyle = async (
4250
`data:text/javascript;base64,${btoa(Array.from(new TextEncoder().encode(code), (byte) => String.fromCodePoint(byte)).join(""))}`
4351
),
4452
loadModule = async (filename: string) => {
45-
const { descriptor, errors } = parse(await (await fetch(filename)).text(), {
46-
filename,
47-
});
53+
const { descriptor, errors } = parse(
54+
await (await fetchText(filename)).text(),
55+
{
56+
filename,
57+
},
58+
);
4859
const compilerOptions: CompilerOptions = { expressionPlugins: [] },
4960
scriptBlocks = ["script", "scriptSetup"],
5061
contents = await Promise.all(
@@ -56,7 +67,7 @@ const addStyle = async (
5667
compilerOptions.expressionPlugins?.push("jsx");
5768
if (/tsx?$/.test(lang))
5869
compilerOptions.expressionPlugins?.push("typescript");
59-
return src && (await (await fetch(src)).text());
70+
return src && (await (await fetchText(src)).text());
6071
}),
6172
),
6273
id = `data-v-${hash(filename)}`,
@@ -103,7 +114,7 @@ const addStyle = async (
103114
scoped,
104115
slotted: descriptor.slotted,
105116
source: descriptor.template.src
106-
? await (await fetch(descriptor.template.src)).text()
117+
? await (await fetchText(descriptor.template.src)).text()
107118
: descriptor.template.content,
108119
// @ts-expect-error TODO remove expect-error after 3.6
109120
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment

0 commit comments

Comments
 (0)