| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e4f133c commit c658a8a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,7 +49,10 @@ const { setupMainThreadPort } = require('internal/worker/messaging'); | |||
| 49 | 49 | const { | |
| 50 | 50 | onGlobalUncaughtException, | |
| 51 | 51 | evalScript, | |
| 52 | + evalTypeScript, | ||
| 52 | 53 | evalModuleEntryPoint, | |
| 54 | + parseAndEvalCommonjsTypeScript, | ||
| 55 | + parseAndEvalModuleTypeScript, | ||
| 53 | 56 | } = require('internal/process/execution'); | |
| 54 | 57 | ||
| 55 | 58 | let debug = require('internal/util/debuglog').debuglog('worker', (fn) => { | |
@@ -166,7 +169,29 @@ port.on('message', (message) => { | |||
| 166 | 169 | value: filename, | |
| 167 | 170 | }); | |
| 168 | 171 | 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); | ||
| 170 | 195 | break; | |
| 171 | 196 | } | |
| 172 | 197 | ||
| Original file line number | Diff line number | Diff 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 flags = ['--experimental-strip-types', '--disable-warning=ExperimentalWarning']; | ||
| 21 | + | ||
| 22 | + test('Worker eval module typescript without input-type', async () => { | ||
| 23 | + const w = new Worker(esmHelloWorld, { eval: true, execArgv: [...flags] }); | ||
| 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 | + ...flags] }); | ||
| 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 | + ...flags] }); | ||
| 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 | + ...flags] }); | ||
| 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: [...flags] }); | ||
| 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 | + ...flags] }); | ||
| 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 | + ...flags] }); | ||
| 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 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments