Skip to content

Commit da51590

Browse files
fix: inconsistant hash in the load hook
1 parent e6350d5 commit da51590

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ function getRelativeImportPath(from: string, to: string) {
3232
return import_path;
3333
}
3434

35+
function getIdHash(id: string) {
36+
const md5sum = crypto.createHash("md5");
37+
38+
return new Promise<string>((resolve, reject) =>
39+
fs.createReadStream(id)
40+
.on("data", chunk => md5sum.update(chunk))
41+
.on("end", () => resolve(md5sum.digest("hex")))
42+
.on("error", err => reject(err))
43+
);
44+
}
45+
3546
/**
3647
* Make assets external but include them in the output.
3748
* @param pattern - A picomatch pattern, or array of patterns,
@@ -66,7 +77,7 @@ export default function externalAssets(pattern: FilterPattern): Plugin {
6677
|| !idFilter(id) // Filtered out id.
6778
) return null;
6879

69-
const hash = crypto.createHash('md5').update(id).digest('hex');
80+
const hash = await getIdHash(id);
7081

7182
// In the output phase,
7283
// We'll use this mapping to replace the hash with a relative path from a chunk to the emitted asset.

tests/snapshots/output.test.js.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10895,7 +10895,7 @@ Generated by [AVA](https://avajs.dev).
1089510895

1089610896
[
1089710897
{
10898-
code: `define(["./assets/image-0fc60877.png", "./assets/text-6d7076f2.txt", "./assets/styles-fc0ceb37.css"], function (png, text, _externalAssets_4310739eb4843f3840c8bd0630aa60b0) { 'use strict';␊
10898+
code: `define(["./assets/image-0fc60877.png", "./assets/text-6d7076f2.txt", "./assets/styles-fc0ceb37.css"], function (png, text, _externalAssets_60d2da2b35495c39d05946f38d2a843e) { 'use strict';␊
1089910899
1090010900
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }␊
1090110901
@@ -59976,7 +59976,7 @@ Generated by [AVA](https://avajs.dev).
5997659976
{
5997759977
code: `import png from "./assets/image-0fc60877.png";␊
5997859978
import text from "./assets/text-6d7076f2.txt";␊
59979-
import { s as sayHi } from './chunks/sub/file2_4ac99744.js';␊
59979+
import { s as sayHi } from './chunks/sub/file2_2ded6d0b.js';␊
5998059980
import "./assets/styles-fc0ceb37.css";␊
5998159981
5998259982
var index1 = () => {␊
@@ -60002,7 +60002,7 @@ Generated by [AVA](https://avajs.dev).
6000260002
{
6000360003
code: `import png from "./assets/image-0fc60877.png";␊
6000460004
import text from "./assets/text-6d7076f2.txt";␊
60005-
import { s as sayHi } from './chunks/sub/file2_4ac99744.js';␊
60005+
import { s as sayHi } from './chunks/sub/file2_2ded6d0b.js';␊
6000660006
import "./assets/styles-fc0ceb37.css";␊
6000760007
6000860008
var index4 = () => {␊
@@ -60039,7 +60039,7 @@ Generated by [AVA](https://avajs.dev).
6003960039
exports: [
6004060040
's',
6004160041
],
60042-
fileName: 'chunks/sub/file2_4ac99744.js',
60042+
fileName: 'chunks/sub/file2_2ded6d0b.js',
6004360043
implicitlyLoadedBefore: [],
6004460044
isDynamicEntry: false,
6004560045
isEntry: false,
-1 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)