From 31978526a0ae5d6166d4eae580308c9bd0bef1b2 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Mon, 2 Sep 2024 11:59:39 -0700 Subject: [PATCH] improve bundler integration --- packages/jimp/package.json | 21 ++++++++++++--------- packages/jimp/rollup.config.js | 26 +++++++++++++++++++++++++- packages/jimp/src/index-browser.mts | 1 + 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 packages/jimp/src/index-browser.mts diff --git a/packages/jimp/package.json b/packages/jimp/package.json index 0b800575..11cf474d 100644 --- a/packages/jimp/package.json +++ b/packages/jimp/package.json @@ -95,19 +95,22 @@ "exclude": [ "**/*.test.ts" ], + "esmDialects": [ + "browser" + ], "exports": { "./package.json": "./package.json", ".": "./src/index.ts", - "./fonts": "./src/fonts.ts", - "./browser": { - "types": "./dist/esm/index.d.ts", - "import": "./browser.js" - } + "./fonts": "./src/fonts.ts" } }, "exports": { "./package.json": "./package.json", ".": { + "browser": { + "types": "./dist/browser/index.d.ts", + "default": "./dist/browser/index.js" + }, "import": { "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.js" @@ -118,6 +121,10 @@ } }, "./fonts": { + "browser": { + "types": "./dist/browser/fonts.d.ts", + "default": "./dist/browser/fonts.js" + }, "import": { "types": "./dist/esm/fonts.d.ts", "default": "./dist/esm/fonts.js" @@ -126,10 +133,6 @@ "types": "./dist/commonjs/fonts.d.ts", "default": "./dist/commonjs/fonts.js" } - }, - "./browser": { - "types": "./dist/esm/index.d.ts", - "import": "./browser.js" } }, "main": "./dist/commonjs/index.js", diff --git a/packages/jimp/rollup.config.js b/packages/jimp/rollup.config.js index cc54eda3..cc2da84c 100644 --- a/packages/jimp/rollup.config.js +++ b/packages/jimp/rollup.config.js @@ -5,6 +5,25 @@ import alias from "@rollup/plugin-alias"; import nodePolyfills from "rollup-plugin-polyfill-node"; import inject from "@rollup/plugin-inject"; import terser from "@rollup/plugin-terser"; +import { dts } from "rollup-plugin-dts"; + +import { promises as fs } from "fs"; + +async function maybeUnlink(path) { + try { + await fs.unlink(path); + } catch (error) { + if (error.code !== "ENOENT") { + throw error; + } + } +} + +// Remove tshy's output +await maybeUnlink("dist/browser/index-browser.d.mts.map"); +await maybeUnlink("dist/browser/index-browser.mjs.map"); +await maybeUnlink("dist/browser/index.d.ts"); +await maybeUnlink("dist/browser/index.js"); function injectCodePlugin(code) { return { @@ -62,11 +81,16 @@ export default [ ], output: [ { - file: `browser.js`, + file: "dist/browser/index.js", format: "es", sourcemap: true, inlineDynamicImports: true, }, ], }, + { + input: `src/index.ts`, + output: [{ file: "dist/browser/index.d.ts", format: "es" }], + plugins: [dts()], + }, ]; diff --git a/packages/jimp/src/index-browser.mts b/packages/jimp/src/index-browser.mts new file mode 100644 index 00000000..beef1397 --- /dev/null +++ b/packages/jimp/src/index-browser.mts @@ -0,0 +1 @@ +// A stub. The real build is done by rollup.