From 70cbdcadc02faccfaeabe005198f340d3a92428e Mon Sep 17 00:00:00 2001 From: kavindadewmith Date: Wed, 29 Apr 2026 19:08:18 +0530 Subject: [PATCH 1/2] Enhance browser compatibility by exposing Buffer globally and update Vite configuration for pre-bundling external dependencies --- packages/browser/esbuild.config.mjs | 15 --------------- packages/browser/src/index.ts | 11 +++++++++++ packages/nuxt/src/module.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/browser/esbuild.config.mjs b/packages/browser/esbuild.config.mjs index 900af451d..59b272b09 100644 --- a/packages/browser/esbuild.config.mjs +++ b/packages/browser/esbuild.config.mjs @@ -46,14 +46,6 @@ const polyfillPlugin = { }; const commonOptions = { - banner: { - js: ` - import { Buffer } from 'buffer/index.js'; - if (typeof window !== 'undefined' && !window.Buffer) { - window.Buffer = Buffer; - } - `, - }, bundle: true, define: { global: 'globalThis', // Required by crypto-browserify @@ -63,13 +55,6 @@ const commonOptions = { }, entryPoints: ['src/index.ts'], external: externalDeps, - footer: { - js: ` - if (typeof window !== 'undefined' && !window.Buffer) { - window.Buffer = require('buffer/index.js').Buffer; - } - `, - }, platform: 'browser', plugins: [ polyfillPlugin, diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index 915425028..9f83a1fb7 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -19,6 +19,17 @@ /** * Entry point for all public APIs of this SDK. */ + +// Expose Buffer globally so that browser environments that check window.Buffer +// (e.g. libraries that guard against missing Node polyfills) can find it. +// `buffer` is bundled inline by esbuild — no external import appears in the dist. +// eslint-disable-next-line import/no-cycle +import {Buffer} from 'buffer'; + +if (typeof window !== 'undefined' && !(window as any).Buffer) { + (window as any).Buffer = Buffer; +} + // eslint-disable-next-line import/no-cycle export * from './__legacy__/client'; // eslint-disable-next-line import/no-cycle diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 4b0762be2..8fd6a7ad8 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -25,6 +25,7 @@ import { addServerPlugin, createResolver, defineNuxtModule, + extendViteConfig, } from '@nuxt/kit'; import type {Nuxt} from '@nuxt/schema'; import {defu} from 'defu'; @@ -294,6 +295,34 @@ export default defineNuxtModule({ // ── Auth callback ──────────────────────────────────────────────────────── addComponent({filePath: resolve('./runtime/components/auth/Callback'), name: 'AsgardeoCallback'}); + + // Tell Vite to pre-bundle the CJS-only packages that @asgardeo/browser, + // @asgardeo/javascript, and @asgardeo/vue carry as external dependencies. + // Without this, Vite serves them raw from disk via @fs URLs and fails with + // "Export 'X' is not defined in module" errors when installed from npm. + // This is only needed for the client Vite build; Nitro handles the server. + extendViteConfig( + config => { + config.optimizeDeps ??= {}; + config.optimizeDeps.include ??= []; + + const deps = [ + '@asgardeo/browser', + '@asgardeo/javascript', + '@asgardeo/vue', + 'base64url', + 'cross-fetch', + 'fast-sha256', + ]; + + for (const dep of deps) { + if (!config.optimizeDeps.include.includes(dep)) { + config.optimizeDeps.include.push(dep); + } + } + }, + {client: true}, + ); }, }); From 26c18066862dc1ed52c8bbde277e0c2b7c988488 Mon Sep 17 00:00:00 2001 From: kavindadewmith Date: Wed, 29 Apr 2026 19:23:09 +0530 Subject: [PATCH 2/2] Fix lint errors --- packages/nuxt/src/module.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 8fd6a7ad8..0854713a7 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -31,6 +31,8 @@ import type {Nuxt} from '@nuxt/schema'; import {defu} from 'defu'; import type {AsgardeoNuxtConfig, AsgardeoSessionPayload, AsgardeoSSRData} from './runtime/types'; +type ViteUserConfig = Parameters[0]>[0]; + const PACKAGE_NAME: string = '@asgardeo/nuxt'; type ServerRoute = { @@ -302,11 +304,8 @@ export default defineNuxtModule({ // "Export 'X' is not defined in module" errors when installed from npm. // This is only needed for the client Vite build; Nitro handles the server. extendViteConfig( - config => { - config.optimizeDeps ??= {}; - config.optimizeDeps.include ??= []; - - const deps = [ + (viteConfig: ViteUserConfig) => { + const deps: string[] = [ '@asgardeo/browser', '@asgardeo/javascript', '@asgardeo/vue', @@ -315,11 +314,15 @@ export default defineNuxtModule({ 'fast-sha256', ]; - for (const dep of deps) { - if (!config.optimizeDeps.include.includes(dep)) { - config.optimizeDeps.include.push(dep); - } - } + const existingInclude: string[] = (viteConfig.optimizeDeps?.include as string[]) ?? []; + const newDeps: string[] = deps.filter((dep: string) => !existingInclude.includes(dep)); + + Object.assign(viteConfig, { + optimizeDeps: { + ...viteConfig.optimizeDeps, + include: [...existingInclude, ...newDeps], + }, + }); }, {client: true}, );