diff --git a/packages/core/src/cli/compilationFixes.ts b/packages/core/src/cli/compilationFixes.ts new file mode 100644 index 000000000..dc89a342d --- /dev/null +++ b/packages/core/src/cli/compilationFixes.ts @@ -0,0 +1,33 @@ +// Fixes -> Error: Dynamic require of "fs" is not supported +// Issue link: https://github.com/evanw/esbuild/issues/1921 +const DYNAMIC_REQUIRE_FS_FIX = ` +// ---- DYNAMIC_REQUIRE_FS_FIX ---- +var require = (await import("node:module")).createRequire(import.meta.url); +var __filename = (await import("node:url")).fileURLToPath(import.meta.url); +var __dirname = (await import("node:path")).dirname(__filename); +// ---- DYNAMIC_REQUIRE_FS_FIX ---- +`.trim() + +// Fixes -> Error: Cannot find module '../package.json' +// This issue is related to the Playwright test runner. +const PLAYWRIGHT_RELATIVE_PACKAGE_IMPORT_FIX = ` +// ---- PLAYWRIGHT_RELATIVE_PACKAGE_IMPORT_FIX ---- +var originalRequireResolve = require.resolve; +require.resolve = (id, ...args) => { + if (id.endsWith("/package.json")) { + id = require("node:path").join(process.cwd(), "package.json"); + } + return originalRequireResolve(id, ...args); +} +// ---- PLAYWRIGHT_RELATIVE_PACKAGE_IMPORT_FIX ---- +`.trim() + +export const FIXES_BANNER = ` +/// ######## BANNER WITH FIXES START ######## + +${DYNAMIC_REQUIRE_FS_FIX} + +${PLAYWRIGHT_RELATIVE_PACKAGE_IMPORT_FIX} + +/// ######## BANNER WITH FIXES END ######## +`.trimStart() diff --git a/packages/core/src/cli/compileWalletSetupFunctions.ts b/packages/core/src/cli/compileWalletSetupFunctions.ts index eca6941a1..e464f0a2d 100644 --- a/packages/core/src/cli/compileWalletSetupFunctions.ts +++ b/packages/core/src/cli/compileWalletSetupFunctions.ts @@ -2,6 +2,7 @@ import path from 'node:path' import { glob } from 'glob' import { build } from 'tsup' import { ensureCacheDirExists } from '../ensureCacheDirExists' +import { FIXES_BANNER } from './compilationFixes' const OUT_DIR_NAME = 'wallet-setup-dist' @@ -22,6 +23,9 @@ export async function compileWalletSetupFunctions(walletSetupDir: string) { splitting: true, sourcemap: false, config: false, + banner: { + js: FIXES_BANNER + }, esbuildOptions(options) { options.drop = ['console', 'debugger'] }