From 782c1325fdc42a88b05617b6d1697d625bd2230f Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Fri, 26 Jan 2024 17:18:22 +0100 Subject: [PATCH 01/20] fix(legacy): use swc to transform esm to es3 --- lib/esbuild.config.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 571b433..eed00b9 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -3,6 +3,8 @@ import { fileURLToPath } from 'url'; import { build, context, analyzeMetafile } from 'esbuild'; import * as dotenv from 'dotenv'; +import swc from '@swc/core'; +import { promises as fs } from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -44,6 +46,14 @@ const config = { sourcemap: BUILD === 'development', }; +const swcOptions = { + minify: true, + sourceMaps: true, + jsc: { + target: 'es3', + }, +}; + if (WATCH) { const ctx = await context({ @@ -51,29 +61,30 @@ if (WATCH) { format: 'esm', outfile: resolve(DIST, 'index.mjs'), treeShaking: true, - target: [ - 'es6' - ] }); await ctx.watch(); } else { + // Transpile TypeScript to ESM const resultESM = await build({ ...config, format: 'esm', - outfile: resolve(DIST, 'index.mjs'), + outfile: resolve(DIST, 'index.js'), treeShaking: true, - target: [ - 'es6' - ] }); + // Transpile TypeScript to CommonJS const resultCJS = await build({ ...config, format: 'cjs', outfile: resolve(DIST, 'index.cjs'), - treeShaking: true + treeShaking: true, }); + // Transform ESM to ES3 + const resultES3 = await swc.transformFile(resolve(DIST, 'index.js'), swcOptions); + await fs.writeFile(resolve(DIST, 'index.mjs'), resultES3.code, 'utf-8'); + await fs.writeFile(resolve(DIST, 'index.mjs.map'), resultES3.map, 'utf-8'); + if (DEBUG) { const analyzeESM = await analyzeMetafile(resultESM.metafile, { verbose: false @@ -81,6 +92,7 @@ if (WATCH) { const analyzeCJS = await analyzeMetafile(resultCJS.metafile, { verbose: false }); + console.log(analyzeESM); console.log(analyzeCJS); } From f7e361994b5f6dacbd368128612e22305cf3c20f Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Fri, 26 Jan 2024 17:24:39 +0100 Subject: [PATCH 02/20] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2d5d858..7f64a02 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": "1.1.3", + "version": "1.1.4", "author": "hCaptcha team and contributors", "license": "MIT", "keywords": [ From 9bb6a37aa3ed23747a55ff312f0801c37f731b9d Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Sun, 4 Feb 2024 22:13:24 +0100 Subject: [PATCH 03/20] chore(es5): generate iife from transformed code --- lib/esbuild.config.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index eed00b9..76afce1 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -4,7 +4,6 @@ import { fileURLToPath } from 'url'; import { build, context, analyzeMetafile } from 'esbuild'; import * as dotenv from 'dotenv'; import swc from '@swc/core'; -import { promises as fs } from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -50,7 +49,7 @@ const swcOptions = { minify: true, sourceMaps: true, jsc: { - target: 'es3', + target: 'es5', }, }; @@ -61,6 +60,9 @@ if (WATCH) { format: 'esm', outfile: resolve(DIST, 'index.mjs'), treeShaking: true, + target: [ + 'es6' + ] }); await ctx.watch(); } else { @@ -68,8 +70,11 @@ if (WATCH) { const resultESM = await build({ ...config, format: 'esm', - outfile: resolve(DIST, 'index.js'), + outfile: resolve(DIST, 'index.mjs'), treeShaking: true, + target: [ + 'es6' + ] }); // Transpile TypeScript to CommonJS @@ -80,10 +85,28 @@ if (WATCH) { treeShaking: true, }); - // Transform ESM to ES3 - const resultES3 = await swc.transformFile(resolve(DIST, 'index.js'), swcOptions); - await fs.writeFile(resolve(DIST, 'index.mjs'), resultES3.code, 'utf-8'); - await fs.writeFile(resolve(DIST, 'index.mjs.map'), resultES3.map, 'utf-8'); + // Transform to ES5 + const transformedESM = await swc.transformFile(resolve(DIST, 'index.mjs'), swcOptions); + + // Build ES5 bundle + const resultES5 = await build({ + ...config, + entryPoints: undefined, + format: 'iife', + globalName: 'hCaptcha', + stdin: { + contents: transformedESM.code, + resolveDir: DIST, + sourcefile: 'index.es5.js', + }, + outfile: resolve(DIST, 'index.es5.js'), + treeShaking: true, + minify: false, + target: [ + 'es5', + ] + }); + if (DEBUG) { const analyzeESM = await analyzeMetafile(resultESM.metafile, { @@ -92,8 +115,12 @@ if (WATCH) { const analyzeCJS = await analyzeMetafile(resultCJS.metafile, { verbose: false }); + const analyzeES5 = await analyzeMetafile(resultES5.metafile, { + verbose: false + }); console.log(analyzeESM); console.log(analyzeCJS); + console.log(analyzeES5); } } From dc0aaf15f237eae572b642f2bd3e437dd1a13f91 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Mon, 5 Feb 2024 00:09:18 +0100 Subject: [PATCH 04/20] chore(es5): export polyfills --- lib/esbuild.config.js | 8 ++++ lib/polyfills.js | 88 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 lib/polyfills.js diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 76afce1..6500255 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -107,6 +107,14 @@ if (WATCH) { ] }); + // Add Polyfills + await build({ + ...config, + entryPoints: [resolve(SRC, '../polyfills.js')], + format: 'cjs', + outfile: resolve(DIST, 'polyfills.js'), + treeShaking: true, + }); if (DEBUG) { const analyzeESM = await analyzeMetafile(resultESM.metafile, { diff --git a/lib/polyfills.js b/lib/polyfills.js new file mode 100644 index 0000000..fde7522 --- /dev/null +++ b/lib/polyfills.js @@ -0,0 +1,88 @@ +// Polyfills for ES5 browser support + +if (!Object.entries) { + Object.entries = function (obj) { + if (obj == null) { + throw new TypeError('Object.entries called on null or undefined'); + } + + var entries = []; + for (var key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + entries.push([key, obj[key]]); + } + } + + return entries; + }; +} + +if (typeof Object.assign !== 'function') { + Object.assign = function (target) { + if (target == null) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + target = Object(target); + + for (var index = 1; index < arguments.length; index++) { + var source = arguments[index]; + + if (source != null) { + for (var key in source) { + if (Object.prototype.hasOwnProperty.call(source, key)) { + target[key] = source[key]; + } + } + } + } + + return target; + }; +} + +if (!Array.prototype.find) { + Object.defineProperty(Array.prototype, 'find', { + value: function (predicate, thisArg) { + if (this == null) { + throw new TypeError('Array.prototype.find called on null or undefined'); + } + + if (typeof predicate !== 'function') { + throw new TypeError('predicate must be a function'); + } + + var array = Object(this); + var length = array.length >>> 0; + + for (var i = 0; i < length; i++) { + var value = array[i]; + if (predicate.call(thisArg, value, i, array)) { + return value; + } + } + + return undefined; + }, + configurable: true, + writable: true + }); +} + +if (!Object.getOwnPropertyDescriptors) { + Object.defineProperty(Object, 'getOwnPropertyDescriptors', { + value: function(obj) { + var descriptors = {}; + + for (var key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + descriptors[key] = Object.getOwnPropertyDescriptor(obj, key); + } + } + + return descriptors; + }, + configurable: true, + writable: true + }); +} From 9babf50c4f570a4b89117559abcb95ff134ab145 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Mon, 5 Feb 2024 09:14:14 +0100 Subject: [PATCH 05/20] chore(es5): minify es5 code --- lib/esbuild.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 6500255..20725b3 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -101,7 +101,6 @@ if (WATCH) { }, outfile: resolve(DIST, 'index.es5.js'), treeShaking: true, - minify: false, target: [ 'es5', ] From 73af8dd6ff29865a3bea7cc54f331051e541952a Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Wed, 7 Feb 2024 17:58:14 +0100 Subject: [PATCH 06/20] chore: update readme --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index b923c22..333d27a 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,45 @@ const { response } = await hcaptcha.execute({ async: true }); | `reportapi` | String | No | `-` | See enterprise docs. | | `sentry` | Boolean | No | `-` | See enterprise docs. | | `custom` | Boolean | No | `-` | See enterprise docs. | + +### ES5 Support + +To use in ES5 environments, add the following: + +1. **polyfills.js**: This script provides polyfills for features not supported in older browsers. + +```html + +``` + +2. **index.es5.js**: This is the main script file for the `@hcaptcha/loader` package, compiled for ES5 environments. + +```html + +``` + + +Once you have included the necessary dependencies, you can use the `@hcaptcha/loader` package in your JavaScript code. + +```html + + + + + + +
+ + + +``` + +#### Important Notes + +- Ensure that the paths to the script files (`polyfills.js` and `index.es5.js`) are correct relative to your HTML file. From 5ca4480fa27717e179b4538593780ac2be8ecaa4 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 8 Feb 2024 19:07:03 +0100 Subject: [PATCH 07/20] chore(es5): add core-js to lib package --- lib/esbuild.config.js | 6 ++- lib/package.json | 3 +- lib/polyfills.js | 88 ----------------------------------------- lib/src/polyfills.ts | 4 ++ lib/tsconfig.types.json | 3 ++ pnpm-lock.yaml | 12 ++++++ 6 files changed, 25 insertions(+), 91 deletions(-) delete mode 100644 lib/polyfills.js create mode 100644 lib/src/polyfills.ts diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 20725b3..e3c5f01 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -109,10 +109,12 @@ if (WATCH) { // Add Polyfills await build({ ...config, - entryPoints: [resolve(SRC, '../polyfills.js')], - format: 'cjs', + entryPoints: [resolve(SRC, 'polyfills.ts')], outfile: resolve(DIST, 'polyfills.js'), treeShaking: true, + target: [ + 'es5', + ] }); if (DEBUG) { diff --git a/lib/package.json b/lib/package.json index 8cef576..baea3df 100644 --- a/lib/package.json +++ b/lib/package.json @@ -12,7 +12,8 @@ "test:unit": "jest" }, "dependencies": { - "@sentry/browser": "^7.73.0" + "@sentry/browser": "^7.73.0", + "core-js": "^3.35.1" }, "devDependencies": { "@hcaptcha/types": "^1.0.3", diff --git a/lib/polyfills.js b/lib/polyfills.js deleted file mode 100644 index fde7522..0000000 --- a/lib/polyfills.js +++ /dev/null @@ -1,88 +0,0 @@ -// Polyfills for ES5 browser support - -if (!Object.entries) { - Object.entries = function (obj) { - if (obj == null) { - throw new TypeError('Object.entries called on null or undefined'); - } - - var entries = []; - for (var key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - entries.push([key, obj[key]]); - } - } - - return entries; - }; -} - -if (typeof Object.assign !== 'function') { - Object.assign = function (target) { - if (target == null) { - throw new TypeError('Cannot convert undefined or null to object'); - } - - target = Object(target); - - for (var index = 1; index < arguments.length; index++) { - var source = arguments[index]; - - if (source != null) { - for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } - } - - return target; - }; -} - -if (!Array.prototype.find) { - Object.defineProperty(Array.prototype, 'find', { - value: function (predicate, thisArg) { - if (this == null) { - throw new TypeError('Array.prototype.find called on null or undefined'); - } - - if (typeof predicate !== 'function') { - throw new TypeError('predicate must be a function'); - } - - var array = Object(this); - var length = array.length >>> 0; - - for (var i = 0; i < length; i++) { - var value = array[i]; - if (predicate.call(thisArg, value, i, array)) { - return value; - } - } - - return undefined; - }, - configurable: true, - writable: true - }); -} - -if (!Object.getOwnPropertyDescriptors) { - Object.defineProperty(Object, 'getOwnPropertyDescriptors', { - value: function(obj) { - var descriptors = {}; - - for (var key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - descriptors[key] = Object.getOwnPropertyDescriptor(obj, key); - } - } - - return descriptors; - }, - configurable: true, - writable: true - }); -} diff --git a/lib/src/polyfills.ts b/lib/src/polyfills.ts new file mode 100644 index 0000000..b46d37b --- /dev/null +++ b/lib/src/polyfills.ts @@ -0,0 +1,4 @@ +import 'core-js/es/array/find'; +import 'core-js/es/object/assign'; +import 'core-js/es/object/entries'; +import 'core-js/es/object/get-own-property-descriptors'; diff --git a/lib/tsconfig.types.json b/lib/tsconfig.types.json index 35f4d01..8cf2a7b 100644 --- a/lib/tsconfig.types.json +++ b/lib/tsconfig.types.json @@ -13,5 +13,8 @@ "include": [ "src/**/*" + ], + "exclude": [ + "src/polyfills.ts" ] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2433bb8..5065438 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,5 +1,9 @@ lockfileVersion: '6.0' +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + importers: .: @@ -43,6 +47,9 @@ importers: '@sentry/browser': specifier: ^7.73.0 version: 7.73.0 + core-js: + specifier: ^3.35.1 + version: 3.35.1 devDependencies: '@hcaptcha/types': specifier: ^1.0.3 @@ -2171,6 +2178,11 @@ packages: resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==} dev: true + /core-js@3.35.1: + resolution: {integrity: sha512-IgdsbxNyMskrTFxa9lWHyMwAJU5gXOPP+1yO+K59d50VLVAIDAbs7gIv705KzALModfK3ZrSZTPNpC0PQgIZuw==} + requiresBuild: true + dev: false + /corser@2.0.1: resolution: {integrity: sha512-utCYNzRSQIZNPIcGZdQc92UVJYAhtGAteCFg0yRaFm8f0P+CPtyGyHXJcGXnffjCybUCEx3FQ2G7U3/o9eIkVQ==} engines: {node: '>= 0.4.0'} From 6e154da654f133bf6f6915c0fda9da00e179809d Mon Sep 17 00:00:00 2001 From: Brad Peters Date: Thu, 8 Feb 2024 12:47:46 -0800 Subject: [PATCH 08/20] fix: add footer to expose hCaptchaLoader as function instead of hCaptchaLoader.hCaptchaLoader --- lib/esbuild.config.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index e3c5f01..d5fb8e8 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -93,13 +93,16 @@ if (WATCH) { ...config, entryPoints: undefined, format: 'iife', - globalName: 'hCaptcha', + globalName: 'hCaptchaLoaderPkg', stdin: { contents: transformedESM.code, resolveDir: DIST, sourcefile: 'index.es5.js', }, outfile: resolve(DIST, 'index.es5.js'), + footer: { + js: 'var hCaptchaLoader = hCaptchaLoaderPkg.hCaptchaLoader;', + }, treeShaking: true, target: [ 'es5', From 41a6951776fdd5c5396655736313c814ba1add79 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 8 Feb 2024 21:57:14 +0100 Subject: [PATCH 09/20] fix: cleanup --- lib/esbuild.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index d5fb8e8..3e18e51 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -92,7 +92,6 @@ if (WATCH) { const resultES5 = await build({ ...config, entryPoints: undefined, - format: 'iife', globalName: 'hCaptchaLoaderPkg', stdin: { contents: transformedESM.code, From d1f1dd475c4f7de292aed722192a8852fecbacba Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 8 Feb 2024 22:53:13 +0100 Subject: [PATCH 10/20] chore: update readme --- README.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 333d27a..b1f2e7b 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,13 @@ To use in ES5 environments, add the following: 1. **polyfills.js**: This script provides polyfills for features not supported in older browsers. ```html - + ``` 2. **index.es5.js**: This is the main script file for the `@hcaptcha/loader` package, compiled for ES5 environments. ```html - + ``` @@ -69,8 +69,8 @@ Once you have included the necessary dependencies, you can use the `@hcaptcha/lo ```html - - + +
@@ -84,7 +84,3 @@ Once you have included the necessary dependencies, you can use the `@hcaptcha/lo ``` - -#### Important Notes - -- Ensure that the paths to the script files (`polyfills.js` and `index.es5.js`) are correct relative to your HTML file. From 0238a9220531c9930df7a7d0e014c294a2ea088a Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Fri, 9 Feb 2024 02:16:01 +0100 Subject: [PATCH 11/20] chore: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b1f2e7b..d3119e5 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Once you have included the necessary dependencies, you can use the `@hcaptcha/lo
-
-``` +- `polyfills.js`: Provides polyfills for features not supported in older browsers. +- `index.es5.js`: **@hcaptcha/loader** package compiled for ES5 environments. -2. **index.es5.js**: This is the main script file for the `@hcaptcha/loader` package, compiled for ES5 environments. +### Import Bundle(s) +Both bundles generated utilize IIFE format instead of modern importation syntax such as `require` or `esm`. + +```js +// Optional polyfill import +import '@hCaptcha/loader/dist/polyfills.js'; +// ES5 version of hCaptcha Loader +import '@hCaptcha/loader/dist/index.es5.js'; + +hCaptchaLoader.then(function() { + var element = document.createElement('div'); + // hCaptcha API is ready + hcaptcha.render(element, { + sitekey: 'YOUR_SITE_KEY', + // Additional options here + }); +}); -```html - ``` +### CDN +The hCaptcha Loader targetted for older browsers can also be imported via a CDN by leveraging UNPKG](https://www.unpkg.com/), see example below. -Once you have included the necessary dependencies, you can use the `@hcaptcha/loader` package in your JavaScript code. ```html @@ -74,10 +88,13 @@ Once you have included the necessary dependencies, you can use the `@hcaptcha/lo From 9f16141463ae4b69786733578f178f6e542b4cbe Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Tue, 13 Feb 2024 13:05:24 +0100 Subject: [PATCH 14/20] fix: cleanup --- README.md | 6 +++--- lib/src/loader.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9eb6df2..5077ca7 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,11 @@ The hCaptcha Loader targetted for older browsers can also be imported via a CDN +
-
- + }); + ``` diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 3e18e51..b9ecb7b 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -100,7 +100,7 @@ if (WATCH) { }, outfile: resolve(DIST, 'index.es5.js'), footer: { - js: 'var hCaptchaLoader = hCaptchaLoaderPkg.hCaptchaLoader;', + js: 'window.hCaptchaLoader = hCaptchaLoaderPkg.hCaptchaLoader;', }, treeShaking: true, target: [ diff --git a/lib/src/loader.ts b/lib/src/loader.ts index 91e9750..83bad76 100644 --- a/lib/src/loader.ts +++ b/lib/src/loader.ts @@ -101,7 +101,7 @@ export function hCaptchaApi(params: ILoaderParams = { cleanup: true }, sentry: S export async function loadScript(params, retries = 0) { const message = retries < MAX_RETRIES ? 'Retry loading hCaptcha Api' : 'Exceeded maximum retries'; - const sentry = initSentry(params && params.sentry); + const sentry = initSentry(params.sentry); try { @@ -125,6 +125,6 @@ export async function loadScript(params, retries = 0) { } -export async function hCaptchaLoader(params) { +export async function hCaptchaLoader(params = {}) { return await loadScript(params); } From 113b9a2bdea69863778728cd1efd6a23b4ca50c9 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 15 Feb 2024 13:07:12 +0100 Subject: [PATCH 17/20] chore: version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e84012b..89b6320 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": "1.2.1", + "version": "1.2.2", "author": "hCaptcha team and contributors", "license": "MIT", "keywords": [ From 314b35ca0282af530d40fea09fe37a2b04fc34a1 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 15 Feb 2024 13:37:39 +0100 Subject: [PATCH 18/20] fix: simplify legacy ts readme --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 11531a9..1e763bf 100644 --- a/README.md +++ b/README.md @@ -78,15 +78,10 @@ hCaptchaLoader().then(function(hcaptcha) { ``` ### TypeScript -To ensure compatibility with TypeScript environments, it is necessary to either override the type (`(window as any).hCaptchaLoader()`) or declare an interface: +To ensure compatibility with TypeScript environments, use indexer access: ```ts -declare global { - interface Window { - hCaptchaLoader: any; - } -} -window.hCaptchaLoader().then(function(hcaptcha) { +window['hCaptchaLoader']().then(function(hcaptcha) { var element = document.createElement('div'); // hCaptcha API is ready hcaptcha.render(element, { From d6bdc2e43c760e921bb548792b4300c75105f34e Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 15 Feb 2024 14:39:31 +0100 Subject: [PATCH 19/20] fix: sourcemaps and readme --- README.md | 15 +-------------- lib/esbuild.config.js | 2 +- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1e763bf..d96e3f9 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ import '@hCaptcha/loader/dist/polyfills.js'; // ES5 version of hCaptcha Loader import '@hCaptcha/loader/dist/index.es5.js'; -hCaptchaLoader().then(function(hcaptcha) { +window['hCaptchaLoader']().then(function(hcaptcha) { var element = document.createElement('div'); // hCaptcha API is ready hcaptcha.render(element, { @@ -76,19 +76,6 @@ hCaptchaLoader().then(function(hcaptcha) { }); }); -``` -### TypeScript -To ensure compatibility with TypeScript environments, use indexer access: -```ts - -window['hCaptchaLoader']().then(function(hcaptcha) { - var element = document.createElement('div'); - // hCaptcha API is ready - hcaptcha.render(element, { - sitekey: 'YOUR_SITE_KEY', - // Additional options here - }); -}); ``` ### CDN diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index b9ecb7b..3f06f13 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -47,7 +47,7 @@ const config = { const swcOptions = { minify: true, - sourceMaps: true, + sourceMaps: BUILD === 'development', jsc: { target: 'es5', }, From f53176335d064919410855e48aaa0d7fc54619b7 Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Thu, 15 Feb 2024 21:05:51 +0100 Subject: [PATCH 20/20] fix: readme --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d96e3f9..f8fea21 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ import '@hCaptcha/loader/dist/polyfills.js'; // ES5 version of hCaptcha Loader import '@hCaptcha/loader/dist/index.es5.js'; -window['hCaptchaLoader']().then(function(hcaptcha) { +hCaptchaLoader().then(function(hcaptcha) { var element = document.createElement('div'); // hCaptcha API is ready hcaptcha.render(element, { @@ -76,6 +76,15 @@ window['hCaptchaLoader']().then(function(hcaptcha) { }); }); +``` +### TypeScript +To handle typescript with ES5 version, use the following statement. +```ts +declare global { + interface Window { + hCaptchaLoader: any; + } +} ``` ### CDN