diff --git a/src/config-default.ts b/src/config-default.ts index b0f0d3d..904b93f 100644 --- a/src/config-default.ts +++ b/src/config-default.ts @@ -26,7 +26,7 @@ const default_options: Options = { }, cdn: { process: 'off', - src_include: /^.*$/, + src_include: null, src_exclude: null, }, compress: true, diff --git a/src/config-types.ts b/src/config-types.ts index 938aa45..1204f34 100644 --- a/src/config-types.ts +++ b/src/config-types.ts @@ -43,7 +43,7 @@ export type Options = { process: | 'off' //default | 'optimize'; - src_include: RegExp; + src_include: RegExp | null; src_exclude: RegExp | null; transformer?: UrlTransformer; // Custom 'unpic' cdn url transformer, if not present it will be determined by 'unpic' based on original url }; diff --git a/src/optimize.ts b/src/optimize.ts index d90820e..0e41d84 100755 --- a/src/optimize.ts +++ b/src/optimize.ts @@ -329,59 +329,65 @@ async function processImage( /* * Check for external images */ - if (!isLocal(attrib_src)) { - switch (config.image.cdn.process) { - case 'off': - break; - case 'optimize': - let cdnTransformer = config.image.cdn.transformer; - if (!cdnTransformer) { - const canonical = getCanonicalCdnForUrl(attrib_src); - if (!canonical) break; - cdnTransformer = getTransformer(canonical.cdn); - } - if (!cdnTransformer) break; - if ( - !isIncluded( - attrib_src, - config.image.cdn.src_include, - config.image.cdn.src_exclude - ) - ) - break; - let attrib_width = getIntAttr(img, 'width'); - if (!attrib_width) { - $state.reportIssue(htmlfile, { - type: 'warn', - msg: `Missing or malformed'width' attribute for image ${attrib_src}, unable to perform CDN transform`, - }); - return; - } - let attrib_height = getIntAttr(img, 'height'); - if (!attrib_height) { - $state.reportIssue(htmlfile, { - type: 'warn', - msg: `Missing or malformed 'height' attribute for image ${attrib_src}, unable to perform CDN transform`, - }); - return; - } - const new_srcset = await generateSrcSetForCdn( - attrib_src, - cdnTransformer, - attrib_width, - attrib_height + switch (config.image.cdn.process) { + case 'off': + break; + case 'optimize': + let cdnTransformer = config.image.cdn.transformer; + if (cdnTransformer && !config.image.cdn.src_include) { + throw new Error( + 'config.image.cdn.src_include is required when specifying a config.image.cdn.transformer' ); + } - if (new_srcset !== null) { - img.attr('srcset', new_srcset); - } - - // Add sizes attribute if not specified - if (img.attr('srcset') && !img.attr('sizes')) { - img.attr('sizes', '100vw'); - } + if (!cdnTransformer) { + const canonical = getCanonicalCdnForUrl(attrib_src); + if (!canonical) break; + cdnTransformer = getTransformer(canonical.cdn); + } + if (!cdnTransformer) break; + if ( + !isIncluded( + attrib_src, + config.image.cdn.src_include || /.*/, + config.image.cdn.src_exclude + ) + ) + break; + let attrib_width = getIntAttr(img, 'width'); + if (!attrib_width) { + $state.reportIssue(htmlfile, { + type: 'warn', + msg: `Missing or malformed'width' attribute for image ${attrib_src}, unable to perform CDN transform`, + }); return; - } + } + let attrib_height = getIntAttr(img, 'height'); + if (!attrib_height) { + $state.reportIssue(htmlfile, { + type: 'warn', + msg: `Missing or malformed 'height' attribute for image ${attrib_src}, unable to perform CDN transform`, + }); + return; + } + const new_srcset = await generateSrcSetForCdn( + attrib_src, + cdnTransformer, + attrib_width, + attrib_height + ); + + if (new_srcset !== null) { + img.attr('srcset', new_srcset); + } + + // Add sizes attribute if not specified + if (img.attr('srcset') && !img.attr('sizes')) { + img.attr('sizes', '100vw'); + } + return; + } + if (!isLocal(attrib_src)) { switch (config.image.external.process) { case 'off': // Don't process external images return; diff --git a/src/utils/resource.ts b/src/utils/resource.ts index bae2840..281139e 100644 --- a/src/utils/resource.ts +++ b/src/utils/resource.ts @@ -155,7 +155,6 @@ export class Resource { } export function isLocal(src: string) { - if (src.startsWith('/cdn_cgi/')) return false; const u = url.parse(src); return !u.host; }