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]'`  |
This commit is contained in:
ylemkimon
2021-10-31 04:23:56 +09:00
committed by GitHub
parent a28b2f084b
commit 15ee9b4a5a
3 changed files with 46 additions and 14 deletions

View File

@@ -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

View File

@@ -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}');
```

View File

@@ -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",