Skip to content

Commit ef21b68

Browse files
authored
refactor(api): generate types next to modules (#8391)
* refactor(api): generate types next to modules" this fixes an issue with `moduleResolution: node` * change file * Update api-moduleresolution-node.md
1 parent db12777 commit ef21b68

3 files changed

Lines changed: 18 additions & 47 deletions

File tree

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 & 46 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
@@ -57,7 +50,7 @@ export default defineConfig([
5750
plugins: [
5851
typescript({
5952
declaration: true,
60-
declarationDir: './dist/types',
53+
declarationDir: './dist',
6154
rootDir: 'src'
6255
}),
6356
makeFlatPackageInDist()
@@ -87,44 +80,10 @@ function makeFlatPackageInDist(): Plugin {
8780
return {
8881
name: 'makeFlatPackageInDist',
8982
writeBundle() {
90-
// append our api modules to `exports` in `package.json` then write it to `./dist`
91-
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
92-
const mods = modules.map((p) => basename(p).split('.')[0])
93-
94-
const outputPkg = {
95-
...pkg,
96-
devDependencies: {},
97-
exports: Object.assign(
98-
{},
99-
...mods.map((mod) => {
100-
let temp: Record<
101-
string,
102-
{ types: string; import: string; require: string }
103-
> = {}
104-
let key = `./${mod}`
105-
if (mod === 'index') {
106-
key = '.'
107-
}
108-
109-
temp[key] = {
110-
types: `./types/${mod}.d.ts`,
111-
import: `./${mod}.js`,
112-
require: `./${mod}.cjs`
113-
}
114-
return temp
115-
}),
116-
// if for some reason in the future we manually add something in the `exports` field
117-
// this will ensure it doesn't get overwritten by the logic above
118-
{ ...(pkg.exports || {}) }
119-
)
120-
}
121-
writeFileSync(
122-
'dist/package.json',
123-
JSON.stringify(outputPkg, undefined, 2)
124-
)
125-
12683
// copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
127-
fg.sync('(LICENSE*|*.md)').forEach((f) => copyFileSync(f, `dist/${f}`))
84+
fg.sync('(LICENSE*|*.md|package.json)').forEach((f) =>
85+
copyFileSync(f, `dist/${f}`)
86+
)
12887
}
12988
}
13089
}

0 commit comments

Comments
 (0)