From 6e1a7ec446922e1c09e9568d1f1dce1579193a5a Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Wed, 17 Jul 2024 19:07:13 -0400 Subject: [PATCH] import meta to a function (#764) * import meta to a function * added comment and issue --- packages/api/src/web-features.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/api/src/web-features.ts b/packages/api/src/web-features.ts index 444176b4c..5eebe5269 100644 --- a/packages/api/src/web-features.ts +++ b/packages/api/src/web-features.ts @@ -13,6 +13,17 @@ const didUrlRegex = /^https?:\/\/dweb\/([^/]+)\/?(.*)?$/; const httpToHttpsRegex = /^http:/; const trailingSlashRegex = /\/$/; +// This is in place to prevent our `bundler-bonanza` repo from failing for Node CJS builds +// Not sure if this is working as expected in all environments, crated an issue +// TODO: https://github.com/TBD54566975/web5-js/issues/767 +function importMetaIfSupported() { + try { + return new Function('return import.meta')(); + } catch(_error) { + return undefined; + } +} + async function getDwnEndpoints(did) { const { didDocument } = await DidResolver.resolve(did); let endpoints = didDocument?.service?.find(service => service.type === 'DecentralizedWebNode')?.serviceEndpoint; @@ -115,7 +126,7 @@ async function installWorker(options: any = {}): Promise { // You can only have one worker per path, so check to see if one is already registered if (!registration){ // @ts-ignore - const installUrl = options.path || (globalThis.document ? document?.currentScript?.src : import.meta?.url); + const installUrl = options.path || (globalThis.document ? document?.currentScript?.src : importMetaIfSupported()?.url); if (installUrl) navigator.serviceWorker.register(installUrl, { type: 'module' }).catch(error => { console.error('DWeb networking feature installation failed: ', error); });