From 5be0f87fc3b885a724e20d47f1f0559192a3772d Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Thu, 23 Nov 2023 22:17:02 +0200 Subject: [PATCH 1/3] fix(input_secrets): add ow import as a banner to correctly import it per bundle --- packages/input_secrets/src/input_secrets.ts | 3 ++- packages/input_secrets/src/ow.ts | 9 -------- packages/input_secrets/tsup.config.ts | 23 ++++++++++++++++++++- scripts/create-tsup-config.ts | 6 +++--- 4 files changed, 27 insertions(+), 14 deletions(-) delete mode 100644 packages/input_secrets/src/ow.ts diff --git a/packages/input_secrets/src/input_secrets.ts b/packages/input_secrets/src/input_secrets.ts index d317ad9ea..a9bb4887b 100644 --- a/packages/input_secrets/src/input_secrets.ts +++ b/packages/input_secrets/src/input_secrets.ts @@ -1,7 +1,8 @@ import { KeyObject } from 'crypto'; import { privateDecrypt, publicEncrypt } from '@apify/utilities'; +import type { Ow } from 'ow'; -import { ow } from './ow'; +declare const ow: Ow; const BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/; const ENCRYPTED_INPUT_VALUE_PREFIX = 'ENCRYPTED_VALUE'; diff --git a/packages/input_secrets/src/ow.ts b/packages/input_secrets/src/ow.ts deleted file mode 100644 index 63a8d72d8..000000000 --- a/packages/input_secrets/src/ow.ts +++ /dev/null @@ -1,9 +0,0 @@ -// This file is needed to solve ow's false representation of the package in older versions. - -// In CJS, to use ow, you need to call ow.default -// In ESM, you'd think `import ow from 'ow'` works, but it doesn't, because you need to import ow and use the default PROPERTY, not export. - -import type { Ow } from 'ow'; -import defOw = require('ow'); - -export const ow: Ow = defOw.default as Ow; diff --git a/packages/input_secrets/tsup.config.ts b/packages/input_secrets/tsup.config.ts index 265bee2eb..c1100f0e9 100644 --- a/packages/input_secrets/tsup.config.ts +++ b/packages/input_secrets/tsup.config.ts @@ -1,3 +1,24 @@ import { createTsupConfig } from '../../scripts/create-tsup-config'; -export default createTsupConfig(); +export default createTsupConfig({ + banner(ctx) { + switch (ctx.format) { + case 'cjs': { + return { + js: `const __ow_import = require('ow'); +const ow = __ow_import.default || __ow_import;`, + }; + } + case 'esm': { + return { + js: `import __ow_import from 'ow'; +const ow = __ow_import.default || __ow_import;`, + }; + } + default: { + return {}; + } + } + }, + shims: true, +}); diff --git a/scripts/create-tsup-config.ts b/scripts/create-tsup-config.ts index d8718fb94..68df9ecfb 100644 --- a/scripts/create-tsup-config.ts +++ b/scripts/create-tsup-config.ts @@ -7,7 +7,8 @@ export const createTsupConfig = ({ target = 'es2020', sourcemap = true, esbuildOptions = undefined, -}: ConfigOptions = {}) => defineConfig({ + ...other +}: Options = {}) => defineConfig({ clean: true, dts: false, entry: ['src/index.ts'], @@ -20,6 +21,5 @@ export const createTsupConfig = ({ keepNames: true, globalName, esbuildOptions, + ...other, }); - -type ConfigOptions = Pick; From e9b2b9cbc61501e16662bea621b2e03607651dc1 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Thu, 23 Nov 2023 22:25:00 +0200 Subject: [PATCH 2/3] fix: make tests and builds work --- packages/input_secrets/src/input_secrets.ts | 7 +++++-- packages/input_secrets/src/ow.ts | 19 +++++++++++++++++++ packages/input_secrets/tsup.config.ts | 4 ++-- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 packages/input_secrets/src/ow.ts diff --git a/packages/input_secrets/src/input_secrets.ts b/packages/input_secrets/src/input_secrets.ts index a9bb4887b..3a518608e 100644 --- a/packages/input_secrets/src/input_secrets.ts +++ b/packages/input_secrets/src/input_secrets.ts @@ -1,8 +1,11 @@ import { KeyObject } from 'crypto'; import { privateDecrypt, publicEncrypt } from '@apify/utilities'; -import type { Ow } from 'ow'; +import _testOw, { type Ow } from 'ow'; -declare const ow: Ow; +// eslint-disable-next-line no-underscore-dangle +declare const __injectedOw: Ow; + +const ow: Ow = typeof __injectedOw === 'undefined' ? _testOw : __injectedOw || _testOw; const BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/; const ENCRYPTED_INPUT_VALUE_PREFIX = 'ENCRYPTED_VALUE'; diff --git a/packages/input_secrets/src/ow.ts b/packages/input_secrets/src/ow.ts new file mode 100644 index 000000000..71192291c --- /dev/null +++ b/packages/input_secrets/src/ow.ts @@ -0,0 +1,19 @@ +// This file is needed to solve ow's false representation of the package in older versions, as well as other issues due to tsup and double build issues + +// In CJS, to use ow, you need to call ow.default +// In ESM, you'd think `import ow from 'ow'` works, but it doesn't, because you need to import ow and use the default PROPERTY, not export. + +import { createRequire } from 'node:module'; + +import type { Ow } from 'ow'; + +// Why do we need this? +// - By using just `require` (not `globalThis.require`), tsup polyfills it with a method that throws in ESM builds +// - By using `globalThis.require`, the typeof check will NOT be replaced with the typeof check for tsup's polyfill (and globalThis.require IS present) +// - By using `createRequire`, we can import ow like we do for CJS builds, making it all work 🎉 + +// eslint-disable-next-line no-underscore-dangle +const _require: typeof require = typeof globalThis.require !== 'undefined' ? require : createRequire(__dirname); + +const defOw = _require('ow'); +export const ow: Ow = defOw.default as Ow; diff --git a/packages/input_secrets/tsup.config.ts b/packages/input_secrets/tsup.config.ts index c1100f0e9..06da0201e 100644 --- a/packages/input_secrets/tsup.config.ts +++ b/packages/input_secrets/tsup.config.ts @@ -6,13 +6,13 @@ export default createTsupConfig({ case 'cjs': { return { js: `const __ow_import = require('ow'); -const ow = __ow_import.default || __ow_import;`, +const __injectedOw = __ow_import.default || __ow_import;`, }; } case 'esm': { return { js: `import __ow_import from 'ow'; -const ow = __ow_import.default || __ow_import;`, +const __injectedOw = __ow_import.default || __ow_import;`, }; } default: { From fbb0e99afc4db9b078f29d951d0ac2984a59b125 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Thu, 23 Nov 2023 22:28:53 +0200 Subject: [PATCH 3/3] chore: remove useless file --- packages/input_secrets/src/ow.ts | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 packages/input_secrets/src/ow.ts diff --git a/packages/input_secrets/src/ow.ts b/packages/input_secrets/src/ow.ts deleted file mode 100644 index 71192291c..000000000 --- a/packages/input_secrets/src/ow.ts +++ /dev/null @@ -1,19 +0,0 @@ -// This file is needed to solve ow's false representation of the package in older versions, as well as other issues due to tsup and double build issues - -// In CJS, to use ow, you need to call ow.default -// In ESM, you'd think `import ow from 'ow'` works, but it doesn't, because you need to import ow and use the default PROPERTY, not export. - -import { createRequire } from 'node:module'; - -import type { Ow } from 'ow'; - -// Why do we need this? -// - By using just `require` (not `globalThis.require`), tsup polyfills it with a method that throws in ESM builds -// - By using `globalThis.require`, the typeof check will NOT be replaced with the typeof check for tsup's polyfill (and globalThis.require IS present) -// - By using `createRequire`, we can import ow like we do for CJS builds, making it all work 🎉 - -// eslint-disable-next-line no-underscore-dangle -const _require: typeof require = typeof globalThis.require !== 'undefined' ? require : createRequire(__dirname); - -const defOw = _require('ow'); -export const ow: Ow = defOw.default as Ow;