Skip to content

Commit

Permalink
fix: inconsistant hash in the load hook
Browse files Browse the repository at this point in the history
  • Loading branch information
recursive-beast committed Mar 3, 2021
1 parent e6350d5 commit da51590
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ function getRelativeImportPath(from: string, to: string) {
return import_path;
}

function getIdHash(id: string) {
const md5sum = crypto.createHash("md5");

return new Promise<string>((resolve, reject) =>
fs.createReadStream(id)
.on("data", chunk => md5sum.update(chunk))
.on("end", () => resolve(md5sum.digest("hex")))
.on("error", err => reject(err))
);
}

/**
* Make assets external but include them in the output.
* @param pattern - A picomatch pattern, or array of patterns,
Expand Down Expand Up @@ -66,7 +77,7 @@ export default function externalAssets(pattern: FilterPattern): Plugin {
|| !idFilter(id) // Filtered out id.
) return null;

const hash = crypto.createHash('md5').update(id).digest('hex');
const hash = await getIdHash(id);

// In the output phase,
// We'll use this mapping to replace the hash with a relative path from a chunk to the emitted asset.
Expand Down
8 changes: 4 additions & 4 deletions tests/snapshots/output.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -10895,7 +10895,7 @@ Generated by [AVA](https://avajs.dev).

[
{
code: `define(["./assets/image-0fc60877.png", "./assets/text-6d7076f2.txt", "./assets/styles-fc0ceb37.css"], function (png, text, _externalAssets_4310739eb4843f3840c8bd0630aa60b0) { 'use strict';␊
code: `define(["./assets/image-0fc60877.png", "./assets/text-6d7076f2.txt", "./assets/styles-fc0ceb37.css"], function (png, text, _externalAssets_60d2da2b35495c39d05946f38d2a843e) { 'use strict';␊
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }␊
Expand Down Expand Up @@ -59976,7 +59976,7 @@ Generated by [AVA](https://avajs.dev).
{
code: `import png from "./assets/image-0fc60877.png";␊
import text from "./assets/text-6d7076f2.txt";␊
import { s as sayHi } from './chunks/sub/file2_4ac99744.js';␊
import { s as sayHi } from './chunks/sub/file2_2ded6d0b.js';␊
import "./assets/styles-fc0ceb37.css";␊
var index1 = () => {␊
Expand All @@ -60002,7 +60002,7 @@ Generated by [AVA](https://avajs.dev).
{
code: `import png from "./assets/image-0fc60877.png";␊
import text from "./assets/text-6d7076f2.txt";␊
import { s as sayHi } from './chunks/sub/file2_4ac99744.js';␊
import { s as sayHi } from './chunks/sub/file2_2ded6d0b.js';␊
import "./assets/styles-fc0ceb37.css";␊
var index4 = () => {␊
Expand Down Expand Up @@ -60039,7 +60039,7 @@ Generated by [AVA](https://avajs.dev).
exports: [
's',
],
fileName: 'chunks/sub/file2_4ac99744.js',
fileName: 'chunks/sub/file2_2ded6d0b.js',
implicitlyLoadedBefore: [],
isDynamicEntry: false,
isEntry: false,
Expand Down
Binary file modified tests/snapshots/output.test.js.snap
Binary file not shown.

0 comments on commit da51590

Please sign in to comment.