|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const { imageSizeFromFile } = require('image-size/fromFile') |
| 4 | + |
| 5 | +const IMAGE_EXT_PATTERN = /\.(png|jpe?g|gif|svg|webp|avif)$/i |
| 6 | + |
| 7 | +/** |
| 8 | + * Parse intrinsic SVG dimensions (width/height attributes, then viewBox). |
| 9 | + * |
| 10 | + * @param {string} contents SVG file contents. |
| 11 | + * @return {{ width: number, height: number }|null} |
| 12 | + */ |
| 13 | +function getSvgDimensionsFromContents(contents) { |
| 14 | + if (!contents) { |
| 15 | + return null |
| 16 | + } |
| 17 | + |
| 18 | + const widthMatch = contents.match(/\bwidth="(\d+(?:\.\d+)?)"/) |
| 19 | + const heightMatch = contents.match(/\bheight="(\d+(?:\.\d+)?)"/) |
| 20 | + |
| 21 | + if (widthMatch && heightMatch) { |
| 22 | + return { |
| 23 | + width: Math.round(Number(widthMatch[1])), |
| 24 | + height: Math.round(Number(heightMatch[1])), |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const viewBoxMatch = contents.match(/\bviewBox="[\d.\s-]+?\s+([\d.]+)\s+([\d.]+)"/) |
| 29 | + |
| 30 | + if (viewBoxMatch) { |
| 31 | + return { |
| 32 | + width: Math.max(1, Math.round(Number(viewBoxMatch[1]))), |
| 33 | + height: Math.max(1, Math.round(Number(viewBoxMatch[2]))), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return null |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Read width/height from a built image file under dist/. |
| 42 | + * |
| 43 | + * @param {string} filePath Absolute path to the image file. |
| 44 | + * @return {Promise<{ width: number, height: number }|null>} |
| 45 | + */ |
| 46 | +async function getImageDimensions(filePath) { |
| 47 | + if (!fs.existsSync(filePath)) { |
| 48 | + return null |
| 49 | + } |
| 50 | + |
| 51 | + if (/\.svg$/i.test(filePath)) { |
| 52 | + const contents = fs.readFileSync(filePath, 'utf8') |
| 53 | + return getSvgDimensionsFromContents(contents) |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + const dimensions = await imageSizeFromFile(filePath) |
| 58 | + |
| 59 | + if (!dimensions.width || !dimensions.height) { |
| 60 | + return null |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + width: dimensions.width, |
| 65 | + height: dimensions.height, |
| 66 | + } |
| 67 | + } catch (error) { |
| 68 | + return null |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Webpack plugin: write static image dimensions to a JSON manifest under dist/. |
| 74 | + */ |
| 75 | +class WebpackStaticImageDimensionsPlugin { |
| 76 | + constructor(options = {}) { |
| 77 | + this.options = { |
| 78 | + outputPath: 'dist', |
| 79 | + manifestFile: 'image-dimensions.json', |
| 80 | + imagesSubdir: 'images', |
| 81 | + ...options, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + apply(compiler) { |
| 86 | + compiler.hooks.afterEmit.tapAsync('WebpackStaticImageDimensionsPlugin', async (compilation, callback) => { |
| 87 | + try { |
| 88 | + const context = compiler.options.context |
| 89 | + const outputPath = path.resolve(context, this.options.outputPath) |
| 90 | + const manifestPath = path.join(outputPath, this.options.manifestFile) |
| 91 | + const imageDimensions = {} |
| 92 | + |
| 93 | + const assets = compilation.getAssets() |
| 94 | + |
| 95 | + for (const asset of assets) { |
| 96 | + if (!asset.name.startsWith(`${this.options.imagesSubdir}/`) || !IMAGE_EXT_PATTERN.test(asset.name)) { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + const filePath = path.join(outputPath, asset.name) |
| 101 | + const dimensions = await getImageDimensions(filePath) |
| 102 | + |
| 103 | + if (!dimensions) { |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + const themeRelativePath = `dist/${asset.name.replace(/\\/g, '/')}` |
| 108 | + imageDimensions[themeRelativePath] = dimensions |
| 109 | + } |
| 110 | + |
| 111 | + const manifest = { |
| 112 | + imageDimensions, |
| 113 | + } |
| 114 | + |
| 115 | + fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`) |
| 116 | + callback() |
| 117 | + } catch (error) { |
| 118 | + callback(error) |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = WebpackStaticImageDimensionsPlugin |
0 commit comments