11/* eslint-disable @typescript-eslint/no-var-requires */
22'use strict' ;
33
4- const path = require ( 'path' ) ;
5- const { EventEmitter } = require ( 'events' ) ;
6- const async = require ( 'neo-async' ) ;
7- const fs = require ( 'graceful-fs' ) ;
8- const writeFileAtomic = require ( 'write-file-atomic' ) ;
9- const { DEFAULT_EXTENSIONS } = require ( '@babel/core' ) ;
10-
11- const getParser = require ( 'jscodeshift/src/getParser' ) ;
12- const jscodeshift = require ( 'jscodeshift/src/core' ) ;
13-
14- let presetEnv ;
15- try {
16- presetEnv = require ( '@babel/preset-env' ) ;
17- } catch ( _ ) { }
4+ import path from 'path' ;
5+ import { fileURLToPath } from 'url' ;
6+ import fs from 'graceful-fs' ;
7+ import { EventEmitter } from 'events' ;
8+ import async from 'neo-async' ;
9+ import writeFileAtomic from 'write-file-atomic' ;
10+
11+ import getParser from 'jscodeshift/src/getParser.js' ;
12+ import jscodeshift from 'jscodeshift/src/core.js' ;
13+
14+ /**
15+ * Register the TSX plugin to allow require TS(X) files.
16+ */
17+ import { register } from 'tsx/esm/api' ;
18+ register ( ) ;
1819
1920let emitter ;
2021let finish ;
2122let notify ;
2223let transform ;
2324let parserFromTransform ;
2425
25- if ( module . parent ) {
26+ if ( import . meta . url . replace ( 'file://' , '' ) !== process . argv [ 1 ] ) {
2627 emitter = new EventEmitter ( ) ;
2728 // @ts -expect-error
2829 emitter . send = data => run ( data ) ;
2930 finish = ( ) => emitter . emit ( 'disconnect' ) ;
3031 notify = data => emitter . emit ( 'message' , data ) ;
31-
32- module . exports = args => {
33- setup ( args [ 0 ] , args [ 1 ] ) ;
34- return emitter ;
35- } ;
3632} else {
3733 finish = ( ) => setImmediate ( ( ) => process . disconnect ( ) ) ;
3834 notify = data => process . send ( data ) ;
3935 process . on ( 'message' , data => run ( data ) ) ;
4036 setup ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
4137}
4238
39+ // Used by `run-in-band` to run the worker in the same process
40+ export default async function main ( args ) {
41+ await setup ( args [ 0 ] , args [ 1 ] ) ;
42+ return emitter ;
43+ }
44+
4345function prepareJscodeshift ( options ) {
4446 const parser =
4547 parserFromTransform || getParser ( options . parser , options . parserConfig ) ;
@@ -59,59 +61,34 @@ function retrievePath(str) {
5961}
6062
6163function getModule ( mod ) {
62- return mod . hasOwnProperty ( ' default' ) ? mod . default : mod ;
64+ return Boolean ( mod . default ) ? mod . default : mod ;
6365}
6466
65- function setup ( entryPath , babel ) {
66- if ( babel === 'babel' ) {
67- const presets = [ ] ;
68- if ( presetEnv ) {
69- presets . push ( [ presetEnv . default , { targets : { node : true } } ] ) ;
70- }
71-
72- presets . push ( require ( '@babel/preset-typescript' ) . default ) ;
73-
74- require ( '@babel/register' ) ( {
75- configFile : false ,
76- babelrc : false ,
77- presets,
78- plugins : [
79- require ( '@babel/plugin-proposal-class-properties' ) . default ,
80- require ( '@babel/plugin-proposal-nullish-coalescing-operator' ) . default ,
81- require ( '@babel/plugin-proposal-optional-chaining' ) . default ,
82- require ( '@babel/plugin-transform-modules-commonjs' ) . default ,
83- ] ,
84- extensions : [ ...DEFAULT_EXTENSIONS , '.ts' , '.tsx' ] ,
85- // By default, babel register only compiles things inside the current working directory.
86- // https://github.com/babel/babel/blob/2a4f16236656178e84b05b8915aab9261c55782c/packages/babel-register/src/node.js#L140-L157
87- ignore : [
88- // Ignore parser related files
89- / @ b a b e l \/ p a r s e r / ,
90- / \/ f l o w - p a r s e r \/ / ,
91- / \/ r e c a s t \/ / ,
92- / \/ a s t - t y p e s \/ / ,
93- ] ,
94- } ) ;
95- }
67+ async function getModuleName ( path ) {
68+ const moduleName = retrievePath ( path ) . split ( 'node_modules/' ) [ 1 ] ;
69+ const pkg = await import ( moduleName ) ;
70+ return getModule ( pkg ) ;
71+ }
9672
73+ async function setup ( entryPath ) {
9774 const transformId = retrieveTransformId ( entryPath ) ;
9875 const presetId = retrievePresetId ( entryPath ) ;
9976
10077 let transformPkg ;
10178 let transformModule ;
10279
10380 if ( transformId ) {
104- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
81+ transformPkg = await getModuleName ( entryPath ) ;
10582 transformModule = transformPkg . transforms [ transformId ] ;
10683 }
10784
10885 if ( presetId ) {
109- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
86+ transformPkg = await getModuleName ( entryPath ) ;
11087 transformModule = transformPkg . presets [ presetId ] ;
11188 }
11289
11390 if ( ! transformId && ! presetId ) {
114- transformModule = require ( path . resolve ( entryPath ) ) ;
91+ transformModule = await import ( path . resolve ( entryPath ) ) ;
11592 }
11693
11794 transform = getModule ( transformModule ) ;
@@ -141,6 +118,7 @@ function trimStackTrace(trace) {
141118 // Remove this file from the stack trace of an error thrown in the transformer
142119 const result = [ ] ;
143120 trace . split ( '\n' ) . every ( line => {
121+ const __filename = fileURLToPath ( import . meta. url ) ;
144122 if ( line . indexOf ( __filename ) === - 1 ) {
145123 result . push ( line ) ;
146124 return true ;
0 commit comments