forked from JesperDramsch/python-deadlines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsTransform.js
More file actions
51 lines (44 loc) · 1.84 KB
/
Copy pathjsTransform.js
File metadata and controls
51 lines (44 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Custom transformer for JavaScript files
* Handles IIFE patterns and jQuery plugins
*/
module.exports = {
process(sourceText, sourcePath) {
// Skip transformation for node_modules and minified files
if (sourcePath.includes('node_modules') || sourcePath.includes('.min.js')) {
return { code: sourceText };
}
// Handle IIFE patterns - unwrap them for testing
let transformed = sourceText;
// Remove IIFE wrapper if present
const iifePattern = /^\s*\(\s*function\s*\(\s*\)\s*{([\s\S]*?)}\s*\)\s*\(\s*\)\s*;?\s*$/;
const iifeMatch = sourceText.match(iifePattern);
if (iifeMatch) {
transformed = iifeMatch[1];
}
// Remove jQuery document ready wrapper if present
const jqueryReadyPattern = /\$\(document\)\.ready\s*\(\s*function\s*\(\s*\)\s*{([\s\S]*?)}\s*\)\s*;?/;
const jqueryMatch = transformed.match(jqueryReadyPattern);
if (jqueryMatch) {
// Keep the initialization code but make it callable
transformed = transformed.replace(jqueryReadyPattern,
`if (typeof window !== 'undefined' && window.IS_TESTING !== true) {
$(document).ready(function() {${jqueryMatch[1]}});
}`
);
}
// Export any global objects for testing
const globalObjects = ['NotificationManager', 'FavoritesManager', 'ConferenceStateManager'];
globalObjects.forEach(obj => {
if (transformed.includes(`const ${obj} = {`) || transformed.includes(`var ${obj} = {`)) {
transformed += `\nif (typeof module !== 'undefined' && module.exports) { module.exports.${obj} = ${obj}; }`;
}
});
// Ensure 'use strict' is at the top if present
if (transformed.includes("'use strict'")) {
transformed = transformed.replace(/['"]use strict['"];?/g, '');
transformed = "'use strict';\n" + transformed;
}
return { code: transformed };
},
};