Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8b20cc2

Browse filesBrowse files
worker: add eval ts input
PR-URL: #56394 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
1 parent 9d4930b commit 8b20cc2
Copy full SHA for 8b20cc2

File tree

Expand file treeCollapse file tree

2 files changed

+93
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+93
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/main/worker_thread.js‎

Copy file name to clipboardExpand all lines: lib/internal/main/worker_thread.js
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ const { setupMainThreadPort } = require('internal/worker/messaging');
4949
const {
5050
onGlobalUncaughtException,
5151
evalScript,
52+
evalTypeScript,
5253
evalModuleEntryPoint,
54+
parseAndEvalCommonjsTypeScript,
55+
parseAndEvalModuleTypeScript,
5356
} = require('internal/process/execution');
5457

5558
let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
@@ -166,7 +169,29 @@ port.on('message', (message) => {
166169
value: filename,
167170
});
168171
ArrayPrototypeSplice(process.argv, 1, 0, name);
169-
evalScript(name, filename);
172+
const tsEnabled = getOptionValue('--experimental-strip-types');
173+
const inputType = getOptionValue('--input-type');
174+
175+
if (inputType === 'module-typescript' && tsEnabled) {
176+
// This is a special case where we want to parse and eval the
177+
// TypeScript code as a module
178+
parseAndEvalModuleTypeScript(filename, false);
179+
break;
180+
}
181+
182+
let evalFunction;
183+
if (inputType === 'commonjs') {
184+
evalFunction = evalScript;
185+
} else if (inputType === 'commonjs-typescript' && tsEnabled) {
186+
evalFunction = parseAndEvalCommonjsTypeScript;
187+
} else if (tsEnabled) {
188+
evalFunction = evalTypeScript;
189+
} else {
190+
// Default to commonjs.
191+
evalFunction = evalScript;
192+
}
193+
194+
evalFunction(name, filename);
170195
break;
171196
}
172197

Collapse file
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
const { test } = require('node:test');
6+
const { once } = require('events');
7+
8+
const esmHelloWorld = `
9+
import worker from 'worker_threads';
10+
const foo: string = 'Hello, World!';
11+
worker.parentPort.postMessage(foo);
12+
`;
13+
14+
const cjsHelloWorld = `
15+
const { parentPort } = require('worker_threads');
16+
const foo: string = 'Hello, World!';
17+
parentPort.postMessage(foo);
18+
`;
19+
20+
const disableTypeScriptWarningFlag = '--disable-warning=ExperimentalWarning';
21+
22+
test('Worker eval module typescript without input-type', async () => {
23+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: [disableTypeScriptWarningFlag] });
24+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
25+
});
26+
27+
test('Worker eval module typescript with --input-type=module-typescript', async () => {
28+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=module-typescript',
29+
disableTypeScriptWarningFlag] });
30+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
31+
});
32+
33+
test('Worker eval module typescript with --input-type=commonjs-typescript', async () => {
34+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=commonjs-typescript',
35+
disableTypeScriptWarningFlag] });
36+
37+
const [err] = await once(w, 'error');
38+
assert.strictEqual(err.name, 'SyntaxError');
39+
assert.match(err.message, /Cannot use import statement outside a module/);
40+
});
41+
42+
test('Worker eval module typescript with --input-type=module', async () => {
43+
const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=module',
44+
disableTypeScriptWarningFlag] });
45+
const [err] = await once(w, 'error');
46+
assert.strictEqual(err.name, 'SyntaxError');
47+
assert.match(err.message, /Missing initializer in const declaration/);
48+
});
49+
50+
test('Worker eval commonjs typescript without input-type', async () => {
51+
const w = new Worker(cjsHelloWorld, { eval: true, execArgv: [disableTypeScriptWarningFlag] });
52+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
53+
});
54+
55+
test('Worker eval commonjs typescript with --input-type=commonjs-typescript', async () => {
56+
const w = new Worker(cjsHelloWorld, { eval: true, execArgv: ['--input-type=commonjs-typescript',
57+
disableTypeScriptWarningFlag] });
58+
assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']);
59+
});
60+
61+
test('Worker eval commonjs typescript with --input-type=module-typescript', async () => {
62+
const w = new Worker(cjsHelloWorld, { eval: true, execArgv: ['--input-type=module-typescript',
63+
disableTypeScriptWarningFlag] });
64+
const [err] = await once(w, 'error');
65+
assert.strictEqual(err.name, 'ReferenceError');
66+
assert.match(err.message, /require is not defined in ES module scope, you can use import instead/);
67+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.