eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
https://eta.js.org
MIT License
1.35k stars 60 forks source link

Cannot import "browser" version from ESM package #283

Open nfarina opened 2 months ago

nfarina commented 2 months ago

Describe the bug

My package is 100% ESM, and I'm using Vite for development and production builds.

To Reproduce Create any blank Vite project and add+import Eta

Expected behavior browser.module.mjs should be used instead of eta.module.mjs

Screenshots

vite v5.2.8 building for production...
[plugin:vite:resolve] [plugin vite:resolve] Module "node:path" has been externalized for browser compatibility, imported by "/Users/nick/Code/burnside-web/node_modules/eta/dist/eta.module.mjs". See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

Package & Environment Details

Additional context This was tackled in #146, and the suggested workaround was detailed in #242, namely to use:

import { Eta } from "eta/dist/browser.module.mjs";

However, this does not work in an ESM project that supports the exports keyword in package.json - since this import isn't explicitly defined there!

This modified exports key in package.json fixes the issue for me:

  "exports": {
    ".": {
      "types": "./dist/types/index.d.ts",
      "import": "./dist/eta.module.mjs",
      "browser": "./dist/browser.umd.js",
      "require": "./dist/eta.umd.js",
      "default": "./dist/eta.umd.js"
    },
    "./browser": {
      "types": "./dist/types/index.d.ts",
      "default": "./dist/browser.module.mjs"
    }
  },

With this, I can write:

import { Eta } from "eta/browser";

And everything works as expected, including types.