Skip to content

Commit

Permalink
feat: support transforming via require flag (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 10, 2022
1 parent d302850 commit eb0bcc8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions 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) {
Expand Down
2 changes: 1 addition & 1 deletion 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
5 changes: 5 additions & 0 deletions tests/specs/typescript/ts.ts
Expand Up @@ -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 }) => {
Expand Down
13 changes: 13 additions & 0 deletions tests/utils/tsx.ts
Expand Up @@ -93,6 +93,19 @@ export async function createNode(
cwd: fixturePath,
});
},
requireFlag(
filePath: string,
) {
return this.tsx({
args: [
'--eval',
'null',
'--require',
filePath,
],
cwd: fixturePath,
});
},
};
}

Expand Down

0 comments on commit eb0bcc8

Please sign in to comment.