@@ -52,55 +52,60 @@ function calculateCachePath(content: string, filePath: string): string {
5252 return path . join ( cacheDir , hash [ 0 ] + hash [ 1 ] , fileName ) ;
5353}
5454
55+ export function transformHook ( code : string , filename : string , isModule = false ) : string {
56+ const cachePath = calculateCachePath ( code , filename ) ;
57+ const codePath = cachePath + '.js' ;
58+ const sourceMapPath = cachePath + '.map' ;
59+ sourceMaps . set ( filename , sourceMapPath ) ;
60+ if ( fs . existsSync ( codePath ) )
61+ return fs . readFileSync ( codePath , 'utf8' ) ;
62+ // We don't use any browserslist data, but babel checks it anyway.
63+ // Silence the annoying warning.
64+ process . env . BROWSERSLIST_IGNORE_OLD_DATA = 'true' ;
65+ const babel : typeof import ( '@babel/core' ) = require ( '@babel/core' ) ;
66+
67+ const plugins = [
68+ [ require . resolve ( '@babel/plugin-proposal-class-properties' ) ] ,
69+ [ require . resolve ( '@babel/plugin-proposal-numeric-separator' ) ] ,
70+ [ require . resolve ( '@babel/plugin-proposal-logical-assignment-operators' ) ] ,
71+ [ require . resolve ( '@babel/plugin-proposal-nullish-coalescing-operator' ) ] ,
72+ [ require . resolve ( '@babel/plugin-proposal-optional-chaining' ) ] ,
73+ [ require . resolve ( '@babel/plugin-syntax-json-strings' ) ] ,
74+ [ require . resolve ( '@babel/plugin-syntax-optional-catch-binding' ) ] ,
75+ [ require . resolve ( '@babel/plugin-syntax-async-generators' ) ] ,
76+ [ require . resolve ( '@babel/plugin-syntax-object-rest-spread' ) ] ,
77+ [ require . resolve ( '@babel/plugin-proposal-export-namespace-from' ) ] ,
78+ ] ;
79+ if ( ! isModule ) {
80+ plugins . push ( [ require . resolve ( '@babel/plugin-transform-modules-commonjs' ) ] ) ;
81+ plugins . push ( [ require . resolve ( '@babel/plugin-proposal-dynamic-import' ) ] ) ;
82+ }
83+
84+ const result = babel . transformFileSync ( filename , {
85+ babelrc : false ,
86+ configFile : false ,
87+ assumptions : {
88+ // Without this, babel defines a top level function that
89+ // breaks playwright evaluates.
90+ setPublicClassFields : true ,
91+ } ,
92+ presets : [
93+ [ require . resolve ( '@babel/preset-typescript' ) , { onlyRemoveTypeImports : true } ] ,
94+ ] ,
95+ plugins,
96+ sourceMaps : 'both' ,
97+ } as babel . TransformOptions ) ! ;
98+ if ( result . code ) {
99+ fs . mkdirSync ( path . dirname ( cachePath ) , { recursive : true } ) ;
100+ if ( result . map )
101+ fs . writeFileSync ( sourceMapPath , JSON . stringify ( result . map ) , 'utf8' ) ;
102+ fs . writeFileSync ( codePath , result . code , 'utf8' ) ;
103+ }
104+ return result . code || '' ;
105+ }
106+
55107export function installTransform ( ) : ( ) => void {
56- return pirates . addHook ( ( code , filename ) => {
57- const cachePath = calculateCachePath ( code , filename ) ;
58- const codePath = cachePath + '.js' ;
59- const sourceMapPath = cachePath + '.map' ;
60- sourceMaps . set ( filename , sourceMapPath ) ;
61- if ( fs . existsSync ( codePath ) )
62- return fs . readFileSync ( codePath , 'utf8' ) ;
63- // We don't use any browserslist data, but babel checks it anyway.
64- // Silence the annoying warning.
65- process . env . BROWSERSLIST_IGNORE_OLD_DATA = 'true' ;
66- const babel : typeof import ( '@babel/core' ) = require ( '@babel/core' ) ;
67- const result = babel . transformFileSync ( filename , {
68- babelrc : false ,
69- configFile : false ,
70- assumptions : {
71- // Without this, babel defines a top level function that
72- // breaks playwright evaluates.
73- setPublicClassFields : true ,
74- } ,
75- presets : [
76- [ require . resolve ( '@babel/preset-typescript' ) , { onlyRemoveTypeImports : true } ] ,
77- ] ,
78- plugins : [
79- [ require . resolve ( '@babel/plugin-proposal-class-properties' ) ] ,
80- [ require . resolve ( '@babel/plugin-proposal-numeric-separator' ) ] ,
81- [ require . resolve ( '@babel/plugin-proposal-logical-assignment-operators' ) ] ,
82- [ require . resolve ( '@babel/plugin-proposal-nullish-coalescing-operator' ) ] ,
83- [ require . resolve ( '@babel/plugin-proposal-optional-chaining' ) ] ,
84- [ require . resolve ( '@babel/plugin-syntax-json-strings' ) ] ,
85- [ require . resolve ( '@babel/plugin-syntax-optional-catch-binding' ) ] ,
86- [ require . resolve ( '@babel/plugin-syntax-async-generators' ) ] ,
87- [ require . resolve ( '@babel/plugin-syntax-object-rest-spread' ) ] ,
88- [ require . resolve ( '@babel/plugin-proposal-export-namespace-from' ) ] ,
89- [ require . resolve ( '@babel/plugin-transform-modules-commonjs' ) ] ,
90- [ require . resolve ( '@babel/plugin-proposal-dynamic-import' ) ] ,
91- ] ,
92- sourceMaps : 'both' ,
93- } as babel . TransformOptions ) ! ;
94- if ( result . code ) {
95- fs . mkdirSync ( path . dirname ( cachePath ) , { recursive : true } ) ;
96- if ( result . map )
97- fs . writeFileSync ( sourceMapPath , JSON . stringify ( result . map ) , 'utf8' ) ;
98- fs . writeFileSync ( codePath , result . code , 'utf8' ) ;
99- }
100- return result . code || '' ;
101- } , {
102- exts : [ '.ts' ]
103- } ) ;
108+ return pirates . addHook ( transformHook , { exts : [ '.ts' ] } ) ;
104109}
105110
106111export function wrapFunctionWithLocation < A extends any [ ] , R > ( func : ( location : Location , ...args : A ) => R ) : ( ...args : A ) => R {
0 commit comments