From 1c582a942e345a066b65620e4db9f688ec142bb9 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 14 Dec 2023 18:27:48 +0200 Subject: [PATCH] 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 --- .changes/api-module-resolution-node.md | 5 +++ tooling/api/package.json | 9 ++++- tooling/api/rollup.config.ts | 47 +++----------------------- 3 files changed, 18 insertions(+), 43 deletions(-) create mode 100644 .changes/api-module-resolution-node.md diff --git a/.changes/api-module-resolution-node.md b/.changes/api-module-resolution-node.md new file mode 100644 index 000000000..26e96bf91 --- /dev/null +++ b/.changes/api-module-resolution-node.md @@ -0,0 +1,5 @@ +--- +"@tauri-apps/api": "patch:bug" +--- + +Fix a regression where typescript could not find types when using `"moduleResolution": "node"` diff --git a/tooling/api/package.json b/tooling/api/package.json index 3a3a2ea7a..235de4e0e 100644 --- a/tooling/api/package.json +++ b/tooling/api/package.json @@ -19,10 +19,17 @@ }, "homepage": "https://github.com/tauri-apps/tauri#readme", "type": "module", - "types": "./types/index.d.ts", "main": "./index.cjs", "module": "./index.js", "exports": { + ".": { + "import": "./index.js", + "require": "./index.cjs" + }, + "./*": { + "import": "./*.js", + "require": "./*.cjs" + }, "./package.json": "./package.json" }, "scripts": { diff --git a/tooling/api/rollup.config.ts b/tooling/api/rollup.config.ts index c440f7afa..ae1786d3d 100644 --- a/tooling/api/rollup.config.ts +++ b/tooling/api/rollup.config.ts @@ -7,14 +7,7 @@ import typescript from '@rollup/plugin-typescript' import terser from '@rollup/plugin-terser' import fg from 'fast-glob' import { basename, join } from 'path' -import { - writeFileSync, - copyFileSync, - opendirSync, - rmSync, - Dir, - readFileSync -} from 'fs' +import { copyFileSync, opendirSync, rmSync, Dir } from 'fs' import { fileURLToPath } from 'url' // cleanup dist dir @@ -45,7 +38,7 @@ export default defineConfig([ plugins: [ typescript({ declaration: true, - declarationDir: './dist/types', + declarationDir: './dist', rootDir: 'src' }), makeFlatPackageInDist() @@ -75,40 +68,10 @@ function makeFlatPackageInDist(): Plugin { return { name: 'makeFlatPackageInDist', writeBundle() { - // append our api modules to `exports` in `package.json` then write it to `./dist` - const pkg = JSON.parse(readFileSync('package.json', 'utf8')) - const mods = modules.map((p) => basename(p).split('.')[0]) - - const outputPkg = { - ...pkg, - devDependencies: {}, - exports: Object.assign( - {}, - ...mods.map((mod) => { - const exports: Record< - string, - { types: string; import: string; require: string } - > = {} - const key = mod === 'index' ? '.' : `./${mod}` - exports[key] = { - types: `./types/${mod}.d.ts`, - import: `./${mod}.js`, - require: `./${mod}.cjs` - } - return exports - }), - // if for some reason in the future we manually add something in the `exports` field - // this will ensure it doesn't get overwritten by the logic above - { ...(pkg.exports || {}) } - ) - } - writeFileSync( - 'dist/package.json', - JSON.stringify(outputPkg, undefined, 2) - ) - // copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist` - fg.sync('(LICENSE*|*.md)').forEach((f) => copyFileSync(f, `dist/${f}`)) + fg.sync('(LICENSE*|*.md|package.json)').forEach((f) => + copyFileSync(f, `dist/${f}`) + ) } } }