Skip to content

Commit

Permalink
Add triple-slash types reference to trusted-types.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted committed Nov 26, 2024
1 parent e972e6e commit 4722546
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
1 change: 1 addition & 0 deletions dist/purify.cjs.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="trusted-types" />
/*! @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 */

/**
Expand Down
1 change: 1 addition & 0 deletions dist/purify.es.d.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="trusted-types" />
/*! @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 */

/**
Expand Down
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
31 changes: 0 additions & 31 deletions scripts/fix-cjs-types.js

This file was deleted.

58 changes: 58 additions & 0 deletions scripts/fix-types.js
Original file line number Diff line number Diff line change
@@ -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 `/// <reference types="trusted-types" />\n${types}`;
}

0 comments on commit 4722546

Please sign in to comment.