UNPKG

rollup-plugin-imagetools

Version:
107 lines (103 loc) 4.94 kB
'use strict'; var imagetoolsCore = require('imagetools-core'); var pluginutils = require('@rollup/pluginutils'); var MagicString = require('magic-string'); var path = require('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 = pluginutils.createFilter(pluginOptions.include, pluginOptions.exclude); const transformFactories = pluginOptions.extendTransforms ? pluginOptions.extendTransforms(imagetoolsCore.builtins) : imagetoolsCore.builtins; const outputFormats = pluginOptions.extendOutputFormats ? pluginOptions.extendOutputFormats(imagetoolsCore.builtinOutputFormats) : imagetoolsCore.builtinOutputFormats; return { name: 'imagetools', resolveId(source, importer = '') { const id = path.resolve(path.dirname(importer), source); if (!filter(id)) return null; return id; }, async load(id) { var _a, _b, _c; if (!filter(id)) return null; const srcURL = imagetoolsCore.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 = imagetoolsCore.extractEntries(directives); const imageConfigs = (_b = (_a = pluginOptions.resolveConfigs) === null || _a === void 0 ? void 0 : _a.call(pluginOptions, parameters, outputFormats)) !== null && _b !== void 0 ? _b : imagetoolsCore.resolveConfigs(parameters, outputFormats); const img = imagetoolsCore.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 } = imagetoolsCore.generateTransforms(config, transformFactories, logger); const { image, metadata } = await imagetoolsCore.applyTransforms(transforms, img, pluginOptions.removeMetadata); const fileName = path.basename(srcURL.pathname, path.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 = imagetoolsCore.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 pluginutils.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; } } }; } exports.imagetools = imagetools; Object.keys(imagetoolsCore).forEach(function (k) { if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, { enumerable: true, get: function () { return imagetoolsCore[k]; } }); }); //# sourceMappingURL=index.cjs.map