@@ -4,7 +4,7 @@ import * as logger from "./logger";
44
55const TESTPLANE_TRANSFORM_HOOK = Symbol . for ( "testplane.transform.hook" ) ;
66
7- const TRANSFORM_EXTENSIONS = [ ".js" , ".jsx" , ".ts" , ".tsx" , ".mjs" , ".cjs" , ".mts" , ".cts" ] ;
7+ const TRANSFORM_CODE_EXTENSIONS = [ ".js" , ".jsx" , ".ts" , ".tsx" , ".mjs" , ".cjs" , ".mts" , ".cts" ] ;
88const ASSET_EXTENSIONS = [
99 ".css" ,
1010 ".scss" ,
@@ -20,6 +20,10 @@ const ASSET_EXTENSIONS = [
2020 ".woff2" ,
2121] ;
2222
23+ type ProcessWithTransformHook = typeof process & {
24+ [ TESTPLANE_TRANSFORM_HOOK ] ?: { revert : ( ) => void ; enableSourceMaps : ( ) => void } ;
25+ } ;
26+
2327let transformFunc : null | ( ( code : string , sourceFile : string , sourceMaps : boolean ) => string ) = null ;
2428
2529export const transformCode = (
@@ -83,37 +87,62 @@ export const transformCode = (
8387} ;
8488
8589export const registerTransformHook = ( isSilent : boolean = false ) : void => {
86- const processWithEsbuildSymbol = process as typeof process & {
87- [ TESTPLANE_TRANSFORM_HOOK ] ?: { revert : ( ) => void } ;
88- } ;
90+ const processWithTranspileSymbol = process as ProcessWithTransformHook ;
8991
90- if ( processWithEsbuildSymbol [ TESTPLANE_TRANSFORM_HOOK ] || process . env . TS_ENABLE === "false" ) {
92+ if ( processWithTranspileSymbol [ TESTPLANE_TRANSFORM_HOOK ] || process . env . TS_ENABLE === "false" ) {
9193 return ;
9294 }
9395
9496 try {
95- const revertTransformHook = addHook (
96- ( code , filename ) => transformCode ( code , { sourceFile : filename , sourceMaps : false , isSilent } ) ,
97- {
98- exts : TRANSFORM_EXTENSIONS ,
99- matcher : filename => ! filename . includes ( "node_modules" ) ,
100- ignoreNodeModules : false ,
101- } ,
102- ) ;
97+ const mkTransformCodeHook =
98+ ( sourceMaps = false ) : Parameters < typeof addHook > [ 0 ] =>
99+ ( code , sourceFile ) =>
100+ transformCode ( code , { sourceFile, sourceMaps, isSilent } ) ;
101+
102+ const transformCodeOptions : Parameters < typeof addHook > [ 1 ] = {
103+ exts : TRANSFORM_CODE_EXTENSIONS ,
104+ ignoreNodeModules : true ,
105+ } ;
106+
107+ let areSourceMapsEnabled = false ;
108+
109+ let revertTransformHook = addHook ( mkTransformCodeHook ( ) , transformCodeOptions ) ;
103110
104111 const revertAssetHook = addHook ( ( ) => "module.exports = {};" , {
105112 exts : ASSET_EXTENSIONS ,
106113 ignoreNodeModules : false ,
107114 } ) ;
108115
116+ const enableSourceMaps = ( ) : void => {
117+ if ( areSourceMapsEnabled ) {
118+ return ;
119+ }
120+
121+ areSourceMapsEnabled = true ;
122+
123+ revertTransformHook ( ) ;
124+
125+ revertTransformHook = addHook ( mkTransformCodeHook ( true ) , transformCodeOptions ) ;
126+ } ;
127+
109128 const revertAll = ( ) : void => {
110129 revertTransformHook ( ) ;
111130 revertAssetHook ( ) ;
112- delete processWithEsbuildSymbol [ TESTPLANE_TRANSFORM_HOOK ] ;
131+ delete processWithTranspileSymbol [ TESTPLANE_TRANSFORM_HOOK ] ;
113132 } ;
114133
115- processWithEsbuildSymbol [ TESTPLANE_TRANSFORM_HOOK ] = { revert : revertAll } ;
134+ processWithTranspileSymbol [ TESTPLANE_TRANSFORM_HOOK ] = { revert : revertAll , enableSourceMaps } ;
116135 } catch ( err ) {
117136 logger . warn ( `testplane: an error occurred while trying to register transform hook.` , err ) ;
118137 }
119138} ;
139+
140+ export const enableSourceMaps = ( ) : void => {
141+ const processWithTranspileSymbol = process as ProcessWithTransformHook ;
142+
143+ if ( ! processWithTranspileSymbol [ TESTPLANE_TRANSFORM_HOOK ] ) {
144+ return ;
145+ }
146+
147+ processWithTranspileSymbol [ TESTPLANE_TRANSFORM_HOOK ] . enableSourceMaps ( ) ;
148+ } ;
0 commit comments