Skip to content

Commit adafff7

Browse files
refactor: delegate asset deduplication to this.emitFile
BREAKING CHANGE: imports to deduplicated assets are not merged into one
1 parent a37cd8d commit adafff7

File tree

4 files changed

+5
-54
lines changed

4 files changed

+5
-54
lines changed

src/helpers.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import fs from "fs";
21
import path from "path";
3-
import crypto from "crypto";
42
import { OutputOptions } from "rollup";
53

64
export function getOutputId(filename: string, outputOptions: OutputOptions) {
@@ -26,28 +24,3 @@ export function getRelativeImportPath(from: string, to: string) {
2624

2725
return import_path;
2826
}
29-
30-
export function getContentHash(filepath: string) {
31-
const md5sum = crypto.createHash("md5");
32-
33-
return new Promise<string>((resolve, reject) =>
34-
fs.createReadStream(filepath)
35-
.on("data", chunk => md5sum.update(chunk))
36-
.on("end", () => resolve(md5sum.digest("hex")))
37-
.on("error", err => reject(err))
38-
);
39-
}
40-
41-
export function getIdDeduplicator() {
42-
const hashToIdMap: Record<string, string | undefined> = {};
43-
44-
return async (id: string) => {
45-
const hash = await getContentHash(id);
46-
let result = hashToIdMap[hash];
47-
48-
if (result) return result;
49-
50-
hashToIdMap[hash] = id;
51-
return id;
52-
}
53-
}

src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "path";
33
import { Plugin } from "rollup";
44
import { createFilter, FilterPattern } from "@rollup/pluginutils";
55
import { parse, print, types, visit } from "recast";
6-
import { getIdDeduplicator, getOutputId, getRelativeImportPath } from "./helpers";
6+
import { getOutputId, getRelativeImportPath } from "./helpers";
77

88
const PLUGIN_NAME = "external-assets";
99
const PREFIX = `\0${PLUGIN_NAME}:`;
@@ -36,7 +36,6 @@ export default function externalAssets(
3636
): Plugin {
3737
const idFilter = createFilter(include, exclude, options);
3838
const assets = new Map<string, Buffer>();
39-
const deduplicateId = getIdDeduplicator();
4039

4140
return {
4241
name: PLUGIN_NAME,
@@ -57,10 +56,6 @@ export default function externalAssets(
5756
async load(id) {
5857
if (!idFilter(id)) return null;
5958

60-
// For two or more assets with the same content, only one asset is going to be emitted.
61-
// `this.emitFile` deduplicates in the same way.
62-
id = await deduplicateId(id);
63-
6459
assets.set(id, await fs.readFile(id));
6560

6661
// Load a proxy module that rollup will discard in favor of inligning the imports.

tests/__snapshots__/output.test.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`deduplicate assets with different names: index6 1`] = `
4-
"import png from \\"./assets/image2-479dcdf1.png\\";
4+
"import png from \\"./assets/image-0fc60877.png\\";
55
import text from \\"./assets/text-6d7076f2.txt\\";
66
import \\"./assets/styles-fc0ceb37.css\\";
7+
import png2 from \\"./assets/image-0fc60877.png\\";
78
89
// This import should not be removed unless specified by the user or some other plugin.
910
@@ -17,7 +18,7 @@ var main = () => {
1718
sayHi();
1819
};
1920
20-
console.log(png);
21+
console.log(png2);
2122
main();
2223
"
2324
`;

tests/unit.test.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
import path from "path";
2-
import {
3-
getOutputId,
4-
getIdDeduplicator,
5-
} from "../src/helpers";
6-
7-
test("getIdDeduplicator", async () => {
8-
const deduplicateId = getIdDeduplicator();
9-
10-
const image = path.resolve("tests/fixtures/assets/image.png");
11-
const image2 = path.resolve("tests/fixtures/assets/image2.png");
12-
const text = path.resolve("tests/fixtures/assets/text.txt");
13-
14-
expect(async () => await deduplicateId("/non/existent/path")).rejects.toThrow();
15-
expect(await deduplicateId(image)).toBe(image);
16-
expect(await deduplicateId(text)).toBe(text);
17-
expect(await deduplicateId(image2)).toBe(image);
18-
expect(await deduplicateId(image)).toBe(image);
19-
expect(await deduplicateId(image2)).toBe(image);
20-
});
2+
import { getOutputId } from "../src/helpers";
213

224
test("getOutputId", () => {
235
expect(getOutputId("a.ext", { dir: "out" })).toBe(path.resolve("out/a.ext"));

0 commit comments

Comments
 (0)