From 656896eddd058cc4663fc3c3cf19a7d998ac3e6e Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 18 Mar 2026 09:31:51 +0100 Subject: [PATCH 01/15] feat: esm --- .gitignore | 1 + build.mjs | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/channel.js | 4 ++-- lib/colour.js | 6 +++--- lib/composite.js | 4 ++-- lib/constructor.js | 14 +++++++------- lib/index.js | 29 +++++++++++++++++++---------- lib/input.js | 6 +++--- lib/is.js | 2 +- lib/libvips.js | 17 +++++++++-------- lib/operation.js | 4 ++-- lib/output.js | 8 ++++---- lib/resize.js | 4 ++-- lib/sharp.js | 14 +++++++------- lib/utility.js | 26 +++++++++++++++----------- package.json | 16 ++++++++++++---- 16 files changed, 133 insertions(+), 66 deletions(-) create mode 100644 build.mjs diff --git a/.gitignore b/.gitignore index 82f73b3f0..f2ac38d2b 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ package-lock.json .astro docs/dist release-notes.md +dist \ No newline at end of file diff --git a/build.mjs b/build.mjs new file mode 100644 index 000000000..8b6b83dd5 --- /dev/null +++ b/build.mjs @@ -0,0 +1,44 @@ +// @ts-check +import * as fs from 'node:fs/promises'; +import * as esbuild from 'esbuild'; + +await fs.rm(new URL('./dist/', import.meta.url), { force: true, recursive: true }); +await esbuild.build({ + minify: false, + format: 'esm', + platform: 'node', + target: 'node18', + sourcemap: false, + sourcesContent: false, + bundle: false, + entryPoints: ['./lib/*.js'], + outdir: 'dist', +}); +await esbuild.build({ + minify: false, + format: 'cjs', + platform: 'node', + target: 'node18', + sourcemap: false, + sourcesContent: false, + bundle: false, + entryPoints: ['./lib/*.js'], + outdir: 'dist', + plugins: [ + // ESM TLA to CJS require() is not supported ootb + // https://github.com/evanw/esbuild/issues/253 + { + name: 'tla-to-require', + setup(build) { + build.onLoad({ filter: /\.js$/ }, async (args) => { + const contents = await fs.readFile(args.path, 'utf-8'); + return { + contents: contents.replaceAll('await import(', 'require('), + loader: 'js', + }; + }); + }, + }, + ], + outExtension: { '.js': '.cjs' }, +}); diff --git a/lib/channel.js b/lib/channel.js index 3c6c0b439..5d9888c79 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -const is = require('./is'); +import is from './is'; /** * Boolean operations for bandbool. @@ -163,7 +163,7 @@ function bandbool (boolOp) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Object.assign(Sharp.prototype, { // Public instance functions removeAlpha, diff --git a/lib/colour.js b/lib/colour.js index e61c248a8..f2c5155d7 100644 --- a/lib/colour.js +++ b/lib/colour.js @@ -3,8 +3,8 @@ SPDX-License-Identifier: Apache-2.0 */ -const color = require('@img/colour'); -const is = require('./is'); +import color from '@img/colour'; +import is from './is'; /** * Colourspaces. @@ -175,7 +175,7 @@ function _setBackgroundColourOption (key, value) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Object.assign(Sharp.prototype, { // Public tint, diff --git a/lib/composite.js b/lib/composite.js index 1c3e5e629..9df6fe0c3 100644 --- a/lib/composite.js +++ b/lib/composite.js @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -const is = require('./is'); +import is from './is'; /** * Blend modes. @@ -206,7 +206,7 @@ function composite (images) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Sharp.prototype.composite = composite; Sharp.blend = blend; }; diff --git a/lib/constructor.js b/lib/constructor.js index 9aac8105c..c1547f4c8 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -3,11 +3,11 @@ SPDX-License-Identifier: Apache-2.0 */ -const util = require('node:util'); -const stream = require('node:stream'); -const is = require('./is'); +import util from 'node:util'; +import stream from 'node:stream'; +import is from './is'; -require('./sharp'); +import './sharp'; // Use NODE_DEBUG=sharp to enable libvips warnings const debuglog = util.debuglog('sharp'); @@ -426,8 +426,8 @@ Object.setPrototypeOf(Sharp, stream.Duplex); * @example * // Create a pipeline that will download an image, resize it and format it to different files * // Using Promises to know when the pipeline is complete - * const fs = require("fs"); - * const got = require("got"); + * import fs from "node:fs"; + * import got from "got"; * const sharpStream = sharp({ failOn: 'none' }); * * const promises = []; @@ -496,4 +496,4 @@ Object.assign(Sharp.prototype, { clone }); * @module Sharp * @private */ -module.exports = Sharp; +export default Sharp; diff --git a/lib/index.js b/lib/index.js index b80191d71..58d549bb8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,14 +3,23 @@ SPDX-License-Identifier: Apache-2.0 */ -const Sharp = require('./constructor'); -require('./input')(Sharp); -require('./resize')(Sharp); -require('./composite')(Sharp); -require('./operation')(Sharp); -require('./colour')(Sharp); -require('./channel')(Sharp); -require('./output')(Sharp); -require('./utility')(Sharp); +import Sharp from './constructor'; +import input from './input'; +import resize from './resize'; +import composite from './composite'; +import operation from './operation'; +import colour from './colour'; +import channel from './channel'; +import output from './output'; +import utility from './utility'; -module.exports = Sharp; +input(Sharp); +resize(Sharp); +composite(Sharp); +operation(Sharp); +colour(Sharp); +channel(Sharp); +output(Sharp); +utility(Sharp); + +export default Sharp; diff --git a/lib/input.js b/lib/input.js index 48388a1d2..b9b4f2232 100644 --- a/lib/input.js +++ b/lib/input.js @@ -3,8 +3,8 @@ SPDX-License-Identifier: Apache-2.0 */ -const is = require('./is'); -const sharp = require('./sharp'); +import is from './is'; +import sharp from './sharp'; /** * Justification alignment @@ -792,7 +792,7 @@ function stats (callback) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Object.assign(Sharp.prototype, { // Private _inputOptionsFromObject, diff --git a/lib/is.js b/lib/is.js index 3ac9a1a35..bfb4188f8 100644 --- a/lib/is.js +++ b/lib/is.js @@ -124,7 +124,7 @@ const nativeError = (native, context) => { return context; }; -module.exports = { +export default { defined, object, plainObject, diff --git a/lib/libvips.js b/lib/libvips.js index 881dc5c13..7a28e29be 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -3,14 +3,15 @@ SPDX-License-Identifier: Apache-2.0 */ -const { spawnSync } = require('node:child_process'); -const { createHash } = require('node:crypto'); -const semverCoerce = require('semver/functions/coerce'); -const semverGreaterThanOrEqualTo = require('semver/functions/gte'); -const semverSatisfies = require('semver/functions/satisfies'); -const detectLibc = require('detect-libc'); +import { spawnSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import semverCoerce from 'semver/functions/coerce'; +import semverGreaterThanOrEqualTo from 'semver/functions/gte'; +import semverSatisfies from 'semver/functions/satisfies'; +import detectLibc from 'detect-libc'; -const { config, engines, optionalDependencies } = require('../package.json'); +// TODO: check if that works +import { config, engines, optionalDependencies } from '../package.json'; /* node:coverage ignore next */ const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || config.libvips; @@ -189,7 +190,7 @@ const useGlobalLibvips = (logger) => { return !!globalVipsVersion && semverGreaterThanOrEqualTo(globalVipsVersion, minimumLibvipsVersion); }; -module.exports = { +export default { minimumLibvipsVersion, prebuiltPlatforms, buildPlatformArch, diff --git a/lib/operation.js b/lib/operation.js index ebbf54e9c..d47a7ac08 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -const is = require('./is'); +import is from './is'; /** * How accurate an operation should be. @@ -987,7 +987,7 @@ function modulate (options) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Object.assign(Sharp.prototype, { autoOrient, rotate, diff --git a/lib/output.js b/lib/output.js index 27a6ac470..ea93d6c95 100644 --- a/lib/output.js +++ b/lib/output.js @@ -3,9 +3,9 @@ SPDX-License-Identifier: Apache-2.0 */ -const path = require('node:path'); -const is = require('./is'); -const sharp = require('./sharp'); +import path from 'node:path'; +import is from './is'; +import sharp from './sharp'; const formats = new Map([ ['heic', 'heif'], @@ -1630,7 +1630,7 @@ function _pipeline (callback, stack) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Object.assign(Sharp.prototype, { // Public toFile, diff --git a/lib/resize.js b/lib/resize.js index 544fbba3a..3a49955a6 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -const is = require('./is'); +import is from './is'; /** * Weighting to apply when using contain/cover fit. @@ -579,7 +579,7 @@ function trim (options) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Object.assign(Sharp.prototype, { resize, extend, diff --git a/lib/sharp.js b/lib/sharp.js index 1081c9314..427f618b7 100644 --- a/lib/sharp.js +++ b/lib/sharp.js @@ -5,9 +5,9 @@ // Inspects the runtime environment and exports the relevant sharp.node binary -const { familySync, versionSync } = require('detect-libc'); +import { familySync, versionSync } from 'detect-libc'; -const { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips'); +import { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } from './libvips'; const runtimePlatform = runtimePlatformArch(); const paths = [ @@ -23,7 +23,7 @@ let path, sharp; const errors = []; for (path of paths) { try { - sharp = require(path); + sharp = await import(path); break; } catch (err) { errors.push(err); @@ -37,9 +37,7 @@ if (sharp && path.startsWith('@img/sharp-linux-x64') && !sharp._isUsingX64V2()) sharp = null; } -if (sharp) { - module.exports = sharp; -} else { +if (!sharp) { const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os)); const help = [`Could not load the "sharp" module using the ${runtimePlatform} runtime`]; @@ -79,7 +77,7 @@ if (sharp) { } if (isLinux && /(symbol not found|CXXABI_)/i.test(messages)) { try { - const { config } = require(`@img/sharp-libvips-${runtimePlatform}/package`); + const { config } = await import(`@img/sharp-libvips-${runtimePlatform}/package`); const libcFound = `${familySync()} ${versionSync()}`; const libcRequires = `${config.musl ? 'musl' : 'glibc'} ${config.musl || config.glibc}`; help.push( @@ -119,3 +117,5 @@ if (sharp) { ); throw new Error(help.join('\n')); } + +export default sharp \ No newline at end of file diff --git a/lib/utility.js b/lib/utility.js index c0ad39f86..20e602c5a 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -3,12 +3,15 @@ SPDX-License-Identifier: Apache-2.0 */ -const events = require('node:events'); -const detectLibc = require('detect-libc'); +import events from 'node:events'; +import detectLibc from 'detect-libc'; -const is = require('./is'); -const { runtimePlatformArch } = require('./libvips'); -const sharp = require('./sharp'); +import is from './is'; +import { runtimePlatformArch } from './libvips'; +import sharp from './sharp'; + +// TODO: check it works +import { version } from '../package.json' const runtimePlatform = runtimePlatformArch(); const libvipsVersion = sharp.libvipsVersion(); @@ -61,19 +64,19 @@ let versions = { if (!libvipsVersion.isGlobal) { if (!libvipsVersion.isWasm) { try { - versions = require(`@img/sharp-${runtimePlatform}/versions`); + versions = await import(`@img/sharp-${runtimePlatform}/versions`); } catch (_) { try { - versions = require(`@img/sharp-libvips-${runtimePlatform}/versions`); + versions = await import(`@img/sharp-libvips-${runtimePlatform}/versions`); } catch (_) {} } } else { try { - versions = require('@img/sharp-wasm32/versions'); + versions = await import('@img/sharp-wasm32/versions'); } catch (_) {} } } -versions.sharp = require('../package.json').version; +versions.sharp = version; /* node:coverage ignore next 5 */ if (versions.heif && format.heif) { @@ -156,7 +159,8 @@ if (detectLibc.familySync() === detectLibc.GLIBC && !sharp._isUsingJemalloc()) { sharp.concurrency(1); } else if (detectLibc.familySync() === detectLibc.MUSL && sharp.concurrency() === 1024) { // Reduce default concurrency when musl thread over-subscription detected - sharp.concurrency(require('node:os').availableParallelism()); + const { availableParallelism } = await import('node:os') + sharp.concurrency(availableParallelism()); } /** @@ -277,7 +281,7 @@ function unblock (options) { * @module Sharp * @private */ -module.exports = (Sharp) => { +export default (Sharp) => { Sharp.cache = cache; Sharp.concurrency = concurrency; Sharp.counters = counters; diff --git a/package.json b/package.json index 0c0d00988..a0c823333 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ ], "scripts": { "build": "node install/build.js", + "build:test": "node build.mjs", "install": "node install/check.js || npm run build", "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*", "test": "npm run lint && npm run test-unit", @@ -108,14 +109,20 @@ "docs-serve": "cd docs && npm start", "docs-publish": "cd docs && npm run build && npx firebase-tools deploy --project pixelplumbing --only hosting:pixelplumbing-sharp" }, - "type": "commonjs", - "main": "lib/index.js", - "types": "lib/index.d.ts", + "type": "module", "files": [ + "dist", "install", - "lib", + "lib/index.d.ts", "src/*.{cc,h,gyp}" ], + "exports": { + ".": { + "types": "./lib/index.d.ts", + "import": "./dist/index.js", + "require": "./dist/index.cjs" + } + }, "repository": { "type": "git", "url": "git://github.com/lovell/sharp.git" @@ -180,6 +187,7 @@ "@img/sharp-libvips-win32-x64": "1.2.4", "@types/node": "*", "emnapi": "^1.7.0", + "esbuild": "0.27.4", "exif-reader": "^2.0.2", "extract-zip": "^2.0.1", "icc": "^3.0.0", From 58d9e1d6a6da264f1fcf5b3ab026756063fe6b1e Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 18 Mar 2026 09:38:05 +0100 Subject: [PATCH 02/15] todos --- lib/libvips.js | 5 +++-- lib/utility.js | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/libvips.js b/lib/libvips.js index 7a28e29be..fcf8f1002 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -5,13 +5,14 @@ import { spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; +import fs from 'node:fs'; import semverCoerce from 'semver/functions/coerce'; import semverGreaterThanOrEqualTo from 'semver/functions/gte'; import semverSatisfies from 'semver/functions/satisfies'; import detectLibc from 'detect-libc'; -// TODO: check if that works -import { config, engines, optionalDependencies } from '../package.json'; +// Cannot use an import because sometimes it can need the attr or with keyword +const { config, engines, optionalDependencies } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8')); /* node:coverage ignore next */ const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || config.libvips; diff --git a/lib/utility.js b/lib/utility.js index 20e602c5a..fb3ed8181 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -4,15 +4,13 @@ */ import events from 'node:events'; +import fs from 'node:fs'; import detectLibc from 'detect-libc'; import is from './is'; import { runtimePlatformArch } from './libvips'; import sharp from './sharp'; -// TODO: check it works -import { version } from '../package.json' - const runtimePlatform = runtimePlatformArch(); const libvipsVersion = sharp.libvipsVersion(); @@ -76,7 +74,8 @@ if (!libvipsVersion.isGlobal) { } catch (_) {} } } -versions.sharp = version; +// Cannot use an import because sometimes it can need the attr or with keyword +versions.sharp = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8')).version; /* node:coverage ignore next 5 */ if (versions.heif && format.heif) { From f58ebfe79e0309bd9c10a20f5cfbad39503064e6 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 18 Mar 2026 09:42:24 +0100 Subject: [PATCH 03/15] wip --- package.json | 2 +- build.mjs => scripts/build.js | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename build.mjs => scripts/build.js (100%) diff --git a/package.json b/package.json index a0c823333..6ef1be88e 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "Don Denton " ], "scripts": { - "build": "node install/build.js", + "build": "node scripts/build.js && node install/build.js", "build:test": "node build.mjs", "install": "node install/check.js || npm run build", "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*", diff --git a/build.mjs b/scripts/build.js similarity index 100% rename from build.mjs rename to scripts/build.js From 0b5fda4e095ed71e26d490dd34db5cc1170eab79 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 18 Mar 2026 09:47:34 +0100 Subject: [PATCH 04/15] docs --- .github/ISSUE_TEMPLATE/installation.md | 2 +- .github/ISSUE_TEMPLATE/possible-bug.md | 2 +- README.md | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/installation.md b/.github/ISSUE_TEMPLATE/installation.md index b47d10e9a..7ec5f38e7 100644 --- a/.github/ISSUE_TEMPLATE/installation.md +++ b/.github/ISSUE_TEMPLATE/installation.md @@ -1,6 +1,6 @@ --- name: Installation -about: Something went wrong during either 'npm install sharp' or 'require("sharp")' +about: Something went wrong during either 'npm install sharp' or 'import "sharp"' labels: installation --- diff --git a/.github/ISSUE_TEMPLATE/possible-bug.md b/.github/ISSUE_TEMPLATE/possible-bug.md index c7e130e17..6e1fff328 100644 --- a/.github/ISSUE_TEMPLATE/possible-bug.md +++ b/.github/ISSUE_TEMPLATE/possible-bug.md @@ -14,7 +14,7 @@ labels: triage - [ ] Running `npm install sharp` completes without error. -- [ ] Running `node -e "require('sharp')"` completes without error. +- [ ] Running `node -e "import 'sharp'"` completes without error. If you cannot confirm both of these, please open an [installation issue](https://github.com/lovell/sharp/issues/new?labels=installation&template=installation.md) instead. diff --git a/README.md b/README.md index 47da52e8b..d82f7e2d6 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ npm install sharp ``` ```javascript +// ESM +import sharp from 'sharp'; + +// CJS const sharp = require('sharp'); ``` From f31f69bd75131bdfb4f41109cb093b1dfa38c9d9 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 18 Mar 2026 09:57:42 +0100 Subject: [PATCH 05/15] fix: package.json --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6ef1be88e..9e0773981 100644 --- a/package.json +++ b/package.json @@ -116,9 +116,11 @@ "lib/index.d.ts", "src/*.{cc,h,gyp}" ], + "main": "./dist/index.cjs", + "module": "./dist/index.js", + "types": "./lib/index.d.ts", "exports": { ".": { - "types": "./lib/index.d.ts", "import": "./dist/index.js", "require": "./dist/index.cjs" } @@ -207,4 +209,4 @@ "funding": { "url": "https://opencollective.com/libvips" } -} +} \ No newline at end of file From 28566ed1cb3333cb292aec15cbc632d2d5c5db99 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 2 Apr 2026 14:08:00 +0200 Subject: [PATCH 06/15] fix --- docs/src/content/docs/api-constructor.md | 4 +-- install/build.js | 2 +- install/check.js | 2 +- lib/{channel.js => channel.mjs} | 2 +- lib/{colour.js => colour.mjs} | 2 +- lib/{composite.js => composite.mjs} | 2 +- lib/{constructor.js => constructor.mjs} | 4 +-- lib/index.js | 25 ------------- lib/index.mjs | 25 +++++++++++++ lib/{input.js => input.mjs} | 4 +-- lib/{is.js => is.mjs} | 0 lib/{libvips.js => libvips.mjs} | 0 lib/{operation.js => operation.mjs} | 2 +- lib/{output.js => output.mjs} | 4 +-- lib/{resize.js => resize.mjs} | 2 +- lib/{sharp.js => sharp.mjs} | 2 +- lib/{utility.js => utility.mjs} | 6 ++-- package.json | 8 ++--- scripts/build.js | 44 ----------------------- scripts/build.mjs | 46 ++++++++++++++++++++++++ test/fixtures/index.js | 2 +- test/unit/clahe.js | 2 +- test/unit/failOn.js | 2 +- test/unit/libvips.js | 2 +- test/unit/text.js | 2 +- test/unit/trim.js | 2 +- 26 files changed, 100 insertions(+), 98 deletions(-) rename lib/{channel.js => channel.mjs} (99%) rename lib/{colour.js => colour.mjs} (99%) rename lib/{composite.js => composite.mjs} (99%) rename lib/{constructor.js => constructor.mjs} (99%) delete mode 100644 lib/index.js create mode 100644 lib/index.mjs rename lib/{input.js => input.mjs} (99%) rename lib/{is.js => is.mjs} (100%) rename lib/{libvips.js => libvips.mjs} (100%) rename lib/{operation.js => operation.mjs} (99%) rename lib/{output.js => output.mjs} (99%) rename lib/{resize.js => resize.mjs} (99%) rename lib/{sharp.js => sharp.mjs} (98%) rename lib/{utility.js => utility.mjs} (98%) delete mode 100644 scripts/build.js create mode 100644 scripts/build.mjs diff --git a/docs/src/content/docs/api-constructor.md b/docs/src/content/docs/api-constructor.md index 3504239dc..b3484701f 100644 --- a/docs/src/content/docs/api-constructor.md +++ b/docs/src/content/docs/api-constructor.md @@ -229,8 +229,8 @@ readableStream.pipe(pipeline); ```js // Create a pipeline that will download an image, resize it and format it to different files // Using Promises to know when the pipeline is complete -const fs = require("fs"); -const got = require("got"); +import fs from "node:fs"; +import got from "got"; const sharpStream = sharp({ failOn: 'none' }); const promises = []; diff --git a/install/build.js b/install/build.js index 2ca224586..82903a055 100644 --- a/install/build.js +++ b/install/build.js @@ -8,7 +8,7 @@ const { globalLibvipsVersion, log, spawnRebuild, -} = require('../lib/libvips'); +} = require('../dist/libvips.cjs'); log('Attempting to build from source via node-gyp'); log('See https://sharp.pixelplumbing.com/install#building-from-source'); diff --git a/install/check.js b/install/check.js index 1cfb7d32e..7d1902ae9 100644 --- a/install/check.js +++ b/install/check.js @@ -4,7 +4,7 @@ */ try { - const { useGlobalLibvips } = require('../lib/libvips'); + const { useGlobalLibvips } = require('../dist/libvips.cjs'); if (useGlobalLibvips() || process.env.npm_config_build_from_source) { process.exit(1); } diff --git a/lib/channel.js b/lib/channel.mjs similarity index 99% rename from lib/channel.js rename to lib/channel.mjs index 5d9888c79..1fdf0a990 100644 --- a/lib/channel.js +++ b/lib/channel.mjs @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -import is from './is'; +import is from './is.mjs'; /** * Boolean operations for bandbool. diff --git a/lib/colour.js b/lib/colour.mjs similarity index 99% rename from lib/colour.js rename to lib/colour.mjs index f2c5155d7..718b20a2a 100644 --- a/lib/colour.js +++ b/lib/colour.mjs @@ -4,7 +4,7 @@ */ import color from '@img/colour'; -import is from './is'; +import is from './is.mjs'; /** * Colourspaces. diff --git a/lib/composite.js b/lib/composite.mjs similarity index 99% rename from lib/composite.js rename to lib/composite.mjs index 9df6fe0c3..a8f2f2063 100644 --- a/lib/composite.js +++ b/lib/composite.mjs @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -import is from './is'; +import is from './is.mjs'; /** * Blend modes. diff --git a/lib/constructor.js b/lib/constructor.mjs similarity index 99% rename from lib/constructor.js rename to lib/constructor.mjs index c1547f4c8..480f20fd3 100644 --- a/lib/constructor.js +++ b/lib/constructor.mjs @@ -5,9 +5,9 @@ import util from 'node:util'; import stream from 'node:stream'; -import is from './is'; +import is from './is.mjs'; -import './sharp'; +import './sharp.mjs'; // Use NODE_DEBUG=sharp to enable libvips warnings const debuglog = util.debuglog('sharp'); diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 58d549bb8..000000000 --- a/lib/index.js +++ /dev/null @@ -1,25 +0,0 @@ -/*! - Copyright 2013 Lovell Fuller and others. - SPDX-License-Identifier: Apache-2.0 -*/ - -import Sharp from './constructor'; -import input from './input'; -import resize from './resize'; -import composite from './composite'; -import operation from './operation'; -import colour from './colour'; -import channel from './channel'; -import output from './output'; -import utility from './utility'; - -input(Sharp); -resize(Sharp); -composite(Sharp); -operation(Sharp); -colour(Sharp); -channel(Sharp); -output(Sharp); -utility(Sharp); - -export default Sharp; diff --git a/lib/index.mjs b/lib/index.mjs new file mode 100644 index 000000000..73a5ad2ed --- /dev/null +++ b/lib/index.mjs @@ -0,0 +1,25 @@ +/*! + Copyright 2013 Lovell Fuller and others. + SPDX-License-Identifier: Apache-2.0 +*/ + +import Sharp from './constructor.mjs'; +import input from './input.mjs'; +import resize from './resize.mjs'; +import composite from './composite.mjs'; +import operation from './operation.mjs'; +import colour from './colour.mjs'; +import channel from './channel.mjs'; +import output from './output.mjs'; +import utility from './utility.mjs'; + +input(Sharp); +resize(Sharp); +composite(Sharp); +operation(Sharp); +colour(Sharp); +channel(Sharp); +output(Sharp); +utility(Sharp); + +export default Sharp; diff --git a/lib/input.js b/lib/input.mjs similarity index 99% rename from lib/input.js rename to lib/input.mjs index b9b4f2232..dbffcc7a9 100644 --- a/lib/input.js +++ b/lib/input.mjs @@ -3,8 +3,8 @@ SPDX-License-Identifier: Apache-2.0 */ -import is from './is'; -import sharp from './sharp'; +import is from './is.mjs'; +import sharp from './sharp.mjs'; /** * Justification alignment diff --git a/lib/is.js b/lib/is.mjs similarity index 100% rename from lib/is.js rename to lib/is.mjs diff --git a/lib/libvips.js b/lib/libvips.mjs similarity index 100% rename from lib/libvips.js rename to lib/libvips.mjs diff --git a/lib/operation.js b/lib/operation.mjs similarity index 99% rename from lib/operation.js rename to lib/operation.mjs index d47a7ac08..d4d1ac777 100644 --- a/lib/operation.js +++ b/lib/operation.mjs @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -import is from './is'; +import is from './is.mjs'; /** * How accurate an operation should be. diff --git a/lib/output.js b/lib/output.mjs similarity index 99% rename from lib/output.js rename to lib/output.mjs index ea93d6c95..75346cd1a 100644 --- a/lib/output.js +++ b/lib/output.mjs @@ -4,8 +4,8 @@ */ import path from 'node:path'; -import is from './is'; -import sharp from './sharp'; +import is from './is.mjs'; +import sharp from './sharp.mjs'; const formats = new Map([ ['heic', 'heif'], diff --git a/lib/resize.js b/lib/resize.mjs similarity index 99% rename from lib/resize.js rename to lib/resize.mjs index 3a49955a6..d4da00eb3 100644 --- a/lib/resize.js +++ b/lib/resize.mjs @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */ -import is from './is'; +import is from './is.mjs'; /** * Weighting to apply when using contain/cover fit. diff --git a/lib/sharp.js b/lib/sharp.mjs similarity index 98% rename from lib/sharp.js rename to lib/sharp.mjs index 427f618b7..3a3de0288 100644 --- a/lib/sharp.js +++ b/lib/sharp.mjs @@ -7,7 +7,7 @@ import { familySync, versionSync } from 'detect-libc'; -import { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } from './libvips'; +import { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } from './libvips.mjs'; const runtimePlatform = runtimePlatformArch(); const paths = [ diff --git a/lib/utility.js b/lib/utility.mjs similarity index 98% rename from lib/utility.js rename to lib/utility.mjs index fb3ed8181..065d38acf 100644 --- a/lib/utility.js +++ b/lib/utility.mjs @@ -7,9 +7,9 @@ import events from 'node:events'; import fs from 'node:fs'; import detectLibc from 'detect-libc'; -import is from './is'; -import { runtimePlatformArch } from './libvips'; -import sharp from './sharp'; +import is from './is.mjs'; +import { runtimePlatformArch } from './libvips.mjs'; +import sharp from './sharp.mjs'; const runtimePlatform = runtimePlatformArch(); const libvipsVersion = sharp.libvipsVersion(); diff --git a/package.json b/package.json index 9e0773981..cd2a3723c 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "Don Denton " ], "scripts": { - "build": "node scripts/build.js && node install/build.js", + "build": "node scripts/build.mjs && node install/build.js", "build:test": "node build.mjs", "install": "node install/check.js || npm run build", "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*", @@ -109,7 +109,7 @@ "docs-serve": "cd docs && npm start", "docs-publish": "cd docs && npm run build && npx firebase-tools deploy --project pixelplumbing --only hosting:pixelplumbing-sharp" }, - "type": "module", + "type": "commonjs", "files": [ "dist", "install", @@ -117,11 +117,11 @@ "src/*.{cc,h,gyp}" ], "main": "./dist/index.cjs", - "module": "./dist/index.js", + "module": "./dist/index.mjs", "types": "./lib/index.d.ts", "exports": { ".": { - "import": "./dist/index.js", + "import": "./dist/index.mjs", "require": "./dist/index.cjs" } }, diff --git a/scripts/build.js b/scripts/build.js deleted file mode 100644 index 8b6b83dd5..000000000 --- a/scripts/build.js +++ /dev/null @@ -1,44 +0,0 @@ -// @ts-check -import * as fs from 'node:fs/promises'; -import * as esbuild from 'esbuild'; - -await fs.rm(new URL('./dist/', import.meta.url), { force: true, recursive: true }); -await esbuild.build({ - minify: false, - format: 'esm', - platform: 'node', - target: 'node18', - sourcemap: false, - sourcesContent: false, - bundle: false, - entryPoints: ['./lib/*.js'], - outdir: 'dist', -}); -await esbuild.build({ - minify: false, - format: 'cjs', - platform: 'node', - target: 'node18', - sourcemap: false, - sourcesContent: false, - bundle: false, - entryPoints: ['./lib/*.js'], - outdir: 'dist', - plugins: [ - // ESM TLA to CJS require() is not supported ootb - // https://github.com/evanw/esbuild/issues/253 - { - name: 'tla-to-require', - setup(build) { - build.onLoad({ filter: /\.js$/ }, async (args) => { - const contents = await fs.readFile(args.path, 'utf-8'); - return { - contents: contents.replaceAll('await import(', 'require('), - loader: 'js', - }; - }); - }, - }, - ], - outExtension: { '.js': '.cjs' }, -}); diff --git a/scripts/build.mjs b/scripts/build.mjs new file mode 100644 index 000000000..fdcd93cd0 --- /dev/null +++ b/scripts/build.mjs @@ -0,0 +1,46 @@ +// @ts-check +import * as fs from "node:fs/promises"; +import * as esbuild from "esbuild"; + +await fs.rm(new URL("../dist/", import.meta.url), { force: true, recursive: true }); + +/** @satisfies {esbuild.BuildOptions} */ +const sharedOptions = { + minify: false, + platform: "node", + target: "node18", + sourcemap: false, + sourcesContent: false, + bundle: false, + entryPoints: ["./lib/*.mjs"], + outdir: "dist", +}; + +await esbuild.build({ + ...sharedOptions, + format: "esm", + outExtension: { ".js": ".mjs" }, +}); +await esbuild.build({ + ...sharedOptions, + format: "cjs", + outExtension: { ".js": ".cjs" }, + plugins: [ + { + name: "cjs-compat", + setup(build) { + build.onLoad({ filter: /\.mjs$/ }, async (args) => { + const contents = await fs.readFile(args.path, "utf-8"); + return { + contents: contents + // ESM TLA to CJS require() is not supported ootb + // https://github.com/evanw/esbuild/issues/253 + .replaceAll("await import(", "require(") + .replaceAll("import.meta.url", "require('node:url').pathToFileURL(__filename)"), + loader: "js", + }; + }); + }, + }, + ], +}); diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 2448843e2..7898ddc2b 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -5,7 +5,7 @@ const path = require('node:path'); const sharp = require('../../'); -const maxColourDistance = require('../../lib/sharp')._maxColourDistance; +const maxColourDistance = require('../../dist/sharp.cjs')._maxColourDistance; // Helpers const getPath = (filename) => path.join(__dirname, filename); diff --git a/test/unit/clahe.js b/test/unit/clahe.js index f3d68ebbb..ed00a5411 100644 --- a/test/unit/clahe.js +++ b/test/unit/clahe.js @@ -6,7 +6,7 @@ const { describe, it } = require('node:test'); const assert = require('node:assert'); -const sharp = require('../../lib'); +const sharp = require('../../dist/index.cjs'); const fixtures = require('../fixtures'); describe('Clahe', () => { diff --git a/test/unit/failOn.js b/test/unit/failOn.js index c4d44e892..bd9ea1521 100644 --- a/test/unit/failOn.js +++ b/test/unit/failOn.js @@ -7,7 +7,7 @@ const { describe, it } = require('node:test'); const assert = require('node:assert'); const fs = require('node:fs'); -const sharp = require('../../lib'); +const sharp = require('../../dist/index.cjs'); const fixtures = require('../fixtures'); describe('failOn', () => { diff --git a/test/unit/libvips.js b/test/unit/libvips.js index 8ed822458..1b5349a6a 100644 --- a/test/unit/libvips.js +++ b/test/unit/libvips.js @@ -7,7 +7,7 @@ const { after, before, describe, it } = require('node:test'); const assert = require('node:assert'); const fs = require('node:fs'); const semver = require('semver'); -const libvips = require('../../lib/libvips'); +const libvips = require('../../dist/libvips.cjs'); const originalPlatform = process.platform; diff --git a/test/unit/text.js b/test/unit/text.js index 029b45f1f..4c36e8f20 100644 --- a/test/unit/text.js +++ b/test/unit/text.js @@ -8,7 +8,7 @@ const assert = require('node:assert'); const sharp = require('../../'); const fixtures = require('../fixtures'); -const { inRange } = require('../../lib/is'); +const { inRange } = require('../../dist/is.cjs'); describe('Text to image', () => { it('text with default values', async (t) => { diff --git a/test/unit/trim.js b/test/unit/trim.js index 99b14a8d9..fd193b1ec 100644 --- a/test/unit/trim.js +++ b/test/unit/trim.js @@ -7,7 +7,7 @@ const { describe, it } = require('node:test'); const assert = require('node:assert'); const sharp = require('../../'); -const inRange = require('../../lib/is').inRange; +const inRange = require('../../dist/is.cjs').inRange; const fixtures = require('../fixtures'); describe('Trim borders', () => { From f7f636992be1667b51746a93bb1fffa662ea2df6 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Sun, 5 Apr 2026 11:04:08 +0200 Subject: [PATCH 07/15] better build script --- package.json | 1 - scripts/build.mjs | 71 ++++++++++++++++++++--------------------------- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index cd2a3723c..b2097b652 100644 --- a/package.json +++ b/package.json @@ -189,7 +189,6 @@ "@img/sharp-libvips-win32-x64": "1.2.4", "@types/node": "*", "emnapi": "^1.7.0", - "esbuild": "0.27.4", "exif-reader": "^2.0.2", "extract-zip": "^2.0.1", "icc": "^3.0.0", diff --git a/scripts/build.mjs b/scripts/build.mjs index fdcd93cd0..501c6f4e8 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,46 +1,35 @@ // @ts-check import * as fs from "node:fs/promises"; -import * as esbuild from "esbuild"; +import { fileURLToPath } from "node:url"; -await fs.rm(new URL("../dist/", import.meta.url), { force: true, recursive: true }); +const distDir = new URL("../dist/", import.meta.url); -/** @satisfies {esbuild.BuildOptions} */ -const sharedOptions = { - minify: false, - platform: "node", - target: "node18", - sourcemap: false, - sourcesContent: false, - bundle: false, - entryPoints: ["./lib/*.mjs"], - outdir: "dist", -}; +await fs.rm(distDir, { force: true, recursive: true }); +await fs.mkdir(new URL("../dist/", import.meta.url), { recursive: true }); -await esbuild.build({ - ...sharedOptions, - format: "esm", - outExtension: { ".js": ".mjs" }, -}); -await esbuild.build({ - ...sharedOptions, - format: "cjs", - outExtension: { ".js": ".cjs" }, - plugins: [ - { - name: "cjs-compat", - setup(build) { - build.onLoad({ filter: /\.mjs$/ }, async (args) => { - const contents = await fs.readFile(args.path, "utf-8"); - return { - contents: contents - // ESM TLA to CJS require() is not supported ootb - // https://github.com/evanw/esbuild/issues/253 - .replaceAll("await import(", "require(") - .replaceAll("import.meta.url", "require('node:url').pathToFileURL(__filename)"), - loader: "js", - }; - }); - }, - }, - ], -}); +const libDir = new URL("../lib/", import.meta.url); + +/** + * @param {string} input + * @returns {string} + */ +function cjsToEsm(input) { + return input + // Turn import into require() and .mjs into .cjs + .replace( + /import\s+(.+)\s+from\s+('[^']+'|"[^"]+");?/g, + (_, bindings, path) => `const ${bindings} = require(${path.replace(".mjs", ".cjs")});`, + ) + .replace(/import\s+('[^']+'|"[^"]+");?/g, (_, path) => `require(${path.replace(".mjs", ".cjs")});`) + // Transforms TLA + .replaceAll("await import(", "require(") + .replaceAll("export default", "module.exports =") + // import.meta.url doesn't exist in cjs + .replaceAll("import.meta.url", "require('node:url').pathToFileURL(__filename)"); +} + +for await (const entry of fs.glob("**/*.mjs", { cwd: fileURLToPath(libDir) })) { + await fs.cp(new URL(entry, libDir), new URL(entry, distDir)); + const contents = await fs.readFile(new URL(entry, libDir), "utf-8"); + await fs.writeFile(new URL(entry.replace(".mjs", ".cjs"), distDir), cjsToEsm(contents)); +} From e80d58f10554e679f1855b2ec0da557585da1515 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Sun, 5 Apr 2026 11:06:55 +0200 Subject: [PATCH 08/15] fix: build --- npm/from-local-build.js | 2 +- src/binding.gyp | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/npm/from-local-build.js b/npm/from-local-build.js index fc35bd2ed..d1f0f76c9 100644 --- a/npm/from-local-build.js +++ b/npm/from-local-build.js @@ -8,7 +8,7 @@ const { copyFileSync, cpSync, readFileSync, writeFileSync, appendFileSync } = require('node:fs'); const { basename, join } = require('node:path'); -const { buildPlatformArch } = require('../lib/libvips'); +const { buildPlatformArch } = require('../dist/libvips.cjs'); const licensing = ` ## Licensing diff --git a/src/binding.gyp b/src/binding.gyp index 2040cde5b..7b4c34a33 100644 --- a/src/binding.gyp +++ b/src/binding.gyp @@ -3,13 +3,13 @@ { 'variables': { - 'vips_version': ' Date: Sun, 5 Apr 2026 11:12:22 +0200 Subject: [PATCH 09/15] fix: script --- {scripts => install}/build.mjs | 0 package.json | 3 +-- 2 files changed, 1 insertion(+), 2 deletions(-) rename {scripts => install}/build.mjs (100%) diff --git a/scripts/build.mjs b/install/build.mjs similarity index 100% rename from scripts/build.mjs rename to install/build.mjs diff --git a/package.json b/package.json index b2097b652..20b3f21b2 100644 --- a/package.json +++ b/package.json @@ -92,8 +92,7 @@ "Don Denton " ], "scripts": { - "build": "node scripts/build.mjs && node install/build.js", - "build:test": "node build.mjs", + "build": "node install/build.mjs && node install/build.js", "install": "node install/check.js || npm run build", "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*", "test": "npm run lint && npm run test-unit", From eaf7cef47de6b0acb2f914a7cdb58fc717c88313 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Sun, 5 Apr 2026 11:18:31 +0200 Subject: [PATCH 10/15] fix: script --- .github/CONTRIBUTING.md | 2 +- .github/workflows/ci.yml | 7 ++++--- package.json | 3 ++- {install => scripts}/build.mjs | 0 test/bench/Dockerfile | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) rename {install => scripts}/build.mjs (100%) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index b65d48b98..72ffc3793 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -25,7 +25,7 @@ Please select the `main` branch as the destination for your Pull Request so your Please squash your changes into a single commit using a command like `git rebase -i upstream/main`. -To test C++ changes, you can compile the module using `npm run build` and then run the tests using `npm test`. +To test C++ changes, you can compile the module using `npm run build:dist && npm run build` and then run the tests using `npm test`. ## Submit a Pull Request with a new feature diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd6838590..6d45418c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,7 +170,7 @@ jobs: architecture: ${{ matrix.nodejs_arch }} - uses: actions/checkout@v4 - run: npm install - - run: npm run build + - run: npm run build:dist && npm run build - run: npm run test-unit - if: matrix.package run: npm run package-from-local-build @@ -213,7 +213,7 @@ jobs: run: apk add build-base git python3 font-noto --update-cache - uses: actions/checkout@v4 - run: npm install - - run: npm run build + - run: npm run build:dist && npm run build - run: npm run test-unit - if: matrix.package run: npm run package-from-local-build @@ -276,6 +276,7 @@ jobs: curl --silent https://${{ matrix.nodejs_hostname }}/download/release/v${{ matrix.nodejs_version}}/node-v${{ matrix.nodejs_version}}-linux-${{ matrix.nodejs_arch }}.tar.xz | tar xJC /opt/nodejs --strip-components=1 export PATH=$PATH:/opt/nodejs/bin npm install + npm run build:dist npm run build node --test test/unit/io.js npm run package-from-local-build @@ -301,7 +302,7 @@ jobs: with: node-version: "20" - run: npm install - - run: emmake npm run build + - run: npm run build:dist && emmake npm run build - name: Verify emscripten versions match run: | EMSCRIPTEN_VERSION_LIBVIPS=$(node -p "require('@img/sharp-libvips-dev-wasm32/versions').emscripten") diff --git a/package.json b/package.json index 20b3f21b2..74ff4cad7 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,8 @@ "Don Denton " ], "scripts": { - "build": "node install/build.mjs && node install/build.js", + "build": "node install/build.js", + "build:dist": "node scripts/build.mjs", "install": "node install/check.js || npm run build", "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*", "test": "npm run lint && npm run test-unit", diff --git a/install/build.mjs b/scripts/build.mjs similarity index 100% rename from install/build.mjs rename to scripts/build.mjs diff --git a/test/bench/Dockerfile b/test/bench/Dockerfile index 290db857a..b9d86d7a0 100644 --- a/test/bench/Dockerfile +++ b/test/bench/Dockerfile @@ -15,7 +15,7 @@ RUN apt-get install -y imagemagick libmagick++-dev graphicsmagick # Install sharp RUN mkdir /tmp/sharp RUN cd /tmp && git clone --single-branch --branch $BRANCH https://github.com/lovell/sharp.git -RUN cd /tmp/sharp && npm install && npm run build +RUN cd /tmp/sharp && npm install && npm run build:dist && npm run build # Install benchmark test RUN cd /tmp/sharp/test/bench && npm install --omit optional From 046e17d047421c16dc83dc04c1e03a1910f16600 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 6 Apr 2026 11:22:13 +0200 Subject: [PATCH 11/15] fix --- biome.json | 2 +- scripts/build.mjs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/biome.json b/biome.json index 7946049c8..b2ba9d0e3 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/2.3.4/schema.json", + "$schema": "./node_modules/@biomejs/biome/configuration_schema.json", "vcs": { "enabled": true, "clientKind": "git", diff --git a/scripts/build.mjs b/scripts/build.mjs index 501c6f4e8..eda355d64 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,6 +1,5 @@ // @ts-check import * as fs from "node:fs/promises"; -import { fileURLToPath } from "node:url"; const distDir = new URL("../dist/", import.meta.url); @@ -28,7 +27,9 @@ function cjsToEsm(input) { .replaceAll("import.meta.url", "require('node:url').pathToFileURL(__filename)"); } -for await (const entry of fs.glob("**/*.mjs", { cwd: fileURLToPath(libDir) })) { +const entries = (await fs.readdir(libDir, { recursive: true })).filter(e => e.endsWith('.mjs')); + +for (const entry of entries) { await fs.cp(new URL(entry, libDir), new URL(entry, distDir)); const contents = await fs.readFile(new URL(entry, libDir), "utf-8"); await fs.writeFile(new URL(entry.replace(".mjs", ".cjs"), distDir), cjsToEsm(contents)); From ab1640aa553ae33a4aaaacb81ecf587558c5e0c2 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 13 Apr 2026 15:13:32 +0200 Subject: [PATCH 12/15] fix --- docs/public/humans.txt | 3 +++ docs/src/content/docs/changelog/v0.35.0.md | 5 +++++ lib/libvips.mjs | 13 +++++-------- lib/utility.mjs | 5 ++--- npm/freebsd-wasm32/export/package.mjs | 1 + npm/freebsd-wasm32/export/sharp.node.mjs | 1 + npm/freebsd-wasm32/export/versions.mjs | 1 + npm/freebsd-wasm32/package.json | 17 +++++++++++++---- npm/webcontainers-wasm32/export/package.mjs | 1 + npm/webcontainers-wasm32/export/sharp.node.mjs | 1 + npm/webcontainers-wasm32/export/versions.mjs | 1 + npm/webcontainers-wasm32/package.json | 17 +++++++++++++---- package.json | 3 ++- scripts/build.mjs | 6 ++---- src/binding.gyp | 4 ++-- test/unit/io.js | 2 +- 16 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 npm/freebsd-wasm32/export/package.mjs create mode 100644 npm/freebsd-wasm32/export/sharp.node.mjs create mode 100644 npm/freebsd-wasm32/export/versions.mjs create mode 100644 npm/webcontainers-wasm32/export/package.mjs create mode 100644 npm/webcontainers-wasm32/export/sharp.node.mjs create mode 100644 npm/webcontainers-wasm32/export/versions.mjs diff --git a/docs/public/humans.txt b/docs/public/humans.txt index 00efa0d71..7d0d31294 100644 --- a/docs/public/humans.txt +++ b/docs/public/humans.txt @@ -329,3 +329,6 @@ GitHub: https://github.com/arcanis Name: Dmytro Tiapukhin GitHub: https://github.com/eddienubes + +Name: Florian Lefebvre +GitHub: https://github.com/florian-lefebvre diff --git a/docs/src/content/docs/changelog/v0.35.0.md b/docs/src/content/docs/changelog/v0.35.0.md index b0d945065..7fda31feb 100644 --- a/docs/src/content/docs/changelog/v0.35.0.md +++ b/docs/src/content/docs/changelog/v0.35.0.md @@ -60,3 +60,8 @@ slug: changelog/v0.35.0 [#4504](https://github.com/lovell/sharp/issues/4504) * Add WebP `exact` option for control over transparent pixel colour values. + +* Adds support for ESM + [#4509](https://github.com/lovell/sharp/pull/4509) + [@florian-lefebvre](https://github.com/florian-lefebvre) + diff --git a/lib/libvips.mjs b/lib/libvips.mjs index dce8b40dd..ddd6aa3a3 100644 --- a/lib/libvips.mjs +++ b/lib/libvips.mjs @@ -5,17 +5,14 @@ import { spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; -import fs from 'node:fs'; import semverCoerce from 'semver/functions/coerce'; import semverGreaterThanOrEqualTo from 'semver/functions/gte'; import semverSatisfies from 'semver/functions/satisfies'; import detectLibc from 'detect-libc'; - -// Cannot use an import because sometimes it can need the attr or with keyword -const { config, engines, optionalDependencies } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8')); +import pkg from '../package.json' with { type: 'json' }; /* node:coverage ignore next */ -const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || config.libvips; +const minimumLibvipsVersionLabelled = process.env.npm_package_config_libvips || pkg.config.libvips; const minimumLibvipsVersion = semverCoerce(minimumLibvipsVersionLabelled).version; const prebuiltPlatforms = [ @@ -85,8 +82,8 @@ const buildSharpLibvipsLibDir = () => { const isUnsupportedNodeRuntime = () => { if (process.release?.name === 'node' && process.versions) { - if (!semverSatisfies(process.versions.node, engines.node)) { - return { found: process.versions.node, expected: engines.node }; + if (!semverSatisfies(process.versions.node, pkg.engines.node)) { + return { found: process.versions.node, expected: pkg.engines.node }; } } }; @@ -111,7 +108,7 @@ const sha512 = (s) => createHash('sha512').update(s).digest('hex'); const yarnLocator = () => { try { const identHash = sha512(`imgsharp-libvips-${buildPlatformArch()}`); - const npmVersion = semverCoerce(optionalDependencies[`@img/sharp-libvips-${buildPlatformArch()}`], { + const npmVersion = semverCoerce(pkg.optionalDependencies[`@img/sharp-libvips-${buildPlatformArch()}`], { includePrerelease: true }).version; return sha512(`${identHash}npm:${npmVersion}`).slice(0, 10); diff --git a/lib/utility.mjs b/lib/utility.mjs index 8764b2546..0645f1db5 100644 --- a/lib/utility.mjs +++ b/lib/utility.mjs @@ -4,12 +4,12 @@ */ import events from 'node:events'; -import fs from 'node:fs'; import detectLibc from 'detect-libc'; import is from './is.mjs'; import { runtimePlatformArch } from './libvips.mjs'; import sharp from './sharp.mjs'; +import pkg from "../package.json" with { type: "json" }; const runtimePlatform = runtimePlatformArch(); const libvipsVersion = sharp.libvipsVersion(); @@ -74,8 +74,7 @@ if (!libvipsVersion.isGlobal) { } catch (_) {} } } -// Cannot use an import because sometimes it can need the attr or with keyword -versions.sharp = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8')).version; +versions.sharp = pkg.version; /* node:coverage ignore next 5 */ if (versions.heif && format.heif) { diff --git a/npm/freebsd-wasm32/export/package.mjs b/npm/freebsd-wasm32/export/package.mjs new file mode 100644 index 000000000..d561aa57f --- /dev/null +++ b/npm/freebsd-wasm32/export/package.mjs @@ -0,0 +1 @@ +export { default } from '@img/sharp-wasm32/package.json' with { type: 'json' }; diff --git a/npm/freebsd-wasm32/export/sharp.node.mjs b/npm/freebsd-wasm32/export/sharp.node.mjs new file mode 100644 index 000000000..006986282 --- /dev/null +++ b/npm/freebsd-wasm32/export/sharp.node.mjs @@ -0,0 +1 @@ +export { default } from "@img/sharp-wasm32/sharp.node"; diff --git a/npm/freebsd-wasm32/export/versions.mjs b/npm/freebsd-wasm32/export/versions.mjs new file mode 100644 index 000000000..b53d54669 --- /dev/null +++ b/npm/freebsd-wasm32/export/versions.mjs @@ -0,0 +1 @@ +export { default } from "@img/sharp-wasm32/versions.json" with { type: "json" }; diff --git a/npm/freebsd-wasm32/package.json b/npm/freebsd-wasm32/package.json index 24f4d7a14..f38384428 100644 --- a/npm/freebsd-wasm32/package.json +++ b/npm/freebsd-wasm32/package.json @@ -21,9 +21,18 @@ }, "type": "commonjs", "exports": { - "./sharp.node": "./export/sharp.node.cjs", - "./package": "./export/package.cjs", - "./versions": "./export/versions.cjs" + "./sharp.node": { + "import": "./export/sharp.node.mjs", + "require": "./export/sharp.node.cjs" + }, + "./package": { + "import": "./export/package.mjs", + "require": "./export/package.cjs" + }, + "./versions": { + "import": "./export/versions.mjs", + "require": "./export/versions.cjs" + } }, "dependencies": { "@img/sharp-wasm32": "0.35.0-rc.2" @@ -34,4 +43,4 @@ "os": [ "freebsd" ] -} +} \ No newline at end of file diff --git a/npm/webcontainers-wasm32/export/package.mjs b/npm/webcontainers-wasm32/export/package.mjs new file mode 100644 index 000000000..d561aa57f --- /dev/null +++ b/npm/webcontainers-wasm32/export/package.mjs @@ -0,0 +1 @@ +export { default } from '@img/sharp-wasm32/package.json' with { type: 'json' }; diff --git a/npm/webcontainers-wasm32/export/sharp.node.mjs b/npm/webcontainers-wasm32/export/sharp.node.mjs new file mode 100644 index 000000000..006986282 --- /dev/null +++ b/npm/webcontainers-wasm32/export/sharp.node.mjs @@ -0,0 +1 @@ +export { default } from "@img/sharp-wasm32/sharp.node"; diff --git a/npm/webcontainers-wasm32/export/versions.mjs b/npm/webcontainers-wasm32/export/versions.mjs new file mode 100644 index 000000000..b53d54669 --- /dev/null +++ b/npm/webcontainers-wasm32/export/versions.mjs @@ -0,0 +1 @@ +export { default } from "@img/sharp-wasm32/versions.json" with { type: "json" }; diff --git a/npm/webcontainers-wasm32/package.json b/npm/webcontainers-wasm32/package.json index 7ad609906..61fa63c39 100644 --- a/npm/webcontainers-wasm32/package.json +++ b/npm/webcontainers-wasm32/package.json @@ -21,9 +21,18 @@ }, "type": "commonjs", "exports": { - "./sharp.node": "./export/sharp.node.cjs", - "./package": "./export/package.cjs", - "./versions": "./export/versions.cjs" + "./sharp.node": { + "import": "./export/sharp.node.mjs", + "require": "./export/sharp.node.cjs" + }, + "./package": { + "import": "./export/package.mjs", + "require": "./export/package.cjs" + }, + "./versions": { + "import": "./export/versions.mjs", + "require": "./export/versions.cjs" + } }, "dependencies": { "@img/sharp-wasm32": "0.35.0-rc.2" @@ -34,4 +43,4 @@ "cpu": [ "wasm32" ] -} +} \ No newline at end of file diff --git a/package.json b/package.json index 2f6f02e4b..56f50f3b3 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,8 @@ "Dennis Beatty ", "Ingvar Stepanyan ", "Don Denton ", - "Dmytro Tiapukhin " + "Dmytro Tiapukhin ", + "Florian Lefebvre " ], "scripts": { "build": "node install/build.js", diff --git a/scripts/build.mjs b/scripts/build.mjs index eda355d64..01845ad9f 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -16,15 +16,13 @@ function cjsToEsm(input) { return input // Turn import into require() and .mjs into .cjs .replace( - /import\s+(.+)\s+from\s+('[^']+'|"[^"]+");?/g, + /import\s+(.+)\s+from\s+('[^']+'|"[^"]+")(?:\s+with\s+\{[^}]*\})?;?/g, (_, bindings, path) => `const ${bindings} = require(${path.replace(".mjs", ".cjs")});`, ) - .replace(/import\s+('[^']+'|"[^"]+");?/g, (_, path) => `require(${path.replace(".mjs", ".cjs")});`) + .replace(/import\s+('[^']+'|"[^"]+")(?:\s+with\s+\{[^}]*\})?;?/g, (_, path) => `require(${path.replace(".mjs", ".cjs")});`) // Transforms TLA .replaceAll("await import(", "require(") .replaceAll("export default", "module.exports =") - // import.meta.url doesn't exist in cjs - .replaceAll("import.meta.url", "require('node:url').pathToFileURL(__filename)"); } const entries = (await fs.readdir(libDir, { recursive: true })).filter(e => e.endsWith('.mjs')); diff --git a/src/binding.gyp b/src/binding.gyp index 2a5d7b6c3..639943404 100644 --- a/src/binding.gyp +++ b/src/binding.gyp @@ -3,8 +3,8 @@ { 'variables': { - 'vips_version': ' Date: Mon, 13 Apr 2026 15:14:29 +0200 Subject: [PATCH 13/15] fix: typo --- docs/src/content/docs/changelog/v0.35.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/content/docs/changelog/v0.35.0.md b/docs/src/content/docs/changelog/v0.35.0.md index 7fda31feb..a420597b1 100644 --- a/docs/src/content/docs/changelog/v0.35.0.md +++ b/docs/src/content/docs/changelog/v0.35.0.md @@ -61,7 +61,7 @@ slug: changelog/v0.35.0 * Add WebP `exact` option for control over transparent pixel colour values. -* Adds support for ESM +* Add support for ESM [#4509](https://github.com/lovell/sharp/pull/4509) [@florian-lefebvre](https://github.com/florian-lefebvre) From 2c23183f06ca29aaa3062e6adec76afded5c5081 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 13 Apr 2026 15:16:51 +0200 Subject: [PATCH 14/15] fix --- lib/sharp.mjs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/sharp.mjs b/lib/sharp.mjs index ff244848f..a8d3c5873 100644 --- a/lib/sharp.mjs +++ b/lib/sharp.mjs @@ -12,41 +12,41 @@ const runtimePlatform = runtimePlatformArch(); /* node:coverage disable */ -const prebuiltBinaryForRuntime = () => { +const prebuiltBinaryForRuntime = async () => { switch (runtimePlatform) { case 'darwin-arm64': - return import('@img/sharp-darwin-arm64/sharp.node'); + return await import('@img/sharp-darwin-arm64/sharp.node'); case 'darwin-x64': - return import('@img/sharp-darwin-x64/sharp.node'); + return await import('@img/sharp-darwin-x64/sharp.node'); case 'linux-arm': - return import('@img/sharp-linux-arm/sharp.node'); + return await import('@img/sharp-linux-arm/sharp.node'); case 'linux-arm64': - return import('@img/sharp-linux-arm64/sharp.node'); + return await import('@img/sharp-linux-arm64/sharp.node'); case 'linux-ppc64': - return import('@img/sharp-linux-ppc64/sharp.node'); + return await import('@img/sharp-linux-ppc64/sharp.node'); case 'linux-riscv64': - return import('@img/sharp-linux-riscv64/sharp.node'); + return await import('@img/sharp-linux-riscv64/sharp.node'); case 'linux-s390x': - return import('@img/sharp-linux-s390x/sharp.node'); + return await import('@img/sharp-linux-s390x/sharp.node'); case 'linux-x64': - return import('@img/sharp-linux-x64/sharp.node'); + return await import('@img/sharp-linux-x64/sharp.node'); case 'linuxmusl-arm64': - return import('@img/sharp-linuxmusl-arm64/sharp.node') ; + return await import('@img/sharp-linuxmusl-arm64/sharp.node') ; case 'linuxmusl-x64': - return import('@img/sharp-linuxmusl-x64/sharp.node'); + return await import('@img/sharp-linuxmusl-x64/sharp.node'); case 'win32-arm64': - return import('@img/sharp-win32-arm64/sharp.node'); + return await import('@img/sharp-win32-arm64/sharp.node'); case 'win32-ia32': - return import('@img/sharp-win32-ia32/sharp.node'); + return await import('@img/sharp-win32-ia32/sharp.node'); case 'win32-x64': - return import('@img/sharp-win32-x64/sharp.node'); + return await import('@img/sharp-win32-x64/sharp.node'); case 'freebsd-arm64': case 'freebsd-x64': - return import('@img/sharp-freebsd-wasm32/sharp.node'); + return await import('@img/sharp-freebsd-wasm32/sharp.node'); case 'linux-wasm32': - return import('@img/sharp-webcontainers-wasm32/sharp.node'); + return await import('@img/sharp-webcontainers-wasm32/sharp.node'); default: - return import('@img/sharp-wasm32/sharp.node'); + return await import('@img/sharp-wasm32/sharp.node'); } }; From f6128524a7261446b38a27dffe4e76914098b804 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 13 Apr 2026 15:20:56 +0200 Subject: [PATCH 15/15] fix --- lib/sharp.mjs | 177 ++++++++++++++++++++++++-------------------------- 1 file changed, 86 insertions(+), 91 deletions(-) diff --git a/lib/sharp.mjs b/lib/sharp.mjs index a8d3c5873..3276a1d4e 100644 --- a/lib/sharp.mjs +++ b/lib/sharp.mjs @@ -5,51 +5,13 @@ // Inspects the runtime environment and exports the relevant sharp.node binary -import { familySync, versionSync } from 'detect-libc'; +import { familySync, versionSync } from "detect-libc"; -import { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } from './libvips.mjs'; +import { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } from "./libvips.mjs"; const runtimePlatform = runtimePlatformArch(); /* node:coverage disable */ -const prebuiltBinaryForRuntime = async () => { - switch (runtimePlatform) { - case 'darwin-arm64': - return await import('@img/sharp-darwin-arm64/sharp.node'); - case 'darwin-x64': - return await import('@img/sharp-darwin-x64/sharp.node'); - case 'linux-arm': - return await import('@img/sharp-linux-arm/sharp.node'); - case 'linux-arm64': - return await import('@img/sharp-linux-arm64/sharp.node'); - case 'linux-ppc64': - return await import('@img/sharp-linux-ppc64/sharp.node'); - case 'linux-riscv64': - return await import('@img/sharp-linux-riscv64/sharp.node'); - case 'linux-s390x': - return await import('@img/sharp-linux-s390x/sharp.node'); - case 'linux-x64': - return await import('@img/sharp-linux-x64/sharp.node'); - case 'linuxmusl-arm64': - return await import('@img/sharp-linuxmusl-arm64/sharp.node') ; - case 'linuxmusl-x64': - return await import('@img/sharp-linuxmusl-x64/sharp.node'); - case 'win32-arm64': - return await import('@img/sharp-win32-arm64/sharp.node'); - case 'win32-ia32': - return await import('@img/sharp-win32-ia32/sharp.node'); - case 'win32-x64': - return await import('@img/sharp-win32-x64/sharp.node'); - case 'freebsd-arm64': - case 'freebsd-x64': - return await import('@img/sharp-freebsd-wasm32/sharp.node'); - case 'linux-wasm32': - return await import('@img/sharp-webcontainers-wasm32/sharp.node'); - default: - return await import('@img/sharp-wasm32/sharp.node'); - } -}; - let sharp; const errors = []; try { @@ -66,10 +28,60 @@ if (!sharp) { } if (!sharp) { try { - sharp = await prebuiltBinaryForRuntime(); - if (['linux-x64', 'linuxmusl-x64'].includes(runtimePlatform) && !sharp._isUsingX64V2()) { - const err = new Error('Prebuilt binaries for Linux x64 require v2 microarchitecture'); - err.code = 'Unsupported CPU'; + switch (runtimePlatform) { + case "darwin-arm64": + sharp = await import("@img/sharp-darwin-arm64/sharp.node"); + break; + case "darwin-x64": + sharp = await import("@img/sharp-darwin-x64/sharp.node"); + break; + case "linux-arm": + sharp = await import("@img/sharp-linux-arm/sharp.node"); + break; + case "linux-arm64": + sharp = await import("@img/sharp-linux-arm64/sharp.node"); + break; + case "linux-ppc64": + sharp = await import("@img/sharp-linux-ppc64/sharp.node"); + break; + case "linux-riscv64": + sharp = await import("@img/sharp-linux-riscv64/sharp.node"); + break; + case "linux-s390x": + sharp = await import("@img/sharp-linux-s390x/sharp.node"); + break; + case "linux-x64": + sharp = await import("@img/sharp-linux-x64/sharp.node"); + break; + case "linuxmusl-arm64": + sharp = await import("@img/sharp-linuxmusl-arm64/sharp.node"); + break; + case "linuxmusl-x64": + sharp = await import("@img/sharp-linuxmusl-x64/sharp.node"); + break; + case "win32-arm64": + sharp = await import("@img/sharp-win32-arm64/sharp.node"); + break; + case "win32-ia32": + sharp = await import("@img/sharp-win32-ia32/sharp.node"); + break; + case "win32-x64": + sharp = await import("@img/sharp-win32-x64/sharp.node"); + break; + case "freebsd-arm64": + case "freebsd-x64": + sharp = await import("@img/sharp-freebsd-wasm32/sharp.node"); + break; + case "linux-wasm32": + sharp = await import("@img/sharp-webcontainers-wasm32/sharp.node"); + break; + default: + sharp = await import("@img/sharp-wasm32/sharp.node"); + break; + } + if (["linux-x64", "linuxmusl-x64"].includes(runtimePlatform) && !sharp._isUsingX64V2()) { + const err = new Error("Prebuilt binaries for Linux x64 require v2 microarchitecture"); + err.code = "Unsupported CPU"; errors.push(err); sharp = null; } @@ -79,84 +91,67 @@ if (!sharp) { } if (!sharp) { - const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os)); + const [isLinux, isMacOs, isWindows] = ["linux", "darwin", "win32"].map((os) => runtimePlatform.startsWith(os)); const help = [`Could not load the "sharp" module using the ${runtimePlatform} runtime`]; - errors.forEach(err => { - if (err.code !== 'MODULE_NOT_FOUND') { + errors.forEach((err) => { + if (err.code !== "MODULE_NOT_FOUND") { help.push(`${err.code}: ${err.message}`); } }); - const messages = errors.map(err => err.message).join(' '); - help.push('Possible solutions:'); + const messages = errors.map((err) => err.message).join(" "); + help.push("Possible solutions:"); // Common error messages if (isUnsupportedNodeRuntime()) { const { found, expected } = isUnsupportedNodeRuntime(); - help.push( - '- Please upgrade Node.js:', - ` Found ${found}`, - ` Requires ${expected}` - ); + help.push("- Please upgrade Node.js:", ` Found ${found}`, ` Requires ${expected}`); } else if (prebuiltPlatforms.includes(runtimePlatform)) { - const [os, cpu] = runtimePlatform.split('-'); - const libc = os.endsWith('musl') ? ' --libc=musl' : ''; + const [os, cpu] = runtimePlatform.split("-"); + const libc = os.endsWith("musl") ? " --libc=musl" : ""; help.push( - '- Ensure optional dependencies can be installed:', - ' npm install --include=optional sharp', - '- Ensure your package manager supports multi-platform installation:', - ' See https://sharp.pixelplumbing.com/install#cross-platform', - '- Add platform-specific dependencies:', - ` npm install --os=${os.replace('musl', '')}${libc} --cpu=${cpu} sharp` + "- Ensure optional dependencies can be installed:", + " npm install --include=optional sharp", + "- Ensure your package manager supports multi-platform installation:", + " See https://sharp.pixelplumbing.com/install#cross-platform", + "- Add platform-specific dependencies:", + ` npm install --os=${os.replace("musl", "")}${libc} --cpu=${cpu} sharp`, ); } else { help.push( `- Manually install libvips >= ${minimumLibvipsVersion}`, - ' See https://sharp.pixelplumbing.com/install#building-from-source', - '- Add WebAssembly-based dependencies:', - ' npm install sharp @img/sharp-wasm32' + " See https://sharp.pixelplumbing.com/install#building-from-source", + "- Add WebAssembly-based dependencies:", + " npm install sharp @img/sharp-wasm32", ); } if (isLinux && /(symbol not found|CXXABI_)/i.test(messages)) { try { const { config } = await import(`@img/sharp-libvips-${runtimePlatform}/package`); const libcFound = `${familySync()} ${versionSync()}`; - const libcRequires = `${config.musl ? 'musl' : 'glibc'} ${config.musl || config.glibc}`; - help.push( - '- Update your OS:', - ` Found ${libcFound}`, - ` Requires ${libcRequires}` - ); + const libcRequires = `${config.musl ? "musl" : "glibc"} ${config.musl || config.glibc}`; + help.push("- Update your OS:", ` Found ${libcFound}`, ` Requires ${libcRequires}`); } catch (_errEngines) {} } if (isLinux && /\/snap\/core[0-9]{2}/.test(messages)) { - help.push( - '- Remove the Node.js Snap, which does not support native modules', - ' snap remove node' - ); + help.push("- Remove the Node.js Snap, which does not support native modules", " snap remove node"); } if (isMacOs && /Incompatible library version/.test(messages)) { - help.push( - '- Update Homebrew:', - ' brew update && brew upgrade vips' - ); + help.push("- Update Homebrew:", " brew update && brew upgrade vips"); } - if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) { - help.push('- Run Node.js without using the --no-addons flag'); + if (errors.some((err) => err.code === "ERR_DLOPEN_DISABLED")) { + help.push("- Run Node.js without using the --no-addons flag"); } // Link to installation docs if (isWindows && /The specified procedure could not be found/.test(messages)) { help.push( - '- Using the canvas package on Windows?', - ' See https://sharp.pixelplumbing.com/install#canvas-and-windows', - '- Check for outdated versions of sharp in the dependency tree:', - ' npm ls sharp' + "- Using the canvas package on Windows?", + " See https://sharp.pixelplumbing.com/install#canvas-and-windows", + "- Check for outdated versions of sharp in the dependency tree:", + " npm ls sharp", ); } - help.push( - '- Consult the installation documentation:', - ' See https://sharp.pixelplumbing.com/install' - ); - throw new Error(help.join('\n')); + help.push("- Consult the installation documentation:", " See https://sharp.pixelplumbing.com/install"); + throw new Error(help.join("\n")); } -export default sharp \ No newline at end of file +export default sharp;