Skip to content

Commit ddbbc83

Browse files
committed
Handle custom txt file to encode correctly
1 parent b44627e commit ddbbc83

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

vite.config.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,28 @@ export default defineConfig({
1616
{
1717
name: 'py-raw-loader',
1818
transform(code, id) {
19-
if (id.endsWith('.py') || id.endsWith('.txt')) {
19+
if (id.endsWith('.py')) {
2020
return { code: `export default ${JSON.stringify(code)};`, map: null };
2121
}
22+
23+
if (id.endsWith('.txt')) {
24+
if (code.startsWith('export default "data:text/plain;base64,')) {
25+
const match = code.match(/data:text\/plain;base64,(.+?)"$/);
26+
if (match) {
27+
const base64Content = match[1];
28+
const decodedContent = Buffer.from(
29+
base64Content,
30+
'base64'
31+
).toString('utf-8');
32+
return {
33+
code: `export default ${JSON.stringify(decodedContent)};`,
34+
map: null,
35+
};
36+
}
37+
}
38+
return { code: `export default ${JSON.stringify(code)};`, map: null };
39+
}
40+
2241
return null;
2342
},
2443
},

0 commit comments

Comments
 (0)