diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 03e70cc..9376733 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -91,6 +91,29 @@ if (WATCH) { treeShaking: true, }); + // Build the loader for modern browser + await build({ + ...config, + entryPoints: [resolve(SRC, 'browser.ts')], + format: 'iife', + outfile: resolve(DIST, 'browser.js'), + treeShaking: true, + target: [ + 'es6', + ] + }); + + // Build the modern browser bundle as importable string + await build({ + entryPoints: [resolve(DIST, 'browser.js')], + loader: { + '.js': 'text', + }, + format: 'esm', + minify: true, + outfile: resolve(DIST, 'inline.js'), + }); + // Transform to ES5 const transformedESM = await swc.transformFile(resolve(DIST, 'index.mjs'), swcOptions); diff --git a/lib/src/browser.ts b/lib/src/browser.ts new file mode 100644 index 0000000..3e33365 --- /dev/null +++ b/lib/src/browser.ts @@ -0,0 +1,11 @@ +import { hCaptchaLoader } from './loader'; + +import type { ILoaderParams } from './types.js'; + +declare global { + interface Window { + hCaptchaLoader: (params?: ILoaderParams) => Promise; + } +} + +window.hCaptchaLoader = hCaptchaLoader; diff --git a/lib/src/inline.ts b/lib/src/inline.ts new file mode 100644 index 0000000..3d8b6db --- /dev/null +++ b/lib/src/inline.ts @@ -0,0 +1,3 @@ +declare const hCaptchaLoaderInlineScript: string; + +export default hCaptchaLoaderInlineScript; diff --git a/package.json b/package.json index 3966bbd..e477617 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hcaptcha/loader", "description": "This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling.", - "version": "2.3.0", + "version": "2.4.0", "author": "hCaptcha team and contributors", "license": "MIT", "keywords": [ @@ -12,8 +12,19 @@ "main": "./dist/index.mjs", "types": "./dist/types/src/index.d.ts", "exports": { - "import": "./dist/index.mjs", - "require": "./dist/index.cjs" + ".": { + "types": "./dist/types/src/index.d.ts", + "import": "./dist/index.mjs", + "require": "./dist/index.cjs" + }, + "./browser": { + "types": "./dist/types/src/browser.d.ts", + "import": "./dist/browser.js" + }, + "./inline": { + "types": "./dist/types/src/inline.d.ts", + "import": "./dist/inline.js" + } }, "files": [ "README.md",