Skip to content

Commit cea034c

Browse files
authored
Merge pull request #1036 from reduckted/feature/trusted-types
Add triple-slash types reference to trusted-types.
2 parents e972e6e + 4722546 commit cea034c

File tree

6 files changed

+70
-35
lines changed

6 files changed

+70
-35
lines changed

dist/purify.cjs.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="trusted-types" />
12
/*! @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 */
23

34
/**

dist/purify.es.d.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="trusted-types" />
12
/*! @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 */
23

34
/**

package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"commit-amend-build": "scripts/commit-amend-build.sh",
88
"prebuild": "rimraf dist/**",
99
"dev": "cross-env NODE_ENV=development BABEL_ENV=rollup rollup -w -c -o dist/purify.js",
10-
"build": "run-s build:types build:rollup build:fix-cjs-types build:cleanup",
10+
"build": "run-s build:types build:rollup build:fix-types build:cleanup",
1111
"build:types": "tsc --outDir dist/types --declaration --emitDeclarationOnly",
1212
"build:rollup": "rollup -c",
13-
"build:fix-cjs-types": "node ./scripts/fix-cjs-types.js",
13+
"build:fix-types": "node ./scripts/fix-types.js",
1414
"build:umd": "rollup -c -f umd -o dist/purify.js",
1515
"build:umd:min": "rollup -c -f umd -o dist/purify.min.js -p terser",
1616
"build:es": "rollup -c -f es -o dist/purify.es.mjs",
@@ -103,6 +103,7 @@
103103
"@rollup/plugin-replace": "^6.0.1",
104104
"@rollup/plugin-terser": "^0.4.4",
105105
"@types/estree": "^1.0.0",
106+
"@types/node": "^16.18.120",
106107
"cross-env": "^7.0.3",
107108
"eslint-config-prettier": "^8.5.0",
108109
"eslint-plugin-prettier": "^4.0.0",

scripts/fix-cjs-types.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

scripts/fix-types.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @ts-check
2+
3+
const fs = require('node:fs/promises');
4+
const path = require('node:path');
5+
6+
(async () => {
7+
// Note that this script is intended to run on the type declaration file that is
8+
// output by Rollup, and not the type declaration file generated from TypeScript.
9+
await fixCjsTypes(path.resolve(__dirname, '../dist/purify.cjs.d.ts'));
10+
await fixEsmTypes(path.resolve(__dirname, '../dist/purify.es.d.mts'));
11+
})().catch((ex) => {
12+
console.error(ex);
13+
process.exitCode = 1;
14+
});
15+
16+
/**
17+
* Fixes the CommonJS type declarations file.
18+
* @param {string} fileName
19+
*/
20+
async function fixCjsTypes(fileName) {
21+
let types = await fs.readFile(fileName, { encoding: 'utf-8' });
22+
23+
// DOMPurify is exported as a default export, but rollup changes
24+
// it to be assigned to `module.exports`. We need to change the
25+
// type declarations to match what rollup changed it to. Remove
26+
// the "default" export from the `export { ... }` statement.
27+
let fixed = types.replace(', _default as default', '');
28+
29+
// Verify that we actually removed something in case the
30+
// type declarations are different to what we expected.
31+
if (fixed === types) {
32+
throw new Error('Failed to fix CommonJS type declarations.');
33+
}
34+
35+
// Append `export = _default;` to match the `module.exports = DOMPurify`
36+
// statement that Rollup creates. This can cause compilation errors
37+
// for certain configurations, so add a `@ts-ignore` comment before it.
38+
fixed += '\n// @ts-ignore\nexport = _default;\n';
39+
40+
await fs.writeFile(fileName, addTrustedTypesReference(fixed));
41+
}
42+
43+
/**
44+
* Fixes the ESM type declarations file.
45+
* @param {string} fileName
46+
*/
47+
async function fixEsmTypes(fileName) {
48+
let types = await fs.readFile(fileName, { encoding: 'utf-8' });
49+
await fs.writeFile(fileName, addTrustedTypesReference(types));
50+
}
51+
52+
function addTrustedTypesReference(types) {
53+
// We need to tell TypeScript that we use the type declarations from
54+
// `trusted-types` so that it ends up in our type declaration type).
55+
// Without this, the references to trusted-types in the type declaration
56+
// file can cause compilation errors for certain configurations.
57+
return `/// <reference types="trusted-types" />\n${types}`;
58+
}

0 commit comments

Comments
 (0)