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..0854713a7 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -25,11 +25,14 @@ import { addServerPlugin, createResolver, defineNuxtModule, + extendViteConfig, } from '@nuxt/kit'; 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 = { @@ -294,6 +297,35 @@ 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( + (viteConfig: ViteUserConfig) => { + const deps: string[] = [ + '@asgardeo/browser', + '@asgardeo/javascript', + '@asgardeo/vue', + 'base64url', + 'cross-fetch', + 'fast-sha256', + ]; + + 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}, + ); }, });