@lonik/oh-image 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugin.js +10 -9
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { basename, dirname, extname, join, parse } from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
3
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
4
|
import queryString from "query-string";
|
|
5
5
|
import sharp from "sharp";
|
|
6
6
|
import pLimit from "p-limit";
|
|
7
7
|
|
|
8
8
|
//#region src/plugin/utils.ts
|
|
9
|
-
function
|
|
10
|
-
|
|
9
|
+
async function getFileHash(filePath) {
|
|
10
|
+
const content = await readFile(filePath);
|
|
11
|
+
return createHash("sha256").update(content).digest("hex").slice(0, 16);
|
|
11
12
|
}
|
|
12
13
|
async function readFileSafe(path) {
|
|
13
14
|
try {
|
|
@@ -102,9 +103,8 @@ function ohImage(options) {
|
|
|
102
103
|
const fileId = basename(url);
|
|
103
104
|
return join(cacheDir, fileId);
|
|
104
105
|
}
|
|
105
|
-
function genIdentifier(uri, format, prefix) {
|
|
106
|
-
const
|
|
107
|
-
const uniqueFileId = `${prefix}-${getRandomString()}-${fileId}.${format}`;
|
|
106
|
+
function genIdentifier(uri, format, prefix, hash) {
|
|
107
|
+
const uniqueFileId = `${prefix}-${hash}-${basename(uri)}.${format}`;
|
|
108
108
|
if (!isBuild) return join(DEV_DIR, uniqueFileId);
|
|
109
109
|
return join(assetsDir, config.distDir, uniqueFileId);
|
|
110
110
|
}
|
|
@@ -150,11 +150,12 @@ function ohImage(options) {
|
|
|
150
150
|
const origin = parsed.path;
|
|
151
151
|
const { name, ext } = parse(parsed.path);
|
|
152
152
|
const metadata = await sharp(parsed.path).metadata();
|
|
153
|
+
const hash = await getFileHash(origin);
|
|
153
154
|
const mergedOptions = {
|
|
154
155
|
...config,
|
|
155
156
|
...parsed.options
|
|
156
157
|
};
|
|
157
|
-
const mainIdentifier = genIdentifier(name, mergedOptions.format ?? ext.slice(1), "main");
|
|
158
|
+
const mainIdentifier = genIdentifier(name, mergedOptions.format ?? ext.slice(1), "main", hash);
|
|
158
159
|
const mainEntry = {
|
|
159
160
|
width: mergedOptions.width,
|
|
160
161
|
height: mergedOptions.height,
|
|
@@ -178,7 +179,7 @@ function ohImage(options) {
|
|
|
178
179
|
placeholderWidth = Math.max(Math.round(metadata.width / metadata.height * PLACEHOLDER_IMG_SIZE), 1);
|
|
179
180
|
placeholderHeight = PLACEHOLDER_IMG_SIZE;
|
|
180
181
|
}
|
|
181
|
-
const placeholderIdentifier = genIdentifier(name, DEFAULT_IMAGE_FORMAT, "placeholder");
|
|
182
|
+
const placeholderIdentifier = genIdentifier(name, DEFAULT_IMAGE_FORMAT, "placeholder", hash);
|
|
182
183
|
const placeholderEntry = {
|
|
183
184
|
width: placeholderWidth,
|
|
184
185
|
height: placeholderHeight,
|
|
@@ -192,7 +193,7 @@ function ohImage(options) {
|
|
|
192
193
|
if (mergedOptions.bps) {
|
|
193
194
|
const srcSets = [];
|
|
194
195
|
for (const breakpoint of mergedOptions.bps) {
|
|
195
|
-
const srcSetIdentifier = genIdentifier(name, DEFAULT_IMAGE_FORMAT, `breakpoint-${breakpoint}
|
|
196
|
+
const srcSetIdentifier = genIdentifier(name, DEFAULT_IMAGE_FORMAT, `breakpoint-${breakpoint}`, hash);
|
|
196
197
|
const srcSetEntry = {
|
|
197
198
|
width: breakpoint,
|
|
198
199
|
format: DEFAULT_IMAGE_FORMAT,
|
package/package.json
CHANGED