From 8e4ba762235b5a9942574004a5ae57578db41d9d Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Wed, 5 Apr 2023 13:37:18 -0400 Subject: [PATCH] remove the need for 'image-size' dependency (#36202) --- package-lock.json | 43 -------------------------------------- package.json | 1 - script/README.md | 10 --------- script/list-image-sizes.js | 29 ++++++++++++------------- 4 files changed, 13 insertions(+), 70 deletions(-) diff --git a/package-lock.json b/package-lock.json index a61718c39f..98844f545d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -173,7 +173,6 @@ }, "optionalDependencies": { "esm": "^3.2.25", - "image-size": "^1.0.1", "jest-puppeteer": "^5.0.4", "puppeteer": "^9.1.1", "website-scraper": "^5.0.0" @@ -10595,21 +10594,6 @@ "dev": true, "license": "ISC" }, - "node_modules/image-size": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.2.tgz", - "integrity": "sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg==", - "optional": true, - "dependencies": { - "queue": "6.0.2" - }, - "bin": { - "image-size": "bin/image-size.js" - }, - "engines": { - "node": ">=14.0.0" - } - }, "node_modules/immutable": { "version": "4.0.0", "devOptional": true, @@ -16759,15 +16743,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/queue": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz", - "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==", - "optional": true, - "dependencies": { - "inherits": "~2.0.3" - } - }, "node_modules/queue-microtask": { "version": "1.2.3", "dev": true, @@ -27620,15 +27595,6 @@ "version": "1.0.1", "dev": true }, - "image-size": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.0.2.tgz", - "integrity": "sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg==", - "optional": true, - "requires": { - "queue": "6.0.2" - } - }, "immutable": { "version": "4.0.0", "devOptional": true @@ -31739,15 +31705,6 @@ "side-channel": "^1.0.4" } }, - "queue": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz", - "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==", - "optional": true, - "requires": { - "inherits": "~2.0.3" - } - }, "queue-microtask": { "version": "1.2.3", "dev": true diff --git a/package.json b/package.json index 773768ff0a..98b35e2ceb 100644 --- a/package.json +++ b/package.json @@ -178,7 +178,6 @@ "name": "docs.github.com", "optionalDependencies": { "esm": "^3.2.25", - "image-size": "^1.0.1", "jest-puppeteer": "^5.0.4", "puppeteer": "^9.1.1", "website-scraper": "^5.0.0" diff --git a/script/README.md b/script/README.md index ffccb3333e..1db40e217b 100644 --- a/script/README.md +++ b/script/README.md @@ -469,14 +469,6 @@ A helper that returns an array of files for a given path and file extension. This script lists all local image files, sorted by their dimensions. -NOTE: If you get this error: - - Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'image-size' ... - -it's because you haven't installed all the *optional* dependencies. To do that, run: - - npm install --include=optional - --- @@ -762,5 +754,3 @@ Exceptions: * Links with fragments (e.g., [Bar](/foo#bar)) will get their root l This script crawls the script directory, hooks on special comment markers in each script, and adds the comment to `script/README.md`. --- - - diff --git a/script/list-image-sizes.js b/script/list-image-sizes.js index 98c73ff1d3..6fd24c1f0f 100755 --- a/script/list-image-sizes.js +++ b/script/list-image-sizes.js @@ -4,37 +4,34 @@ // // This script lists all local image files, sorted by their dimensions. // -// NOTE: If you get this error: -// -// Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'image-size' ... -// -// it's because you haven't installed all the *optional* dependencies. -// To do that, run: -// -// npm install --include=optional -// // [end-readme] import { fileURLToPath } from 'url' import path from 'path' import walk from 'walk-sync' -import imageSize from 'image-size' +import sharp from 'sharp' import { chain } from 'lodash-es' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const imagesPath = path.join(__dirname, '../assets/images') const imagesExtensions = ['.jpg', '.jpeg', '.png', '.gif'] -const images = chain(walk(imagesPath, { directories: false })) - .filter((relativePath) => { - return imagesExtensions.includes(path.extname(relativePath.toLowerCase())) - }) - .map((relativePath) => { +const files = chain(walk(imagesPath, { directories: false })).filter((relativePath) => { + return imagesExtensions.includes(path.extname(relativePath.toLowerCase())) +}) +const infos = await Promise.all( + files.map(async (relativePath) => { const fullPath = path.join(imagesPath, relativePath) - const { width, height } = imageSize(fullPath) + const image = sharp(fullPath) + const { width, height } = await image.metadata() const size = width * height return { relativePath, width, height, size } }) +) +const images = files + .map((relativePath, i) => { + return { relativePath, ...infos[i] } + }) .orderBy('size', 'desc') .value()