55
66// Inspects the runtime environment and exports the relevant sharp.node binary
77
8+ import { createRequire } from "node:module"
89import { familySync , versionSync } from "detect-libc" ;
910
10- import { version } from '../package.json' ;
11- import { runtimePlatformArch , isUnsupportedNodeRuntime , prebuiltPlatforms , minimumLibvipsVersion } from "./libvips.mjs" ;
11+ import libvips from "./libvips.mjs" ;
12+ import pkg from '../package.json' with { type : 'json' } ;
13+
14+ const require = createRequire ( import . meta. url ) ;
15+ const { version } = pkg ;
16+
17+ const { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } = libvips ;
1218const runtimePlatform = runtimePlatformArch ( ) ;
1319
1420/* node:coverage disable */
1521
1622let sharp ;
1723const errors = [ ] ;
1824try {
19- sharp = await import ( `../src/build/Release/sharp-${ runtimePlatform } -${ version } .node` ) ;
25+ sharp = require ( `../src/build/Release/sharp-${ runtimePlatform } -${ version } .node` ) ;
2026} catch ( err ) {
2127 errors . push ( err ) ;
2228}
2329if ( ! sharp ) {
2430 try {
25- sharp = await import ( `../src/build/Release/sharp-wasm32-${ version } .node` ) ;
31+ sharp = require ( `../src/build/Release/sharp-wasm32-${ version } .node` ) ;
2632 } catch ( err ) {
2733 errors . push ( err ) ;
2834 }
@@ -31,53 +37,53 @@ if (!sharp) {
3137 try {
3238 switch ( runtimePlatform ) {
3339 case "darwin-arm64" :
34- sharp = await import ( "@img/sharp-darwin-arm64/sharp.node" ) ;
40+ sharp = require ( "@img/sharp-darwin-arm64/sharp.node" ) ;
3541 break ;
3642 case "darwin-x64" :
37- sharp = await import ( "@img/sharp-darwin-x64/sharp.node" ) ;
43+ sharp = require ( "@img/sharp-darwin-x64/sharp.node" ) ;
3844 break ;
3945 case "linux-arm" :
40- sharp = await import ( "@img/sharp-linux-arm/sharp.node" ) ;
46+ sharp = require ( "@img/sharp-linux-arm/sharp.node" ) ;
4147 break ;
4248 case "linux-arm64" :
43- sharp = await import ( "@img/sharp-linux-arm64/sharp.node" ) ;
49+ sharp = require ( "@img/sharp-linux-arm64/sharp.node" ) ;
4450 break ;
4551 case "linux-ppc64" :
46- sharp = await import ( "@img/sharp-linux-ppc64/sharp.node" ) ;
52+ sharp = require ( "@img/sharp-linux-ppc64/sharp.node" ) ;
4753 break ;
4854 case "linux-riscv64" :
49- sharp = await import ( "@img/sharp-linux-riscv64/sharp.node" ) ;
55+ sharp = require ( "@img/sharp-linux-riscv64/sharp.node" ) ;
5056 break ;
5157 case "linux-s390x" :
52- sharp = await import ( "@img/sharp-linux-s390x/sharp.node" ) ;
58+ sharp = require ( "@img/sharp-linux-s390x/sharp.node" ) ;
5359 break ;
5460 case "linux-x64" :
55- sharp = await import ( "@img/sharp-linux-x64/sharp.node" ) ;
61+ sharp = require ( "@img/sharp-linux-x64/sharp.node" ) ;
5662 break ;
5763 case "linuxmusl-arm64" :
58- sharp = await import ( "@img/sharp-linuxmusl-arm64/sharp.node" ) ;
64+ sharp = require ( "@img/sharp-linuxmusl-arm64/sharp.node" ) ;
5965 break ;
6066 case "linuxmusl-x64" :
61- sharp = await import ( "@img/sharp-linuxmusl-x64/sharp.node" ) ;
67+ sharp = require ( "@img/sharp-linuxmusl-x64/sharp.node" ) ;
6268 break ;
6369 case "win32-arm64" :
64- sharp = await import ( "@img/sharp-win32-arm64/sharp.node" ) ;
70+ sharp = require ( "@img/sharp-win32-arm64/sharp.node" ) ;
6571 break ;
6672 case "win32-ia32" :
67- sharp = await import ( "@img/sharp-win32-ia32/sharp.node" ) ;
73+ sharp = require ( "@img/sharp-win32-ia32/sharp.node" ) ;
6874 break ;
6975 case "win32-x64" :
70- sharp = await import ( "@img/sharp-win32-x64/sharp.node" ) ;
76+ sharp = require ( "@img/sharp-win32-x64/sharp.node" ) ;
7177 break ;
7278 case "freebsd-arm64" :
7379 case "freebsd-x64" :
74- sharp = await import ( "@img/sharp-freebsd-wasm32/sharp.node" ) ;
80+ sharp = require ( "@img/sharp-freebsd-wasm32/sharp.node" ) ;
7581 break ;
7682 case "linux-wasm32" :
77- sharp = await import ( "@img/sharp-webcontainers-wasm32/sharp.node" ) ;
83+ sharp = require ( "@img/sharp-webcontainers-wasm32/sharp.node" ) ;
7884 break ;
7985 default :
80- sharp = await import ( "@img/sharp-wasm32/sharp.node" ) ;
86+ sharp = require ( "@img/sharp-wasm32/sharp.node" ) ;
8187 break ;
8288 }
8389 if ( [ "linux-x64" , "linuxmusl-x64" ] . includes ( runtimePlatform ) && ! sharp . _isUsingX64V2 ( ) ) {
@@ -96,7 +102,7 @@ if (!sharp) {
96102
97103 const help = [ `Could not load the "sharp" module using the ${ runtimePlatform } runtime` ] ;
98104 errors . forEach ( ( err ) => {
99- if ( err . code !== "MODULE_NOT_FOUND" ) {
105+ if ( ! err . code . endsWith ( "MODULE_NOT_FOUND" ) ) {
100106 help . push ( `${ err . code } : ${ err . message } ` ) ;
101107 }
102108 } ) ;
@@ -127,7 +133,7 @@ if (!sharp) {
127133 }
128134 if ( isLinux && / ( s y m b o l n o t f o u n d | C X X A B I _ ) / i. test ( messages ) ) {
129135 try {
130- const { config } = await import ( `@img/sharp-libvips-${ runtimePlatform } /package` ) ;
136+ const { config } = require ( `@img/sharp-libvips-${ runtimePlatform } /package` ) ;
131137 const libcFound = `${ familySync ( ) } ${ versionSync ( ) } ` ;
132138 const libcRequires = `${ config . musl ? "musl" : "glibc" } ${ config . musl || config . glibc } ` ;
133139 help . push ( "- Update your OS:" , ` Found ${ libcFound } ` , ` Requires ${ libcRequires } ` ) ;
0 commit comments