From b0ce76d22fb1b436f7d6b16a40dd5ee464b66779 Mon Sep 17 00:00:00 2001 From: timint Date: Mon, 30 Dec 2024 05:33:25 +0100 Subject: [PATCH] Fix issue #87 - Error: unexpected top-level await with Node 23 ESM --- src/index.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 49eae2a..4034af3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,35 +1,28 @@ import { YError, printStackTrace } from 'yerror'; import debug from 'debug'; import { env } from 'node:process'; +import bindings from 'bindings' +import ttf2woff2Loader from '../jssrc/index.js'; const doDebug = debug('ttf2woff2'); -let ttf2woff2: undefined | ((input: Buffer) => Buffer) = undefined; +let ttf2woff2: undefined; -if ( - !env.TTF2WOFF2_VERSION || - env.TTF2WOFF2_VERSION?.toLowerCase() === 'native' -) { +if (!env.TTF2WOFF2_VERSION || env.TTF2WOFF2_VERSION?.toLowerCase() === 'native') { try { - ttf2woff2 = (await import('bindings')).default('addon.node').convert; + ttf2woff2 = bindings.default('addon.node').convert; doDebug('✅ Using native version.'); } catch (err) { - doDebug( - '❌ Could not load the native version.', - printStackTrace(err as Error), - ); + doDebug('❌ Could not load the native version.', printStackTrace(err as Error)); } } if (!env.TTF2WOFF2_VERSION || env.TTF2WOFF2_VERSION?.toLowerCase() === 'wasm') { if (!ttf2woff2) { try { - ttf2woff2 = (await import('../jssrc/index.js')).default; + ttf2woff2 = ttf2woff2Loader; doDebug('✅ Using WASM version.'); } catch (err) { - doDebug( - '❌ Could not load the WASM version.', - printStackTrace(err as Error), - ); + doDebug('❌ Could not load the WASM version.', printStackTrace(err as Error)); } } }