Skip to content

Commit 2aa1e0c

Browse files
authored
fix(vite): Import should be handled per file instead of generating heavy JSON (#144)
* Update bun.lock * Fix heavy json load. * Fixing problems pointed by LLM * Fixing issues * Remove unnecessary encoding/decoding
1 parent 6a00ced commit 2aa1e0c

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/hikkaku/src/vite/plugin-scratch-import.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import crypto from 'node:crypto'
2-
import { readFile } from 'node:fs/promises'
3-
import * as path from 'node:path'
4-
import { fileURLToPath, pathToFileURL } from 'node:url'
1+
import { pathToFileURL } from 'node:url'
52
import type { Plugin } from 'vite'
63

74
export const pluginScratchImport = (): Plugin => ({
@@ -30,25 +27,29 @@ export const pluginScratchImport = (): Plugin => ({
3027
if (!ext) {
3128
throw new Error(`Unsupported scratch asset type: ${url.pathname}`)
3229
}
33-
const file = await readFile(fileURLToPath(url))
34-
35-
const hash = crypto.createHash('md5')
36-
hash.update(file)
37-
const md5 = hash.digest('hex')
38-
39-
const data = {
40-
name: path.basename(url.pathname),
41-
_data: Buffer.from(file).toString('base64'),
42-
assetId: md5,
43-
dataFormat: ext,
44-
md5ext: `${md5}.${ext}`,
45-
}
4630

4731
return `
48-
const data = ${JSON.stringify(data)}
49-
// to Uint8Array
50-
data._data = Uint8Array.from(atob(data._data), c => c.charCodeAt(0));
32+
import crypto from 'node:crypto';
33+
import { readFile } from 'node:fs/promises';
34+
import * as path from 'node:path';
35+
import { fileURLToPath } from 'node:url';
36+
37+
const pathUrl = new URL(${JSON.stringify(url.href)});
38+
const ext = ${JSON.stringify(ext)};
39+
const file = await readFile(fileURLToPath(pathUrl));
5140
41+
const hash = crypto.createHash('md5');
42+
hash.update(file);
43+
const md5 = hash.digest('hex');
44+
45+
const data = {
46+
name: path.basename(pathUrl.pathname),
47+
_data: Uint8Array.from(file),
48+
assetId: md5,
49+
dataFormat: ext,
50+
md5ext: md5 + "." + ext,
51+
}
52+
5253
export default data
5354
`
5455
}

0 commit comments

Comments
 (0)