MCPcopy Create free account
hub / github.com/cure53/DOMPurify / fixCjsTypes

Function fixCjsTypes

scripts/fix-types.js:65–91  ·  view source on GitHub ↗

* Fixes the CommonJS type declarations file. * @param {string} fileName

(fileName)

Source from the content-addressed store, hash-verified

63 * @param {string} fileName
64 */
65async function fixCjsTypes(fileName) {
66 try {
67 // 1. Read the generated type file
68 let types = await fs.readFile(fileName, { encoding: 'utf-8' });
69
70 // 2. Remove the ESM-style default exports.
71 // We use Regex to handle the variation your compiler is producing.
72 let fixed = types
73 .replace(/export default _default;/g, '')
74 .replace(/, _default as default/g, '');
75
76 // 3. Split any inline `type` export specifiers so older TypeScript versions
77 // can still parse the declarations.
78 fixed = rewriteInlineTypeExports(fixed);
79
80 // 4. Append the CommonJS-friendly export.
81 // This is the "fix" that allows require('dompurify') to work with TS.
82 fixed += '\n// @ts-ignore\nexport = _default;\n';
83
84 // 5. Write the file back to the dist folder
85 await fs.writeFile(fileName, fixed);
86 } catch (err) {
87 // We catch the error but don't re-throw it.
88 // This ensures 'npm run build' continues even if this step hiccups.
89 console.warn(`Warning: Could not patch ${fileName}. Error: ${err.message}`);
90 }
91}

Callers 1

fix-types.jsFile · 0.85

Calls 1

rewriteInlineTypeExportsFunction · 0.85

Tested by

no test coverage detected