|
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' |
5 | 2 | import type { Plugin } from 'vite' |
6 | 3 |
|
7 | 4 | export const pluginScratchImport = (): Plugin => ({ |
@@ -30,25 +27,29 @@ export const pluginScratchImport = (): Plugin => ({ |
30 | 27 | if (!ext) { |
31 | 28 | throw new Error(`Unsupported scratch asset type: ${url.pathname}`) |
32 | 29 | } |
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 | | - } |
46 | 30 |
|
47 | 31 | 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)); |
51 | 40 |
|
| 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 | + |
52 | 53 | export default data |
53 | 54 | ` |
54 | 55 | } |
|
0 commit comments