@@ -55,58 +55,45 @@ export const JestStencilPreprocessor: SyncTransformer = {
55
55
* @returns The transpiled code.
56
56
*/
57
57
function transpileWithStencil ( sourceText : string , sourcePath : string , options : TransformOptions ) : string {
58
- try {
59
- const transpileOptions = {
60
- file : sourcePath ,
61
- currentDirectory : options . config . rootDir || process . cwd ( ) ,
62
- componentMetadata : 'compilerstatic' ,
63
- coreImportPath : '@stencil/core/internal/testing' ,
64
- module : 'cjs' , // always use commonjs since we're in a node environment
65
- sourceMap : 'inline' as const ,
66
- styleImportData : 'queryparams' ,
67
- target : 'es2017' , // target ES2017 for modern node
68
- transformAliasedImportPaths : false ,
69
- } ;
70
-
71
- const results = transpileSync ( sourceText , transpileOptions ) ;
72
-
73
- // Check for errors
74
- const hasErrors =
75
- results . diagnostics && results . diagnostics . some ( ( diagnostic : any ) => diagnostic . level === 'error' ) ;
76
-
77
- if ( hasErrors ) {
78
- const msg = results . diagnostics
79
- . map ( ( diagnostic : any ) => {
80
- let m = '' ;
81
- if ( diagnostic . relFilePath ) {
82
- m += diagnostic . relFilePath ;
83
- if ( typeof diagnostic . lineNumber === 'number' ) {
84
- m += `:${ diagnostic . lineNumber + 1 } ` ;
85
- if ( typeof diagnostic . columnNumber === 'number' ) {
86
- m += `:${ diagnostic . columnNumber } ` ;
87
- }
58
+ const transpileOptions = {
59
+ file : sourcePath ,
60
+ currentDirectory : options . config . rootDir || process . cwd ( ) ,
61
+ componentMetadata : 'compilerstatic' ,
62
+ coreImportPath : '@stencil/core/internal/testing' ,
63
+ module : 'cjs' , // always use commonjs since we're in a node environment
64
+ sourceMap : 'inline' as const ,
65
+ styleImportData : 'queryparams' ,
66
+ target : 'es2017' , // target ES2017 for modern node
67
+ transformAliasedImportPaths : false ,
68
+ } ;
69
+
70
+ const results = transpileSync ( sourceText , transpileOptions ) ;
71
+
72
+ // Check for errors
73
+ const hasErrors = results . diagnostics && results . diagnostics . some ( ( diagnostic : any ) => diagnostic . level === 'error' ) ;
74
+
75
+ if ( hasErrors ) {
76
+ const msg = results . diagnostics
77
+ . map ( ( diagnostic : any ) => {
78
+ let m = '' ;
79
+ if ( diagnostic . relFilePath ) {
80
+ m += diagnostic . relFilePath ;
81
+ if ( typeof diagnostic . lineNumber === 'number' ) {
82
+ m += `:${ diagnostic . lineNumber + 1 } ` ;
83
+ if ( typeof diagnostic . columnNumber === 'number' ) {
84
+ m += `:${ diagnostic . columnNumber } ` ;
88
85
}
89
- m += '\n' ;
90
86
}
91
- m += diagnostic . messageText ;
92
- return m ;
93
- } )
94
- . join ( '\n\n' ) ;
95
- throw new Error ( msg ) ;
96
- }
97
-
98
- let code = results . code || sourceText ;
99
-
100
- // Ensure ES module imports are converted to CommonJS requires
101
- // This fixes issues where Stencil's transpiler sometimes outputs ES modules despite cjs setting
102
- code = convertESModulesToCommonJS ( code ) ;
103
-
104
- return code ;
105
- } catch ( error ) {
106
- // Fallback: if Stencil transpiler fails completely, try to convert ES modules to CommonJS manually
107
- console . warn ( `Stencil transpiler failed for ${ sourcePath } , using fallback transformation:` , error ) ;
108
- return convertESModulesToCommonJS ( sourceText ) ;
87
+ m += '\n' ;
88
+ }
89
+ m += diagnostic . messageText ;
90
+ return m ;
91
+ } )
92
+ . join ( '\n\n' ) ;
93
+ throw new Error ( msg ) ;
109
94
}
95
+
96
+ return results . code || sourceText ;
110
97
}
111
98
112
99
/**
@@ -139,36 +126,5 @@ function transformCSS(sourceText: string): string {
139
126
return `module.exports = ${ JSON . stringify ( sourceText ) } ;` ;
140
127
}
141
128
142
- /**
143
- * Convert ES module imports to CommonJS requires.
144
- * This ensures compatibility with Jest when Stencil's transpiler outputs ES modules.
145
- *
146
- * @param code - The code to transform.
147
- * @returns The transformed code with CommonJS requires.
148
- */
149
- function convertESModulesToCommonJS ( code : string ) : string {
150
- // Convert import statements to require statements
151
- // Handle: import Name from "./file.css?tag=name&encapsulation=shadow";
152
- code = code . replace ( / i m p o r t \s + ( \w + ) \s + f r o m \s + [ " ' ] ( [ ^ " ' ] + \. c s s \? [ ^ " ' ] + ) [ " ' ] ; ? / g, 'const $1 = require("$2");' ) ;
153
-
154
- // Handle: import { name } from "./file";
155
- code = code . replace ( / i m p o r t \s + \{ \s * ( [ ^ } ] + ) \s * \} \s + f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] ; ? / g, 'const { $1 } = require("$2");' ) ;
156
-
157
- // Handle: import * as name from "./file";
158
- code = code . replace ( / i m p o r t \s + \* \s + a s \s + ( \w + ) \s + f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] ; ? / g, 'const $1 = require("$2");' ) ;
159
-
160
- // Handle: import name from "./file";
161
- code = code . replace ( / i m p o r t \s + ( \w + ) \s + f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] ; ? / g, 'const $1 = require("$2");' ) ;
162
-
163
- // Convert export statements to module.exports
164
- // Handle: export { name };
165
- code = code . replace ( / e x p o r t \s + \{ \s * ( [ ^ } ] + ) \s * \} ; ? / g, 'module.exports = { $1 };' ) ;
166
-
167
- // Handle: export default name;
168
- code = code . replace ( / e x p o r t \s + d e f a u l t \s + ( [ ^ ; ] + ) ; ? / g, 'module.exports = $1;' ) ;
169
-
170
- return code ;
171
- }
172
-
173
129
// eslint-disable-next-line import/no-default-export
174
130
export default JestStencilPreprocessor ;
0 commit comments