@all-the.rest/astro-image-cdn 0.2.2 → 0.2.3
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/package.json +1 -1
- package/src/service.js +13 -9
package/package.json
CHANGED
package/src/service.js
CHANGED
|
@@ -176,6 +176,10 @@ function sourceCandidates(value, rootDir) {
|
|
|
176
176
|
return [...candidates];
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
function sourcePathWithoutQuery(value) {
|
|
180
|
+
return typeof value === "string" ? value.split(/[?#]/, 1)[0] : undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
179
183
|
function isInside(parent, candidate) {
|
|
180
184
|
const relative = path.relative(parent, candidate);
|
|
181
185
|
return relative === "" || (!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative));
|
|
@@ -210,11 +214,10 @@ async function findFilesByName(directory, fileName, matches) {
|
|
|
210
214
|
async function requestedSourcePath(source, config) {
|
|
211
215
|
if (typeof source?.fsPath === "string") return path.resolve(source.fsPath);
|
|
212
216
|
|
|
213
|
-
const sourceName =
|
|
217
|
+
const sourceName = sourcePathWithoutQuery(source?.src);
|
|
214
218
|
if (!sourceName) return undefined;
|
|
215
219
|
const rootDir = path.resolve(config.rootDir);
|
|
216
|
-
const
|
|
217
|
-
for (const candidate of sourceCandidates(sourceNameWithoutQuery, rootDir)) {
|
|
220
|
+
for (const candidate of sourceCandidates(sourceName, rootDir)) {
|
|
218
221
|
const absoluteCandidate = path.resolve(rootDir, candidate);
|
|
219
222
|
if (!isConfiguredSourcePath(absoluteCandidate, config)) continue;
|
|
220
223
|
try {
|
|
@@ -226,7 +229,7 @@ async function requestedSourcePath(source, config) {
|
|
|
226
229
|
|
|
227
230
|
// Astro's SVG component wrapper serializes image metadata without its private fsPath.
|
|
228
231
|
// Resolve that requested import by its emitted basename, but reject ambiguous matches.
|
|
229
|
-
const basename = path.posix.basename(
|
|
232
|
+
const basename = path.posix.basename(sourceName)
|
|
230
233
|
.replace(/\.[a-z0-9_-]{8,}(?=\.[^.]+$)/i, "");
|
|
231
234
|
const matches = [];
|
|
232
235
|
for (const directory of (config.sources ?? ["src"]).map((sourceDirectory) => path.resolve(rootDir, sourceDirectory))) {
|
|
@@ -271,12 +274,14 @@ function resolveImage(options, imageConfig) {
|
|
|
271
274
|
const manifest = readManifest(imageConfig);
|
|
272
275
|
const rootDir = path.resolve(imageConfig.service.config.rootDir);
|
|
273
276
|
const source = options.src;
|
|
277
|
+
const sourceName = typeof source === "string" ? source : source?.src;
|
|
278
|
+
const sourcePath = sourcePathWithoutQuery(sourceName);
|
|
274
279
|
const possibleSources = [];
|
|
275
280
|
if (isImportedImage(source)) {
|
|
276
281
|
if (typeof source.fsPath === "string") possibleSources.push(...sourceCandidates(source.fsPath, rootDir));
|
|
277
|
-
possibleSources.push(...sourceCandidates(
|
|
282
|
+
possibleSources.push(...sourceCandidates(sourcePath, rootDir));
|
|
278
283
|
} else {
|
|
279
|
-
possibleSources.push(...sourceCandidates(
|
|
284
|
+
possibleSources.push(...sourceCandidates(sourcePath, rootDir));
|
|
280
285
|
}
|
|
281
286
|
|
|
282
287
|
for (const candidate of possibleSources) {
|
|
@@ -284,9 +289,8 @@ function resolveImage(options, imageConfig) {
|
|
|
284
289
|
if (entry) return entry;
|
|
285
290
|
}
|
|
286
291
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const builtAssetName = path.basename(sourceName).replace(/\.[a-z0-9_-]{8,}(?=\.[^.]+$)/i, "");
|
|
292
|
+
if (typeof sourcePath === "string") {
|
|
293
|
+
const builtAssetName = path.basename(sourcePath).replace(/\.[a-z0-9_-]{8,}(?=\.[^.]+$)/i, "");
|
|
290
294
|
const sameName = Object.values(manifest.images).filter((entry) => path.posix.basename(entry.source) === builtAssetName);
|
|
291
295
|
const sameDimensions = sameName.filter((entry) => entry.width === source?.width && entry.height === source?.height);
|
|
292
296
|
const candidates = sameDimensions.length > 0 ? sameDimensions : sameName;
|