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

Function fixCjsTypes

scripts/fix-types.js:19–40  ·  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 let types = await fs.readFile(fileName, { encoding: 'utf-8' });
21
22 // DOMPurify is exported as a default export, but rollup changes
23 // it to be assigned to `module.exports`. We need to change the
24 // type declarations to match what rollup changed it to. Remove
25 // the "default" export from the `export { ... }` statement.
26 let fixed = types.replace(', _default as default', '');
27
28 // Verify that we actually removed something in case the
29 // type declarations are different to what we expected.
30 if (fixed === types) {
31 throw new Error('Failed to fix CommonJS type declarations.');
32 }
33
34 // Append `export = _default;` to match the `module.exports = DOMPurify`
35 // statement that Rollup creates. This can cause compilation errors
36 // for certain configurations, so add a `@ts-ignore` comment before it.
37 fixed += '\n// @ts-ignore\nexport = _default;\n';
38
39 await fs.writeFile(fileName, fixed);
40}

Callers 1

fix-types.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected