Skip to content

Commit a15a44a

Browse files
authored
Merge pull request #29 from liamfiddler/sharp-relative-files
Sharp relative files
2 parents 6774dd2 + f7d60ab commit a15a44a

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

.eleventy.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const getImageData = async (imageSrc) => {
111111
};
112112

113113
// Adds the attributes to the image element
114-
const processImage = async (imgElem, outputPath) => {
114+
const processImage = async (imgElem, options) => {
115115
const {
116116
transformImgPath,
117117
className,
@@ -123,7 +123,7 @@ const processImage = async (imgElem, outputPath) => {
123123
return;
124124
}
125125

126-
const imgPath = transformImgPath(imgElem.src, outputPath);
126+
const imgPath = transformImgPath(imgElem.src, options);
127127
const parsedUrl = url.parse(imgPath);
128128
let fileExt = path.extname(parsedUrl.pathname).substr(1);
129129

@@ -175,7 +175,7 @@ const processImage = async (imgElem, outputPath) => {
175175
imgElem.setAttribute('width', Math.round(ratioWidth));
176176
}
177177
} catch (e) {
178-
console.error('LazyImages', imgPath, e);
178+
logMessage(`${e.message}: ${imgPath}`);
179179
}
180180
};
181181

@@ -195,7 +195,7 @@ const transformMarkup = async (rawContent, outputPath) => {
195195

196196
if (images.length > 0) {
197197
logMessage(`found ${images.length} images in ${outputPath}`);
198-
await Promise.all(images.map((image) => processImage(image, outputPath)));
198+
await Promise.all(images.map((image) => processImage(image, { outputPath })));
199199
logMessage(`processed ${images.length} images in ${outputPath}`);
200200

201201
if (appendInitScript) {
@@ -222,11 +222,10 @@ const transformMarkup = async (rawContent, outputPath) => {
222222
module.exports = {
223223
initArguments: {},
224224
configFunction: (eleventyConfig, pluginOptions = {}) => {
225-
lazyImagesConfig = Object.assign(
226-
{},
227-
defaultLazyImagesConfig,
228-
pluginOptions
229-
);
225+
lazyImagesConfig = {
226+
...defaultLazyImagesConfig,
227+
...pluginOptions,
228+
};
230229

231230
checkConfig(lazyImagesConfig, defaultLazyImagesConfig);
232231
cache.load(lazyImagesConfig.cacheFile);

helpers.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Ensures relative paths start in the project root
2-
exports.transformImgPath = (src) => {
2+
exports.transformImgPath = (src, options = {}) => {
3+
if (
4+
src.startsWith('./') ||
5+
src.startsWith('../') ||
6+
(!src.startsWith('/') &&
7+
!src.startsWith('http://') &&
8+
!src.startsWith('https://') &&
9+
!src.startsWith('data:'))
10+
) {
11+
// The file path is relative to the output document
12+
const { outputPath, inputPath } = options;
13+
const outputDir = outputPath.substring(0, outputPath.lastIndexOf('/') + 1);
14+
const imgPath = outputDir + src;
15+
// TODO: Finish this when https://github.com/11ty/eleventy/issues/789 is resolved
16+
}
17+
18+
// Reference files from the root project directory
319
if (src.startsWith('/') && !src.startsWith('//')) {
420
return `.${src}`;
521
}

0 commit comments

Comments
 (0)