|
| 1 | +let path = require("path"); |
| 2 | +let FileFinder = require("faucet-pipeline-core/lib/util/files/finder"); |
| 3 | +let sharp = require("sharp"); |
| 4 | +let SVGO = require("svgo"); |
| 5 | +let { stat, readFile } = require("fs").promises; |
| 6 | +let { abort } = require("faucet-pipeline-core/lib/util"); |
| 7 | + |
| 8 | +// we can optimize the settings here, but some would require libvips |
| 9 | +// to be compiled with additional stuff |
| 10 | +let settings = { |
| 11 | + svg: {}, |
| 12 | + png: { adaptiveFiltering: true }, |
| 13 | + jpeg: { progressive: true }, |
| 14 | + webp: {} |
| 15 | +}; |
| 16 | + |
| 17 | +module.exports = { |
| 18 | + key: "images", |
| 19 | + bucket: "static", |
| 20 | + plugin: faucetImages |
| 21 | +}; |
| 22 | + |
| 23 | +function faucetImages(config, assetManager) { |
| 24 | + let optimizers = config.map(optimizerConfig => |
| 25 | + makeOptimizer(optimizerConfig, assetManager)); |
| 26 | + |
| 27 | + return filepaths => Promise.all(optimizers.map(optimize => optimize(filepaths))); |
| 28 | +} |
| 29 | + |
| 30 | +function makeOptimizer(optimizerConfig, assetManager) { |
| 31 | + let source = assetManager.resolvePath(optimizerConfig.source); |
| 32 | + let target = assetManager.resolvePath(optimizerConfig.target, { |
| 33 | + enforceRelative: true |
| 34 | + }); |
| 35 | + let fileFinder = new FileFinder(source, { |
| 36 | + skipDotfiles: true, |
| 37 | + // TODO: make configurable |
| 38 | + filter: withFileExtension("jpg", "jpeg", "png", "webp", "svg") |
| 39 | + }); |
| 40 | + let { |
| 41 | + fingerprint, |
| 42 | + format, |
| 43 | + width, |
| 44 | + height, |
| 45 | + keepRatio, |
| 46 | + scale, |
| 47 | + suffix |
| 48 | + } = optimizerConfig; |
| 49 | + |
| 50 | + return async filepaths => { |
| 51 | + let [fileNames, targetDir] = await Promise.all([ |
| 52 | + (filepaths ? fileFinder.match(filepaths) : fileFinder.all()), |
| 53 | + determineTargetDir(source, target) |
| 54 | + ]); |
| 55 | + return processFiles(fileNames, { |
| 56 | + assetManager, |
| 57 | + source, |
| 58 | + target, |
| 59 | + targetDir, |
| 60 | + fingerprint, |
| 61 | + variant: { |
| 62 | + format, width, height, keepRatio, scale, suffix |
| 63 | + } |
| 64 | + }); |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +// If `source` is a directory, `target` is used as target directory - |
| 69 | +// otherwise, `target`'s parent directory is used |
| 70 | +async function determineTargetDir(source, target) { |
| 71 | + let results = await stat(source); |
| 72 | + return results.isDirectory() ? target : path.dirname(target); |
| 73 | +} |
| 74 | + |
| 75 | +async function processFiles(fileNames, config) { |
| 76 | + return Promise.all(fileNames.map(fileName => processFile(fileName, config))); |
| 77 | +} |
| 78 | + |
| 79 | +async function processFile(fileName, |
| 80 | + { source, target, targetDir, fingerprint, assetManager, variant, suffix }) { |
| 81 | + let sourcePath = path.join(source, fileName); |
| 82 | + let targetPath = path.join(target, fileName); |
| 83 | + |
| 84 | + let format = variant.format ? variant.format : fileExtension(fileName); |
| 85 | + |
| 86 | + let output = format === "svg" ? |
| 87 | + await optimizeSVG(sourcePath) : |
| 88 | + await optimizeBitmap(sourcePath, format, variant); |
| 89 | + |
| 90 | + let writeOptions = { targetDir }; |
| 91 | + if(fingerprint !== undefined) { |
| 92 | + writeOptions.fingerprint = fingerprint; |
| 93 | + } |
| 94 | + return assetManager.writeFile(targetPath, output, writeOptions); |
| 95 | +} |
| 96 | + |
| 97 | +async function optimizeSVG(sourcePath) { |
| 98 | + let input = await readFile(sourcePath); |
| 99 | + |
| 100 | + try { |
| 101 | + let svgo = new SVGO(settings.svg); |
| 102 | + let output = await svgo.optimize(input); |
| 103 | + return output.data; |
| 104 | + } catch(error) { |
| 105 | + abort(`Only SVG can be converted to SVG: ${sourcePath}`); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +async function optimizeBitmap(sourcePath, format, |
| 110 | + { width, height, scale, keepRatio = true }) { |
| 111 | + let image = sharp(sourcePath); |
| 112 | + |
| 113 | + if(scale) { |
| 114 | + let metadata = await image.metadata(); |
| 115 | + image.resize({ width: metadata.width * scale, height: metadata.height * scale }); |
| 116 | + } |
| 117 | + |
| 118 | + if(width || height) { |
| 119 | + let fit = keepRatio ? "inside" : "cover"; |
| 120 | + image.resize({ width: width, height: height, fit: sharp.fit[fit] }); |
| 121 | + } |
| 122 | + |
| 123 | + switch(format) { |
| 124 | + case "jpg": |
| 125 | + case "jpeg": |
| 126 | + image.jpeg(settings.jpeg); |
| 127 | + break; |
| 128 | + case "png": |
| 129 | + image.png(settings.png); |
| 130 | + break; |
| 131 | + case "webp": |
| 132 | + image.webp(settings.webp); |
| 133 | + break; |
| 134 | + default: |
| 135 | + abort(`unsupported format ${format}. We support: JPG, PNG, WebP, SVG`); |
| 136 | + } |
| 137 | + |
| 138 | + return image.toBuffer(); |
| 139 | +} |
| 140 | + |
| 141 | +function withFileExtension(...extensions) { |
| 142 | + return filename => extensions.includes(fileExtension(filename)); |
| 143 | +} |
| 144 | + |
| 145 | +// extname follows this annoying idea that the dot belongs to the extension |
| 146 | +function fileExtension(filename) { |
| 147 | + return path.extname(filename).slice(1); |
| 148 | +} |
0 commit comments