Skip to content

Commit 1c582a9

Browse files
authored
refactor(api): generate types next to modules (#8392)
* refactor(api): generate types next to modules" this fixes an issue with `moduleResolution: node` * change file * Update api-module-resolution-node.md
1 parent 3c371aa commit 1c582a9

File tree

3 files changed

+18
-43
lines changed

3 files changed

+18
-43
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": "patch:bug"
3+
---
4+
5+
Fix a regression where typescript could not find types when using `"moduleResolution": "node"`

tooling/api/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@
1919
},
2020
"homepage": "https://github.com/tauri-apps/tauri#readme",
2121
"type": "module",
22-
"types": "./types/index.d.ts",
2322
"main": "./index.cjs",
2423
"module": "./index.js",
2524
"exports": {
25+
".": {
26+
"import": "./index.js",
27+
"require": "./index.cjs"
28+
},
29+
"./*": {
30+
"import": "./*.js",
31+
"require": "./*.cjs"
32+
},
2633
"./package.json": "./package.json"
2734
},
2835
"scripts": {

tooling/api/rollup.config.ts

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ import typescript from '@rollup/plugin-typescript'
77
import terser from '@rollup/plugin-terser'
88
import fg from 'fast-glob'
99
import { basename, join } from 'path'
10-
import {
11-
writeFileSync,
12-
copyFileSync,
13-
opendirSync,
14-
rmSync,
15-
Dir,
16-
readFileSync
17-
} from 'fs'
10+
import { copyFileSync, opendirSync, rmSync, Dir } from 'fs'
1811
import { fileURLToPath } from 'url'
1912

2013
// cleanup dist dir
@@ -45,7 +38,7 @@ export default defineConfig([
4538
plugins: [
4639
typescript({
4740
declaration: true,
48-
declarationDir: './dist/types',
41+
declarationDir: './dist',
4942
rootDir: 'src'
5043
}),
5144
makeFlatPackageInDist()
@@ -75,40 +68,10 @@ function makeFlatPackageInDist(): Plugin {
7568
return {
7669
name: 'makeFlatPackageInDist',
7770
writeBundle() {
78-
// append our api modules to `exports` in `package.json` then write it to `./dist`
79-
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
80-
const mods = modules.map((p) => basename(p).split('.')[0])
81-
82-
const outputPkg = {
83-
...pkg,
84-
devDependencies: {},
85-
exports: Object.assign(
86-
{},
87-
...mods.map((mod) => {
88-
const exports: Record<
89-
string,
90-
{ types: string; import: string; require: string }
91-
> = {}
92-
const key = mod === 'index' ? '.' : `./${mod}`
93-
exports[key] = {
94-
types: `./types/${mod}.d.ts`,
95-
import: `./${mod}.js`,
96-
require: `./${mod}.cjs`
97-
}
98-
return exports
99-
}),
100-
// if for some reason in the future we manually add something in the `exports` field
101-
// this will ensure it doesn't get overwritten by the logic above
102-
{ ...(pkg.exports || {}) }
103-
)
104-
}
105-
writeFileSync(
106-
'dist/package.json',
107-
JSON.stringify(outputPkg, undefined, 2)
108-
)
109-
11071
// copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
111-
fg.sync('(LICENSE*|*.md)').forEach((f) => copyFileSync(f, `dist/${f}`))
72+
fg.sync('(LICENSE*|*.md|package.json)').forEach((f) =>
73+
copyFileSync(f, `dist/${f}`)
74+
)
11275
}
11376
}
11477
}

0 commit comments

Comments
 (0)