|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { cpus } = require('os') |
4 | | -const { join, extname, basename } = require('path') |
5 | | -const { access } = require('fs').promises |
6 | | -const Image = require('@11ty/eleventy-img') |
| 3 | +import { cpus } from 'node:os' |
| 4 | +import { join, extname, basename } from 'node:path' |
| 5 | +import { access } from 'node:fs/promises' |
| 6 | +import * as url from 'node:url' |
| 7 | +import Image from '@11ty/eleventy-img' |
7 | 8 |
|
| 9 | +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) |
8 | 10 | Image.concurrency = (cpus()).length |
9 | 11 |
|
10 | | -module.exports = function responsiveImage (config) { |
| 12 | +export default function responsiveImage (config) { |
11 | 13 | // Add responsive image helper |
12 | 14 | config.addNunjucksAsyncShortcode('responsiveImage', async function (src, alt, options = {}) { |
13 | 15 | if (typeof alt === 'undefined') { |
@@ -45,8 +47,8 @@ module.exports = function responsiveImage (config) { |
45 | 47 | const html = `<span style="position: relative; display: block; margin-left: auto; margin-right: auto; max-width: ${maxWidth}px; "> |
46 | 48 | <picture> |
47 | 49 | ${Object.values(stats).map(imageFormat => { |
48 | | - return `<source type="image/${imageFormat[0].format}" srcset="${imageFormat.map(entry => `${entry.url} ${entry.width}w`).join(', ')}" sizes="${sizes}">` |
49 | | -}).join('\n')} |
| 50 | + return `<source type="image/${imageFormat[0].format}" srcset="${imageFormat.map(entry => `${entry.url} ${entry.width}w`).join(', ')}" sizes="${sizes}">` |
| 51 | + }).join('\n')} |
50 | 52 | <img loading="lazy" decoding="async" style="max-width: 100%; width: 100%; margin: 0px; vertical-align: middle;" ${options.class ? `class="${options.class}"` : ''} alt="${alt}" src="${lowestSrc.url}" width="${lowestSrc.width}" height="${lowestSrc.height}"> |
51 | 53 | </picture> |
52 | 54 | </span>` |
|
0 commit comments