From bbf11a5783801d5a711235f1357a1d2f68d83be4 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 31 Aug 2023 12:41:25 +0200 Subject: [PATCH 1/3] Migrate the typescript options to a config file Some configurations settings like `paths` cannot be provided through CLI arguments but only in a configuration file. And when using a configuration file, only a few options (like `--outDir` can still be provided) through the CLI. --- gulpfile.mjs | 13 +------------ tsconfig.json | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 tsconfig.json diff --git a/gulpfile.mjs b/gulpfile.mjs index 311c0ba9d..2eae8336a 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -1549,19 +1549,8 @@ gulp.task("jsdoc", function (done) { gulp.task("types", function (done) { console.log("### Generating TypeScript definitions using `tsc`"); - const args = [ - "target ESNext", - "allowJS", - "declaration", - `outDir ${TYPES_DIR}`, - "strict", - "esModuleInterop", - "forceConsistentCasingInFileNames", - "emitDeclarationOnly", - "moduleResolution node", - ].join(" --"); exec( - `"node_modules/.bin/tsc" --${args} src/pdf.js web/pdf_viewer.component.js`, + `"node_modules/.bin/tsc" --outDir ${TYPES_DIR} --project .`, done ); }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..346bcd2cc --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "ESNext", + "allowJs": true, + "declaration": true, + "strict": true, + "emitDeclarationOnly": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "moduleResolution": "node" + }, + "files": [ + "src/pdf.js", + "web/pdf_viewer.component.js" + ] +} From d0f14b1ce301a269a56358579263c59e8ffa5cdd Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 31 Aug 2023 12:59:27 +0200 Subject: [PATCH 2/3] Add support for the import map in the type generator The typescript compiler is now configured to know about the import map to be able to resolve those imports and find the associated types. As tsc outputs declaration files using the original module identifiers and not the resolved ones, tsc-alias is used to post-process the declaration files by resolving those paths. --- gulpfile.mjs | 7 +- package-lock.json | 256 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + tsconfig.json | 11 +- 4 files changed, 273 insertions(+), 2 deletions(-) diff --git a/gulpfile.mjs b/gulpfile.mjs index 2eae8336a..b587d99aa 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -266,6 +266,9 @@ function createWebpackConfig( viewerAlias["web-com"] = "web/chromecom.js"; viewerAlias["web-print_service"] = "web/pdf_print_service.js"; } else if (bundleDefines.GENERIC) { + // Aliases defined here must also be replicated in the paths section of + // the tsconfig.json file for the type generation to work. + // In the tsconfig.json files, the .js extension must be omitted. libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js"; libraryAlias["display-l10n_utils"] = "web/l10n_utils.js"; libraryAlias["display-network"] = "src/display/network.js"; @@ -1551,7 +1554,9 @@ gulp.task("types", function (done) { console.log("### Generating TypeScript definitions using `tsc`"); exec( `"node_modules/.bin/tsc" --outDir ${TYPES_DIR} --project .`, - done + function () { + exec(`"node_modules/.bin/tsc-alias" --outDir ${TYPES_DIR}`, done); + } ); }); diff --git a/package-lock.json b/package-lock.json index 3affc4a02..e21af5f58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,6 +57,7 @@ "stylelint-prettier": "^4.0.2", "terser": "^5.19.2", "through2": "^4.0.2", + "tsc-alias": "^1.8.7", "ttest": "^4.0.0", "typescript": "^5.1.6", "typogr": "^0.6.8", @@ -4395,6 +4396,16 @@ "url": "https://bevry.me/fund" } }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "dev": true, + "optional": true, + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, "node_modules/bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -4768,6 +4779,25 @@ "fsevents": "^1.2.7" } }, + "node_modules/chokidar/node_modules/fsevents": { + "version": "1.2.13", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", + "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", + "deprecated": "The v1 package contains DANGEROUS / INSECURE binaries. Upgrade to safe fsevents v2", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "dependencies": { + "bindings": "^1.5.0", + "nan": "^2.12.1" + }, + "engines": { + "node": ">= 4.0" + } + }, "node_modules/chokidar/node_modules/is-glob": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz", @@ -7534,6 +7564,13 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "dev": true, + "optional": true + }, "node_modules/fill-range": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz", @@ -7812,6 +7849,20 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -10912,6 +10963,19 @@ "node": ">= 0.10" } }, + "node_modules/mylas": { + "version": "2.1.13", + "resolved": "https://registry.npmjs.org/mylas/-/mylas-2.1.13.tgz", + "integrity": "sha512-+MrqnJRtxdF+xngFfUUkIMQrUUL0KsxbADUkn23Z/4ibGg192Q+z+CQyiYwvWTsYjJygmMR8+w3ZDa98Zh6ESg==", + "dev": true, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/raouldeheer" + } + }, "node_modules/nan": { "version": "2.17.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz", @@ -16597,6 +16661,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/plimit-lit": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/plimit-lit/-/plimit-lit-1.5.0.tgz", + "integrity": "sha512-Eb/MqCb1Iv/ok4m1FqIXqvUKPISufcjZ605hl3KM/n8GaX8zfhtgdLwZU3vKjuHGh2O9Rjog/bHTq8ofIShdng==", + "dev": true, + "dependencies": { + "queue-lit": "^1.5.0" + } + }, "node_modules/plugin-error": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/plugin-error/-/plugin-error-1.0.1.tgz", @@ -17165,6 +17238,12 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "node_modules/queue-lit": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/queue-lit/-/queue-lit-1.5.0.tgz", + "integrity": "sha512-IslToJ4eiCEE9xwMzq3viOO5nH8sUWUCwoElrhNMozzr9IIt2qqvB4I+uHu/zJTQVqc9R5DFwok4ijNK1pU3fA==", + "dev": true + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -19729,6 +19808,183 @@ "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==", "dev": true }, + "node_modules/tsc-alias": { + "version": "1.8.7", + "resolved": "https://registry.npmjs.org/tsc-alias/-/tsc-alias-1.8.7.tgz", + "integrity": "sha512-59Q/zUQa3miTf99mLbSqaW0hi1jt4WoG8Uhe5hSZJHQpSoFW9eEwvW7jlKMHXWvT+zrzy3SN9PE/YBhQ+WVydA==", + "dev": true, + "dependencies": { + "chokidar": "^3.5.3", + "commander": "^9.0.0", + "globby": "^11.0.4", + "mylas": "^2.1.9", + "normalize-path": "^3.0.0", + "plimit-lit": "^1.2.6" + }, + "bin": { + "tsc-alias": "dist/bin/index.js" + } + }, + "node_modules/tsc-alias/node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/tsc-alias/node_modules/binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/tsc-alias/node_modules/braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, + "dependencies": { + "fill-range": "^7.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tsc-alias/node_modules/chokidar": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", + "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", + "dev": true, + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/tsc-alias/node_modules/commander": { + "version": "9.5.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-9.5.0.tgz", + "integrity": "sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==", + "dev": true, + "engines": { + "node": "^12.20.0 || >=14" + } + }, + "node_modules/tsc-alias/node_modules/fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tsc-alias/node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/tsc-alias/node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tsc-alias/node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tsc-alias/node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/tsc-alias/node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tsc-alias/node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, + "node_modules/tsc-alias/node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, "node_modules/tsconfig-paths": { "version": "3.14.2", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.2.tgz", diff --git a/package.json b/package.json index f195f1673..86f2b6ca9 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "stylelint-prettier": "^4.0.2", "terser": "^5.19.2", "through2": "^4.0.2", + "tsc-alias": "^1.8.7", "ttest": "^4.0.0", "typescript": "^5.1.6", "typogr": "^0.6.8", diff --git a/tsconfig.json b/tsconfig.json index 346bcd2cc..f549d383c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,16 @@ "emitDeclarationOnly": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "moduleResolution": "node" + "moduleResolution": "node", + "paths": { + "display-fetch_stream": ["./src/display/fetch_stream"], + "display-l10n_utils": ["./web/l10n_utils"], + "display-network": ["./src/display/network"], + "display-node_stream": ["./src/display/node_stream"], + "display-node_utils": ["./src/display/node_utils"], + "display-svg": ["./src/display/svg"], + "pdfjs-lib": ["./src/pdf"] + } }, "files": [ "src/pdf.js", From 46f2c5893db2944f868789383170ac5c4534ccb6 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 31 Aug 2023 15:26:31 +0200 Subject: [PATCH 3/3] Revert "Temporarily stop running `gulp typestest` in GitHub Actions" This reverts commit d9350c38997b31ee9f0058f438b332997eb53e07. --- gulpfile.mjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gulpfile.mjs b/gulpfile.mjs index b587d99aa..d3c77e210 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -2469,6 +2469,7 @@ gulp.task( "ci-test", gulp.series( gulp.parallel("lint", "externaltest", "unittestcli"), - "lint-chromium" + "lint-chromium", + "typestest" ) );