1
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
2
'use strict' ;
3
3
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 ( ) ;
18
19
19
20
let emitter ;
20
21
let finish ;
21
22
let notify ;
22
23
let transform ;
23
24
let parserFromTransform ;
24
25
25
- if ( module . parent ) {
26
+ if ( import . meta . url . replace ( 'file://' , '' ) !== process . argv [ 1 ] ) {
26
27
emitter = new EventEmitter ( ) ;
27
28
// @ts -expect-error
28
29
emitter . send = data => run ( data ) ;
29
30
finish = ( ) => emitter . emit ( 'disconnect' ) ;
30
31
notify = data => emitter . emit ( 'message' , data ) ;
31
-
32
- module . exports = args => {
33
- setup ( args [ 0 ] , args [ 1 ] ) ;
34
- return emitter ;
35
- } ;
36
32
} else {
37
33
finish = ( ) => setImmediate ( ( ) => process . disconnect ( ) ) ;
38
34
notify = data => process . send ( data ) ;
39
35
process . on ( 'message' , data => run ( data ) ) ;
40
36
setup ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
41
37
}
42
38
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
+
43
45
function prepareJscodeshift ( options ) {
44
46
const parser =
45
47
parserFromTransform || getParser ( options . parser , options . parserConfig ) ;
@@ -59,59 +61,34 @@ function retrievePath(str) {
59
61
}
60
62
61
63
function getModule ( mod ) {
62
- return mod . hasOwnProperty ( ' default' ) ? mod . default : mod ;
64
+ return Boolean ( mod . default ) ? mod . default : mod ;
63
65
}
64
66
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
+ }
96
72
73
+ async function setup ( entryPath ) {
97
74
const transformId = retrieveTransformId ( entryPath ) ;
98
75
const presetId = retrievePresetId ( entryPath ) ;
99
76
100
77
let transformPkg ;
101
78
let transformModule ;
102
79
103
80
if ( transformId ) {
104
- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
81
+ transformPkg = await getModuleName ( entryPath ) ;
105
82
transformModule = transformPkg . transforms [ transformId ] ;
106
83
}
107
84
108
85
if ( presetId ) {
109
- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
86
+ transformPkg = await getModuleName ( entryPath ) ;
110
87
transformModule = transformPkg . presets [ presetId ] ;
111
88
}
112
89
113
90
if ( ! transformId && ! presetId ) {
114
- transformModule = require ( path . resolve ( entryPath ) ) ;
91
+ transformModule = await import ( path . resolve ( entryPath ) ) ;
115
92
}
116
93
117
94
transform = getModule ( transformModule ) ;
@@ -141,6 +118,7 @@ function trimStackTrace(trace) {
141
118
// Remove this file from the stack trace of an error thrown in the transformer
142
119
const result = [ ] ;
143
120
trace . split ( '\n' ) . every ( line => {
121
+ const __filename = fileURLToPath ( import . meta. url ) ;
144
122
if ( line . indexOf ( __filename ) === - 1 ) {
145
123
result . push ( line ) ;
146
124
return true ;
0 commit comments