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

Function fixCjsTypes

scripts/fix-types.js:19–41  ·  view source on GitHub ↗

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

(fileName)

Source from the content-addressed store, hash-verified

17 * @param {string} fileName
18 */
19async function fixCjsTypes(fileName) {
20 try {
21 // 1. Read the generated type file
22 let types = await fs.readFile(fileName, { encoding: 'utf-8' });
23
24 // 2. Remove the ESM-style default exports.
25 // We use Regex to handle the variation your compiler is producing.
26 let fixed = types
27 .replace(/export default _default;/g, '')
28 .replace(/, _default as default/g, '');
29
30 // 3. Append the CommonJS-friendly export.
31 // This is the "fix" that allows require('dompurify') to work with TS.
32 fixed += '\n// @ts-ignore\nexport = _default;\n';
33
34 // 4. Write the file back to the dist folder
35 await fs.writeFile(fileName, fixed);
36 } catch (err) {
37 // We catch the error but don't re-throw it.
38 // This ensures 'npm run build' continues even if this step hiccups.
39 console.warn(`Warning: Could not patch ${fileName}. Error: ${err.message}`);
40 }
41}

Callers 1

fix-types.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected