@@ -13,31 +13,37 @@ const {
1313 prepareMainThreadExecution,
1414 markBootstrapComplete,
1515} = require ( 'internal/process/pre_execution' ) ;
16- const { evalModuleEntryPoint, evalScript } = require ( 'internal/process/execution' ) ;
16+ const {
17+ evalModuleEntryPoint,
18+ evalTypeScript,
19+ parseAndEvalCommonjsTypeScript,
20+ parseAndEvalModuleTypeScript,
21+ evalScript,
22+ } = require ( 'internal/process/execution' ) ;
1723const { addBuiltinLibsToObject } = require ( 'internal/modules/helpers' ) ;
18- const { stripTypeScriptModuleTypes } = require ( 'internal/modules/typescript' ) ;
1924const { getOptionValue } = require ( 'internal/options' ) ;
2025
2126prepareMainThreadExecution ( ) ;
2227addBuiltinLibsToObject ( globalThis , '<eval>' ) ;
2328markBootstrapComplete ( ) ;
2429
2530const code = getOptionValue ( '--eval' ) ;
26- const source = getOptionValue ( '--experimental-strip-types' ) ?
27- stripTypeScriptModuleTypes ( code ) :
28- code ;
2931
3032const print = getOptionValue ( '--print' ) ;
3133const shouldLoadESM = getOptionValue ( '--import' ) . length > 0 || getOptionValue ( '--experimental-loader' ) . length > 0 ;
32- if ( getOptionValue ( '--input-type' ) === 'module' ||
33- ( getOptionValue ( '--experimental-default-type' ) === 'module' && getOptionValue ( '--input-type' ) !== 'commonjs' ) ) {
34- evalModuleEntryPoint ( source , print ) ;
34+ const inputType = getOptionValue ( '--input-type' ) ;
35+ const tsEnabled = getOptionValue ( '--experimental-strip-types' ) ;
36+ if ( inputType === 'module' ||
37+ ( getOptionValue ( '--experimental-default-type' ) === 'module' && inputType !== 'commonjs' ) ) {
38+ evalModuleEntryPoint ( code , print ) ;
39+ } else if ( inputType === 'module-typescript' && tsEnabled ) {
40+ parseAndEvalModuleTypeScript ( code , print ) ;
3541} else {
3642 // For backward compatibility, we want the identifier crypto to be the
3743 // `node:crypto` module rather than WebCrypto.
3844 const isUsingCryptoIdentifier =
39- getOptionValue ( '--experimental-global-webcrypto' ) &&
40- RegExpPrototypeExec ( / \b c r y p t o \b / , source ) !== null ;
45+ getOptionValue ( '--experimental-global-webcrypto' ) &&
46+ RegExpPrototypeExec ( / \b c r y p t o \b / , code ) !== null ;
4147 const shouldDefineCrypto = isUsingCryptoIdentifier && internalBinding ( 'config' ) . hasOpenSSL ;
4248
4349 if ( isUsingCryptoIdentifier && ! shouldDefineCrypto ) {
@@ -52,11 +58,24 @@ if (getOptionValue('--input-type') === 'module' ||
5258 } ;
5359 ObjectDefineProperty ( object , name , { __proto__ : null , set : setReal } ) ;
5460 }
55- evalScript ( '[eval]' ,
56- shouldDefineCrypto ? (
57- print ? `let crypto=require("node:crypto");{${ source } }` : `(crypto=>{{${ source } }})(require('node:crypto'))`
58- ) : source ,
59- getOptionValue ( '--inspect-brk' ) ,
60- print ,
61- shouldLoadESM ) ;
61+
62+ let evalFunction ;
63+ if ( inputType === 'commonjs' ) {
64+ evalFunction = evalScript ;
65+ } else if ( inputType === 'commonjs-typescript' && tsEnabled ) {
66+ evalFunction = parseAndEvalCommonjsTypeScript ;
67+ } else if ( tsEnabled ) {
68+ evalFunction = evalTypeScript ;
69+ } else {
70+ // Default to commonjs.
71+ evalFunction = evalScript ;
72+ }
73+
74+ evalFunction ( '[eval]' ,
75+ shouldDefineCrypto ? (
76+ print ? `let crypto=require("node:crypto");{${ code } }` : `(crypto=>{{${ code } }})(require('node:crypto'))`
77+ ) : code ,
78+ getOptionValue ( '--inspect-brk' ) ,
79+ print ,
80+ shouldLoadESM ) ;
6281}
0 commit comments