UNPKG

rollup-plugin-imagetools

Version:
100 lines (97 loc) 4.66 kB
import { builtins, builtinOutputFormats, parseURL, extractEntries, resolveConfigs, loadImage, generateTransforms, applyTransforms, urlFormat } from 'imagetools-core'; export * from 'imagetools-core'; import { createFilter, dataToEsm } from '@rollup/pluginutils'; import MagicString from 'magic-string'; import { resolve, dirname, basename, extname } from 'path'; const defaultOptions = { include: ['**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}', '**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*'], exclude: '', removeMetadata: true }; function imagetools(userOptions = {}) { const pluginOptions = { ...defaultOptions, ...userOptions }; const filter = createFilter(pluginOptions.include, pluginOptions.exclude); const transformFactories = pluginOptions.extendTransforms ? pluginOptions.extendTransforms(builtins) : builtins; const outputFormats = pluginOptions.extendOutputFormats ? pluginOptions.extendOutputFormats(builtinOutputFormats) : builtinOutputFormats; return { name: 'imagetools', resolveId(source, importer = '') { const id = resolve(dirname(importer), source); if (!filter(id)) return null; return id; }, async load(id) { var _a, _b, _c; if (!filter(id)) return null; const srcURL = parseURL(id); let directives = srcURL.searchParams; if (typeof pluginOptions.defaultDirectives === 'function') { directives = new URLSearchParams([...pluginOptions.defaultDirectives(srcURL), ...srcURL.searchParams]); } else if (pluginOptions.defaultDirectives) { directives = new URLSearchParams([...pluginOptions.defaultDirectives, ...srcURL.searchParams]); } if (!directives.toString()) return null; const parameters = extractEntries(directives); const imageConfigs = (_b = (_a = pluginOptions.resolveConfigs) === null || _a === void 0 ? void 0 : _a.call(pluginOptions, parameters, outputFormats)) !== null && _b !== void 0 ? _b : resolveConfigs(parameters, outputFormats); const img = loadImage(decodeURIComponent(srcURL.pathname)); const outputMetadatas = []; const logger = { info: (msg) => this.warn(msg), warn: (msg) => this.warn(msg), error: (msg) => this.error(msg) }; for (const config of imageConfigs) { const { transforms } = generateTransforms(config, transformFactories, logger); const { image, metadata } = await applyTransforms(transforms, img, pluginOptions.removeMetadata); const fileName = basename(srcURL.pathname, extname(srcURL.pathname)) + `.${metadata.format}`; const fileHandle = this.emitFile({ name: fileName, source: await image.toBuffer(), type: 'asset' }); metadata.src = `__ROLLUP_IMAGE_ASSET__${fileHandle}__`; metadata.image = image; outputMetadatas.push(metadata); } let outputFormat = urlFormat(); for (const [key, format] of Object.entries(outputFormats)) { if (directives.has(key)) { const params = (_c = directives .get(key)) === null || _c === void 0 ? void 0 : _c.split(';').filter((v) => !!v); outputFormat = format((params === null || params === void 0 ? void 0 : params.length) ? params : undefined); break; } } return dataToEsm(outputFormat(outputMetadatas)); }, renderChunk(code) { const assetUrlRE = /__ROLLUP_IMAGE_ASSET__([a-z\d]{8})__(?:_(.*?)__)?/g; let match; let s; while ((match = assetUrlRE.exec(code))) { s = s || (s = new MagicString(code)); const [full, hash, postfix = ''] = match; const file = this.getFileName(hash); const outputFilepath = file + postfix; s.overwrite(match.index, match.index + full.length, outputFilepath); } if (s) { return { code: s.toString(), map: s.generateMap({ hires: true }) }; } else { return null; } } }; } export { imagetools }; //# sourceMappingURL=index.mjs.map