diff --git a/dist/purify.cjs.d.ts b/dist/purify.cjs.d.ts index f137254d..82878c89 100644 --- a/dist/purify.cjs.d.ts +++ b/dist/purify.cjs.d.ts @@ -1,3 +1,4 @@ +/// /*! @license DOMPurify 3.2.1 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.1/LICENSE */ /** diff --git a/dist/purify.es.d.mts b/dist/purify.es.d.mts index a0045c71..31b8cfbe 100644 --- a/dist/purify.es.d.mts +++ b/dist/purify.es.d.mts @@ -1,3 +1,4 @@ +/// /*! @license DOMPurify 3.2.1 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.1/LICENSE */ /** diff --git a/package-lock.json b/package-lock.json index f4fe14a8..588e0ae2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@rollup/plugin-replace": "^6.0.1", "@rollup/plugin-terser": "^0.4.4", "@types/estree": "^1.0.0", + "@types/node": "^16.18.120", "cross-env": "^7.0.3", "eslint-config-prettier": "^8.5.0", "eslint-plugin-prettier": "^4.0.0", @@ -2229,7 +2230,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "14.14.13", + "version": "16.18.120", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.120.tgz", + "integrity": "sha512-Dmi4bhZ7CHyD4sv4awCZx9RBxWOXSejxTF6B5WQ5UzfLcyEg7JqdDDsjvdMRYES9EcTWHlHZe01PInSj18yP2A==", "dev": true, "license": "MIT" }, @@ -12538,7 +12541,9 @@ "dev": true }, "@types/node": { - "version": "14.14.13", + "version": "16.18.120", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.120.tgz", + "integrity": "sha512-Dmi4bhZ7CHyD4sv4awCZx9RBxWOXSejxTF6B5WQ5UzfLcyEg7JqdDDsjvdMRYES9EcTWHlHZe01PInSj18yP2A==", "dev": true }, "@types/normalize-package-data": { diff --git a/package.json b/package.json index 766bf165..6b975ee0 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "commit-amend-build": "scripts/commit-amend-build.sh", "prebuild": "rimraf dist/**", "dev": "cross-env NODE_ENV=development BABEL_ENV=rollup rollup -w -c -o dist/purify.js", - "build": "run-s build:types build:rollup build:fix-cjs-types build:cleanup", + "build": "run-s build:types build:rollup build:fix-types build:cleanup", "build:types": "tsc --outDir dist/types --declaration --emitDeclarationOnly", "build:rollup": "rollup -c", - "build:fix-cjs-types": "node ./scripts/fix-cjs-types.js", + "build:fix-types": "node ./scripts/fix-types.js", "build:umd": "rollup -c -f umd -o dist/purify.js", "build:umd:min": "rollup -c -f umd -o dist/purify.min.js -p terser", "build:es": "rollup -c -f es -o dist/purify.es.mjs", @@ -103,6 +103,7 @@ "@rollup/plugin-replace": "^6.0.1", "@rollup/plugin-terser": "^0.4.4", "@types/estree": "^1.0.0", + "@types/node": "^16.18.120", "cross-env": "^7.0.3", "eslint-config-prettier": "^8.5.0", "eslint-plugin-prettier": "^4.0.0", diff --git a/scripts/fix-cjs-types.js b/scripts/fix-cjs-types.js deleted file mode 100644 index a544e078..00000000 --- a/scripts/fix-cjs-types.js +++ /dev/null @@ -1,31 +0,0 @@ -const fs = require('node:fs/promises'); -const path = require('node:path'); - -(async () => { - // Note that this script is intended to run on the type declaration file that is - // output by Rollup, and not the type declaration file generated from TypeScript. - let fileName = path.resolve(__dirname, '../dist/purify.cjs.d.ts'); - let types = await fs.readFile(fileName, { encoding: 'utf-8' }); - - // DOMPurify is exported as a default export, but rollup changes - // it to be assigned to `module.exports`. We need to change the - // type declarations to match what rollup changed it to. Remove - // the "default" export from the `export { ... }` statement. - let fixed = types.replace(', _default as default', ''); - - // Verify that we actually removed something in case the - // type declarations are different to what we expected. - if (fixed === types) { - throw new Error('Failed to fix CommonJS type declarations.'); - } - - // Append `export = _default;` to match the - // `module.exports = DOMPurify` statement - // that Rollup creates. - fixed += '\n// @ts-ignore\nexport = _default;\n'; - - await fs.writeFile(fileName, fixed); -})().catch((ex) => { - console.error(ex); - process.exitCode = 1; -}); diff --git a/scripts/fix-types.js b/scripts/fix-types.js new file mode 100644 index 00000000..02aa4eeb --- /dev/null +++ b/scripts/fix-types.js @@ -0,0 +1,58 @@ +// @ts-check + +const fs = require('node:fs/promises'); +const path = require('node:path'); + +(async () => { + // Note that this script is intended to run on the type declaration file that is + // output by Rollup, and not the type declaration file generated from TypeScript. + await fixCjsTypes(path.resolve(__dirname, '../dist/purify.cjs.d.ts')); + await fixEsmTypes(path.resolve(__dirname, '../dist/purify.es.d.mts')); +})().catch((ex) => { + console.error(ex); + process.exitCode = 1; +}); + +/** + * Fixes the CommonJS type declarations file. + * @param {string} fileName + */ +async function fixCjsTypes(fileName) { + let types = await fs.readFile(fileName, { encoding: 'utf-8' }); + + // DOMPurify is exported as a default export, but rollup changes + // it to be assigned to `module.exports`. We need to change the + // type declarations to match what rollup changed it to. Remove + // the "default" export from the `export { ... }` statement. + let fixed = types.replace(', _default as default', ''); + + // Verify that we actually removed something in case the + // type declarations are different to what we expected. + if (fixed === types) { + throw new Error('Failed to fix CommonJS type declarations.'); + } + + // Append `export = _default;` to match the `module.exports = DOMPurify` + // statement that Rollup creates. This can cause compilation errors + // for certain configurations, so add a `@ts-ignore` comment before it. + fixed += '\n// @ts-ignore\nexport = _default;\n'; + + await fs.writeFile(fileName, addTrustedTypesReference(fixed)); +} + +/** + * Fixes the ESM type declarations file. + * @param {string} fileName + */ +async function fixEsmTypes(fileName) { + let types = await fs.readFile(fileName, { encoding: 'utf-8' }); + await fs.writeFile(fileName, addTrustedTypesReference(types)); +} + +function addTrustedTypesReference(types) { + // We need to tell TypeScript that we use the type declarations from + // `trusted-types` so that it ends up in our type declaration type). + // Without this, the references to trusted-types in the type declaration + // file can cause compilation errors for certain configurations. + return `/// \n${types}`; +}