* Fixes the CommonJS type declarations file. * @param {string} fileName
(fileName)
| 17 | * @param {string} fileName |
| 18 | */ |
| 19 | async 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 | } |