Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support transforming via require flag #140

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/preflight.cts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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