Skip to content

Commit 3251546

Browse files
fix(test): don't do manual esm transpiling
1 parent 5a4c043 commit 3251546

File tree

1 file changed

+36
-80
lines changed

1 file changed

+36
-80
lines changed

src/preprocessor.ts

Lines changed: 36 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -55,58 +55,45 @@ export const JestStencilPreprocessor: SyncTransformer = {
5555
* @returns The transpiled code.
5656
*/
5757
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}`;
8885
}
89-
m += '\n';
9086
}
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);
10994
}
95+
96+
return results.code || sourceText;
11097
}
11198

11299
/**
@@ -139,36 +126,5 @@ function transformCSS(sourceText: string): string {
139126
return `module.exports = ${JSON.stringify(sourceText)};`;
140127
}
141128

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(/import\s+(\w+)\s+from\s+["']([^"']+\.css\?[^"']+)["'];?/g, 'const $1 = require("$2");');
153-
154-
// Handle: import { name } from "./file";
155-
code = code.replace(/import\s+\{\s*([^}]+)\s*\}\s+from\s+["']([^"']+)["'];?/g, 'const { $1 } = require("$2");');
156-
157-
// Handle: import * as name from "./file";
158-
code = code.replace(/import\s+\*\s+as\s+(\w+)\s+from\s+["']([^"']+)["'];?/g, 'const $1 = require("$2");');
159-
160-
// Handle: import name from "./file";
161-
code = code.replace(/import\s+(\w+)\s+from\s+["']([^"']+)["'];?/g, 'const $1 = require("$2");');
162-
163-
// Convert export statements to module.exports
164-
// Handle: export { name };
165-
code = code.replace(/export\s+\{\s*([^}]+)\s*\};?/g, 'module.exports = { $1 };');
166-
167-
// Handle: export default name;
168-
code = code.replace(/export\s+default\s+([^;]+);?/g, 'module.exports = $1;');
169-
170-
return code;
171-
}
172-
173129
// eslint-disable-next-line import/no-default-export
174130
export default JestStencilPreprocessor;

0 commit comments

Comments
 (0)