diff --git a/src/preflight.cts b/src/preflight.cts index 1e872f72..ed03c27b 100644 --- a/src/preflight.cts +++ b/src/preflight.cts @@ -1,6 +1,18 @@ import { constants as osConstants } from 'os'; import './suppress-warnings.cts'; +/** + * Hook require() to transform to CJS + * + * This needs to be loaded via --require flag so subsequent --require + * flags can support TypeScript. + * + * This is also added in loader.ts for the loader API. + * Although it is required twice, it's not executed twice because + * it's cached. + */ +require('@esbuild-kit/cjs-loader'); + // If a parent process is detected if (process.send) { function relaySignal(signal: NodeJS.Signals) { diff --git a/tests/fixtures/log-argv.ts b/tests/fixtures/log-argv.ts index 4f94da66..a8fd079c 100644 --- a/tests/fixtures/log-argv.ts +++ b/tests/fixtures/log-argv.ts @@ -1 +1 @@ -console.log(JSON.stringify(process.argv)); +console.log(JSON.stringify(process.argv) as string); // Unnecessary TS syntax to test diff --git a/tests/specs/typescript/ts.ts b/tests/specs/typescript/ts.ts index 09f174e1..d9cbd37a 100644 --- a/tests/specs/typescript/ts.ts +++ b/tests/specs/typescript/ts.ts @@ -50,6 +50,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => { assertResults(nodeProcess, true); expect(nodeProcess.stdout).toMatch('{"default":1234}'); }); + + test('Require flag', async () => { + const nodeProcess = await node.requireFlag(importPath); + assertResults(nodeProcess, true); + }); }); describe('full path via .js', ({ test }) => { diff --git a/tests/utils/tsx.ts b/tests/utils/tsx.ts index dc393951..ef586fc4 100644 --- a/tests/utils/tsx.ts +++ b/tests/utils/tsx.ts @@ -93,6 +93,19 @@ export async function createNode( cwd: fixturePath, }); }, + requireFlag( + filePath: string, + ) { + return this.tsx({ + args: [ + '--eval', + 'null', + '--require', + filePath, + ], + cwd: fixturePath, + }); + }, }; }