From 15ee9b4a5a0ccf26ee271577e469f8b83f8b3ff8 Mon Sep 17 00:00:00 2001 From: ylemkimon Date: Sun, 31 Oct 2021 04:23:56 +0900 Subject: [PATCH] feat: conditionally export ECMAScript modules (#3377) * feat: conditionally export ECMAScript modules BREAKING CHANGE: With module loaders that support conditional exports and ECMAScript modules, `import katex from 'katex';` will import the ECMAScript module. You can now use: |Before |After | |------------------------------------------|----------------------------------| |`require('katex/dist/contrib/[name].js')` | `require('katex/contrib/[name]')`| |`import katex from 'katex/dist/katex.mjs'`| `import katex from 'katex'` | |`import 'katex/dist/contrib/[name].mjs'` | `import 'katex/contrib/[name]'` | --- docs/migration.md | 23 +++++++++++++++++------ docs/node.md | 10 ++-------- package.json | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/docs/migration.md b/docs/migration.md index 8f05e50d..06b9908d 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -3,27 +3,38 @@ id: migration title: Migration Guide --- -As of KaTeX 1.0, we've changed how MacroExpander and Parser work in order to close -some gaps between KaTeX and LaTeX and therefore there may be breaking changes. +## v0.14.0 -## Macro arguments +With module loaders that support conditional exports and ECMAScript modules, +`import katex from 'katex';` will import the ECMAScript module. + +You can now use: +|Before |After | +|------------------------------------------|----------------------------------| +|`require('katex/dist/contrib/[name].js')` | `require('katex/contrib/[name]')`| +|`import katex from 'katex/dist/katex.mjs'`| `import katex from 'katex'` | +|`import 'katex/dist/contrib/[name].mjs'` | `import 'katex/contrib/[name]'` | + +## v0.13.0 + +### Macro arguments Tokens will not be expanded while parsing a macro argument. For example, `\frac\foo\foo`, where the `\foo` is defined as `12`, will be parsed as `\frac{12}{12}`, not `\frac{1}{2}12`. To expand the argument before parsing, `\expandafter` can be used like `\expandafter\frac\foo\foo`. -## `\def` +### `\def` `\def` no longer accepts a control sequence enclosed in braces. For example, `\def{\foo}{}` no longer works and should be changed to `\def\foo{}`. It also no longer accepts replacement text not enclosed in braces. For example, `\def\foo1` no longer works and should be changed to `\def\foo{1}`. -## `\newline` and `\cr` +### `\newline` and `\cr` `\newline` and `\cr` no longer takes an optional size argument. To specify vertical spacing, `\\` should be used. -## `\cfrac`, `\color`, `\textcolor`, `\colorbox`, `\fcolorbox` +### `\cfrac`, `\color`, `\textcolor`, `\colorbox`, `\fcolorbox` They are no longer allowed as an argument to primitive commands, such as `\sqrt` (without the optional argument) and super/subscript. For example, `\sqrt\textcolor{red}{x}` no longer works and should be changed to diff --git a/docs/node.md b/docs/node.md index b93da396..81967057 100644 --- a/docs/node.md +++ b/docs/node.md @@ -63,17 +63,11 @@ KaTeX is exported as a CommonJS module, which can be imported using `require`: const katex = require('katex'); ``` -If you're using a module loader, transpiler, or bundler that supports interoperability -between ECMAScript module and CommonJS module, you can use `import`: +KaTeX also conditionally exports an ECMAScript module: ```js import katex from 'katex'; ``` -KaTeX also provides an ECMAScript module: -```js -import katex from 'katex/dist/katex.mjs' -``` - > The ES module contains ES6 syntaxes and features, and may need transpiling to use in old environments. @@ -94,6 +88,6 @@ mhchem in Node as follows: ```js const katex = require('katex'); -require('katex/dist/contrib/mhchem.js'); // modify katex module +require('katex/contrib/mhchem'); // modify katex module const html = katex.renderToString('\\ce{CO2 + C -> 2 C0}'); ``` diff --git a/package.json b/package.json index 541851ba..0408425a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,33 @@ "version": "0.13.24", "description": "Fast math typesetting for the web.", "main": "dist/katex.js", + "exports": { + ".": { + "require": "./dist/katex.js", + "import": "./dist/katex.mjs" + }, + "./contrib/auto-render": { + "require": "./dist/contrib/auto-render.js", + "import": "./dist/contrib/auto-render.mjs" + }, + "./contrib/mhchem": { + "require": "./dist/contrib/mhchem.js", + "import": "./dist/contrib/mhchem.mjs" + }, + "./contrib/copy-tex": { + "require": "./dist/contrib/copy-tex.js", + "import": "./dist/contrib/copy-tex.mjs" + }, + "./contrib/mathtex-script-type": { + "require": "./dist/contrib/mathtex-script-type.js", + "import": "./dist/contrib/mathtex-script-type.mjs" + }, + "./contrib/render-a11y-string": { + "require": "./dist/contrib/render-a11y-string.js", + "import": "./dist/contrib/render-a11y-string.mjs" + }, + "./*": "./*" + }, "homepage": "https://katex.org", "repository": { "type": "git",