From 0f5e9b7ec6583b58769b6fe96f1b1d57eee0bb26 Mon Sep 17 00:00:00 2001 From: Kobi Meirson Date: Fri, 9 Aug 2024 12:27:19 +0300 Subject: [PATCH] fix: bundlers not using the `browser` export MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bundlers use _conditions_ to decide which file to import. The logic, [explained by esbuild](https://esbuild.github.io/api/#how-conditions-work) emphasizes it would read the `exports` field *in-order* and try matching it to any of the conditions. When bundling for `browser`, eslint will end up adding `default` and `import` to the list of _conditions_. When iterating `eta`'s `exports` list, it ended up catching `import` instead of `browser`. With this change, it will first catch `browser`. How to test? --- Create a new file named `index.mjs` with the following content: ```javascript import { Eta } from 'eta'; const eta = new Eta(); ``` Install `esbuild` and `eta`: ```sh npm i --save-dev esbuild eta ``` Try to bundle the file: ```sh npx esbuild index.mjs --bundle --conditions=browser ``` With the current published version (3.4.1) it will emit an error: ``` ✘ [ERROR] Could not resolve "node:path" node_modules/eta/dist/eta.module.mjs:1:22: 1 │ import * as path from 'node:path'; ╵ ~~~~~~~~~~~ The package "node:path" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "--platform=node" to do that, which will remove this error. ``` When trying with this branch's build, it passes successfully. resolves eta-dev/eta#283 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0514085..cb7092f 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "source": "src/index.ts", "exports": { "types": "./dist/types/index.d.ts", - "import": "./dist/eta.module.mjs", "browser": "./dist/browser.umd.js", + "import": "./dist/eta.module.mjs", "require": "./dist/eta.umd.js", "default": "./dist/eta.umd.js" },