Skip to content

Commit 8c3eff0

Browse files
committed
TS: emit type declarations for .ts files with external exports, and publish them under dist/types
This change makes it possible to easily expose types from internal .ts files. This is achieved by adding a TS compilation step which generates .d.ts files from .ts ones. Additionally, a copying step is needed for existing .d.ts files, which are not automatically moved to the outDir by tsc. The dist types will still only include those exported by the index files. Other tried approaches (to e.g. avoid the manual copying step) that were not viable: - generating a single .d.ts bundle is not supported by tsc, and other tools/plugins are either deprecated or (in the case of `rollup-plugin-dts`) failed to process our source code due to unsupported JS syntax - relying on the `rollup-typescript` plugin to emit declarations felt overly messy to configure and potentially unreliable as it generated declarations for .js files as well - renaming existing .d.ts to .ts (alongside .js ones) is not an option since it results in an empty output for the corresponding modules on Rollup compilation, as the .js source files are ignored.
1 parent e0943b1 commit 8c3eff0

File tree

5 files changed

+256
-13
lines changed

5 files changed

+256
-13
lines changed

package-lock.json

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

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,32 @@
2121
},
2222
"exports": {
2323
".": {
24-
"types": "./openpgp.d.ts",
24+
"types": "./dist/types/index.d.ts",
2525
"browser": "./dist/openpgp.min.mjs",
2626
"import": "./dist/node/openpgp.mjs",
2727
"require": "./dist/node/openpgp.min.cjs"
2828
},
2929
"./lightweight": {
30-
"types": "./openpgp.d.ts",
30+
"types": "./dist/types/index.d.ts",
3131
"browser": "./dist/lightweight/openpgp.min.mjs"
3232
}
3333
},
34-
"types": "openpgp.d.ts",
34+
"types": "dist/types/index.d.ts",
3535
"type": "module",
3636
"directories": {
3737
"lib": "src"
3838
},
3939
"files": [
4040
"dist/",
41-
"lightweight/",
42-
"openpgp.d.ts",
43-
"**/*.d.ts"
41+
"lightweight/"
4442
],
4543
"scripts": {
4644
"build": "rollup --config",
45+
"build-types": "rm -rf dist/types && tsc --project tsconfig.dist.json && cpy 'src/**/*.d.ts' dist/types",
4746
"build-test": "npm run build -- --config-build-only=test",
48-
"prepare": "npm run build",
47+
"prepare": "npm run build && npm run build-types",
4948
"test": "mocha --timeout 120000 test/unittests.js",
50-
"test-type-definitions": "tsc --project test/typescript/tsconfig.test.json && tsx test/typescript/definitions.ts",
49+
"test-type-definitions": "npm run build-types && tsc --project test/typescript/tsconfig.test.json && tsx test/typescript/definitions.ts",
5150
"benchmark-time": "node test/benchmarks/time.js",
5251
"benchmark-memory-usage": "node test/benchmarks/memory_usage.js",
5352
"prebrowsertest": "npm run build-test",
@@ -96,6 +95,7 @@
9695
"c8": "^10.1.3",
9796
"chai": "^6.2.1",
9897
"chai-as-promised": "^8.0.2",
98+
"cpy-cli": "^6.0.0",
9999
"eckey-utils": "^0.7.14",
100100
"eslint": "^9.38.0",
101101
"eslint-import-resolver-typescript": "^4.4.4",

rollup.config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ const nodeBuild = {
7878
exportConditions: ['node'] // needed for resolution of noble-curves import of '@noble/crypto' in Node 18
7979
}),
8080
typescript({
81-
compilerOptions: { outDir: './dist/node' } // temporary output location, needed to avoid js files being overwritten under `src`
81+
compilerOptions: { outDir: './dist/node' }, // temporary output location, needed to avoid js files being overwritten under `src`
82+
rootDir: 'src' // ignore .ts files outside of src folder
8283
}),
8384
commonjs(),
8485
replace({
@@ -102,7 +103,8 @@ const fullBrowserBuild = {
102103
browser: true
103104
}),
104105
typescript({
105-
compilerOptions: { outDir: './dist' } // temporary output location, needed to avoid js files being overwritten under `src`
106+
compilerOptions: { outDir: './dist' }, // temporary output location, needed to avoid js files being overwritten under `src`
107+
rootDir: 'src' // ignore .ts files outside of src folder
106108
}),
107109
commonjs({
108110
ignore: nodeBuiltinModules.concat(nodeDependencies)
@@ -129,7 +131,8 @@ const lightweightBrowserBuild = {
129131
browser: true
130132
}),
131133
typescript({
132-
compilerOptions: { outDir: './dist/lightweight' }
134+
compilerOptions: { outDir: './dist/lightweight' },
135+
rootDir: 'src' // ignore .ts files outside of src folder
133136
}),
134137
commonjs({
135138
ignore: nodeBuiltinModules.concat(nodeDependencies)

openpgp.d.ts renamed to src/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
*/
1111

1212
import type { WebStream as GenericWebStream, NodeWebStream as GenericNodeWebStream } from '@openpgp/web-stream-tools';
13-
import enums from './src/enums';
14-
import config, { type Config, type PartialConfig } from './src/config';
13+
import enums from './enums';
14+
import config, { type Config, type PartialConfig } from './config';
15+
16+
export { GrammarError } from './packet/grammar';
1517

1618
export { enums, config, Config, PartialConfig };
1719

tsconfig.dist.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// tsconfig taking care of generating declaration files to be released
2+
// under 'dist/types'
3+
{
4+
"extends": "./tsconfig.json",
5+
"compilerOptions": {
6+
"declaration": true,
7+
"emitDeclarationOnly": true,
8+
"declarationDir": "./dist/types",
9+
"rootDir": "src"
10+
},
11+
"files": ["src/index.d.ts"]
12+
}

0 commit comments

Comments
 (0)