Skip to content

Add ESM named exports#115

Open
ocavue wants to merge 9 commits intoronkok:mainfrom
ocavue-forks:ocavue/esm-export
Open

Add ESM named exports#115
ocavue wants to merge 9 commits intoronkok:mainfrom
ocavue-forks:ocavue/esm-export

Conversation

@ocavue
Copy link
Copy Markdown

@ocavue ocavue commented Feb 15, 2026

This pull request introduces ESM named exports, similar to KaTeX/KaTeX#3992.

This allows users to import the API directly:

// before
import Temml from 'temml';
const { renderToString } = Temml
// after
import { renderToString } from 'temml';

Named exports are also beneficial for tree shaking, helping to reduce code size in production applications.


Additionally, this pull request includes some fixes for the .d.ts file. For example, the previous .d.ts file mentions a function call generateParseTree, but in reality, this function has been renamed to __parse in the JavaScript implementation.

I believe this is because you want to import the type @type {import('./temml').generateParseTree} in the .js file. Nowadays, typescript allows users to write types in .js files as JSDoc comments, which can then generate .d.ts files using .js files as the source of truth. If you don't mind adding a few more dependencies (e.g., typescript and tsdown), I can open a separate pull request to move the type definitions into the .js files and generate .d.ts files based on that.

@ronkok
Copy link
Copy Markdown
Owner

ronkok commented Mar 16, 2026

@ocavue, The desire to enable import { renderToString } from 'temml'; is a goal that I can support. But when I try to build from this code, I am getting warnings from Rollup:

temml.js is using named and default exports together.
Consumers of your bundle will have to use chunk.default to access their default export,
which may not be what you want. Use `output.exports: "named"` to disable this warning.

This PR must not be a breaking change. Existing scripts that call Temml must continue to work. Do you have any comments or changes to suggest?

@ocavue ocavue force-pushed the ocavue/esm-export branch from db9f2bf to 68c20e7 Compare March 23, 2026 08:47
@ocavue
Copy link
Copy Markdown
Author

ocavue commented Mar 23, 2026

@ronkok

In the ESM file, you can have both default export export default x and named export export { y }. That's not really possible for a CommonJS file. One of the solutions is using the __esModule: true property to "simulate" that in CJS, which requires extra configuration on the user's side.

See the following links if you want to learn more:

https://rollupjs.org/configuration-options/#output-esmodule
https://rollupjs.org/configuration-options/#output-exports

temml has three builds: ESM, CJS, and IIFE.

I've updated the code in this PR so that only the ESM output is updated. CJS and IIFE are not affected. ESM (utils/temml.mjs) has both default export and named exports. CJS (utils/temml.cjs) and IIFE (test/temml.js, site/assets/temml.min.js) still have only one default export.

No __esModule: true property is added in this PR.

This ensures that we don't have any breaking changes for all users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants