From 535a9423db4327bbde26e07898032fae6e1776de Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 13 Dec 2023 19:51:50 +0200 Subject: [PATCH] refactor(api): generate types next to modules" this fixes an issue with `moduleResolution: node` --- tooling/api/package.json | 9 ++++++- tooling/api/rollup.config.ts | 51 ++++-------------------------------- 2 files changed, 13 insertions(+), 47 deletions(-) diff --git a/tooling/api/package.json b/tooling/api/package.json index 95b9b48b0625..22c4cf0b7f1e 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 29fbbcb3a171..49f1f23f6957 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 @@ -57,7 +50,7 @@ export default defineConfig([ plugins: [ typescript({ declaration: true, - declarationDir: './dist/types', + declarationDir: './dist', rootDir: 'src' }), makeFlatPackageInDist() @@ -87,44 +80,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) => { - let temp: Record< - string, - { types: string; import: string; require: string } - > = {} - let key = `./${mod}` - if (mod === 'index') { - key = '.' - } - - temp[key] = { - types: `./types/${mod}.d.ts`, - import: `./${mod}.js`, - require: `./${mod}.cjs` - } - return temp - }), - // 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}`) + ) } } }