Comparing version 1.2.0 to 1.3.0
@@ -1,2 +0,2 @@ | ||
export { compile, compileSync } from "./lib/core.js"; | ||
export { compile, compileSync, createProcessor } from "./lib/core.js"; | ||
export { evaluate, evaluateSync } from "./lib/evaluate.js"; |
@@ -1,2 +0,2 @@ | ||
export {compile, compileSync} from './lib/core.js' | ||
export {compile, compileSync, createProcessor} from './lib/core.js' | ||
export {evaluate, evaluateSync} from './lib/evaluate.js' |
@@ -35,2 +35,13 @@ /** | ||
export function compileSync(file: VFileCompatible, options?: ProcessorOptions): VFile; | ||
/** | ||
* Pipeline to: | ||
* | ||
* 1. Parse MDX (serialized markdown with embedded JSX, ESM, and expressions) | ||
* 2. Transform through remark (mdast), rehype (hast), and recma (esast) | ||
* 3. Serialize as JavaScript | ||
* | ||
* @param {ProcessorOptions} [options] | ||
* @return {Processor} | ||
*/ | ||
export function createProcessor(options?: ProcessorOptions): Processor; | ||
export type VFileCompatible = string | import("vfile").VFile | import("vfile").VFileOptions | Uint8Array; | ||
@@ -37,0 +48,0 @@ export type VFile = import("vfile").VFile; |
@@ -63,3 +63,3 @@ import unified from 'unified' | ||
*/ | ||
function createProcessor(options = {}) { | ||
export function createProcessor(options = {}) { | ||
var { | ||
@@ -66,0 +66,0 @@ _contain: contain, |
import {promises as fs} from 'fs' | ||
import {compile} from './core.js' | ||
import {createProcessor} from './core.js' | ||
@@ -18,2 +18,4 @@ var eol = /\r\n|\r|\n|\u2028|\u2029/g | ||
export function esbuild(options) { | ||
var processor = createProcessor(options) | ||
return {name: 'esbuild-xdm', setup} | ||
@@ -32,5 +34,15 @@ | ||
var contents | ||
var message | ||
var start | ||
var end | ||
var list | ||
var length | ||
var lineStart | ||
var lineEnd | ||
var match | ||
var line | ||
var column | ||
try { | ||
file = await compile({contents: doc, path: data.path}, options) | ||
file = await processor.process({contents: doc, path: data.path}) | ||
contents = file.contents | ||
@@ -43,11 +55,10 @@ messages = file.messages | ||
messages.forEach(function (message) { | ||
var {start, end} = message.location | ||
var list = message.fatal ? errors : warnings | ||
var length = 0 | ||
var lineStart = 0 | ||
var lineEnd | ||
var match | ||
var line | ||
var column | ||
for (message of messages) { | ||
start = message.location.start | ||
end = message.location.end | ||
list = message.fatal ? errors : warnings | ||
length = 0 | ||
lineStart = 0 | ||
line = undefined | ||
column = undefined | ||
@@ -80,3 +91,3 @@ if (start.line != null && start.column != null && start.offset != null) { | ||
}) | ||
}) | ||
} | ||
@@ -83,0 +94,0 @@ // For some reason, on Erbium, c8 is missing the following two lines. |
@@ -36,7 +36,9 @@ import u from 'unist-builder' | ||
var replacement = [] | ||
var pragmas = [] | ||
var layout | ||
var content | ||
var child | ||
var declaration | ||
var specifier | ||
var pragmas = [] | ||
if (jsxRuntime) { | ||
@@ -83,3 +85,3 @@ pragmas.push('@jsxRuntime ' + jsxRuntime) | ||
// (import/exports) as they are. | ||
tree.body.forEach((child) => { | ||
for (child of tree.body) { | ||
// ```js | ||
@@ -200,10 +202,13 @@ // export default props => <>{props.children}</> | ||
else { | ||
child.declaration.declarations.forEach((d) => { | ||
exportedIdentifiers.push(d.id.name) | ||
}) | ||
for (declaration of child.declaration.declarations) { | ||
exportedIdentifiers.push(declaration.id.name) | ||
} | ||
} | ||
} else { | ||
child.specifiers.forEach((d) => { | ||
exportedIdentifiers.push([d.local.name, d.exported.name]) | ||
}) | ||
for (specifier of child.specifiers) { | ||
exportedIdentifiers.push([ | ||
specifier.local.name, | ||
specifier.exported.name | ||
]) | ||
} | ||
} | ||
@@ -230,3 +235,3 @@ } else if ( | ||
} | ||
}) | ||
} | ||
@@ -233,0 +238,0 @@ // If there was no JSX content at all, add an empty function. |
@@ -126,2 +126,3 @@ import isIdentifierName from 'estree-util-is-identifier-name' | ||
var scope | ||
var name | ||
@@ -135,3 +136,3 @@ if ( | ||
scope.tags.forEach((name) => { | ||
for (name of scope.tags) { | ||
defaults.push( | ||
@@ -144,5 +145,5 @@ u('Property', { | ||
) | ||
}) | ||
} | ||
scope.components.forEach((name) => { | ||
for (name of scope.components) { | ||
if (name !== 'MDXLayout') { | ||
@@ -171,5 +172,5 @@ defaults.push( | ||
) | ||
}) | ||
} | ||
scope.objects.forEach((name) => { | ||
for (name of scope.objects) { | ||
actual.push( | ||
@@ -183,3 +184,3 @@ u('Property', { | ||
) | ||
}) | ||
} | ||
@@ -186,0 +187,0 @@ if (defaults.length > 0 || actual.length > 0) { |
{ | ||
"name": "xdm", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "an MDX compiler", | ||
@@ -32,5 +32,8 @@ "license": "MIT", | ||
"index.d.ts", | ||
"rollup.js", | ||
"rollup.d.ts", | ||
"webpack.cjs" | ||
], | ||
"dependencies": { | ||
"@rollup/pluginutils": "^4.0.0", | ||
"astring": "^1.6.0", | ||
@@ -94,3 +97,3 @@ "estree-util-build-jsx": "^1.1.0", | ||
"webpack": "^5.0.0", | ||
"xo": "^0.37.0" | ||
"xo": "^0.38.0" | ||
}, | ||
@@ -123,3 +126,5 @@ "scripts": { | ||
"rules": { | ||
"complexity": "off", | ||
"import/no-mutable-exports": "off", | ||
"max-depth": "off", | ||
"new-cap": "off", | ||
@@ -126,0 +131,0 @@ "no-var": "off", |
122
readme.md
@@ -16,3 +16,3 @@ # xdm | ||
maps, ESM only, defaulting to an automatic JSX runtime, no Babel, smallish | ||
browser size, more docs, esbuild plugin). | ||
browser size, more docs, esbuild and Rollup plugins). | ||
@@ -51,2 +51,3 @@ ## Install | ||
* [`evaluateSync(file, options)`](#evaluatesyncfile-options) | ||
* [`createProcessor(options)`](#createprocessoroptions) | ||
* [MDX syntax](#mdx-syntax) | ||
@@ -200,11 +201,15 @@ * [Markdown](#markdown) | ||
[`compileSync`](#compilesyncfile-options), | ||
[`evaluate`][eval], and | ||
[`evaluateSync`](#evaluatesyncfile-options). | ||
[`evaluate`][eval], | ||
[`evaluateSync`](#evaluatesyncfile-options), and | ||
[`createProcessor`](#createprocessoroptions). | ||
There is no default export. | ||
`xdm/esbuild.js` exports a function as the default export that returns an | ||
[esbuild][] plugin. | ||
`xdm/rollup.js` exports a function as the default export that returns a | ||
[Rollup][] plugin. | ||
`xdm/webpack.cjs` exports a [webpack][] loader as the default export. | ||
`xdm/esbuild.js` exports a function that returns an esbuild plugin as the | ||
default export. | ||
### `compile(file, options?)` | ||
@@ -275,2 +280,8 @@ | ||
###### `options.recmaPlugins` | ||
List of recma plugins, presets, and pairs. | ||
This is a new ecosystem, currently in beta, to transform | ||
[esast](https://github.com/syntax-tree/esast) (JavaScript) trees. | ||
###### `options.SourceMapGenerator` | ||
@@ -549,3 +560,3 @@ | ||
To get the full power of MDX it’s suggested to use `compile`, write to a file, | ||
and then run with Node or bundle with esbuild/Rollup/webpack. | ||
and then run with Node or bundle with [esbuild][]/[Rollup][]/[webpack][]. | ||
But if you trust your content and know that it doesn’t contain imports, | ||
@@ -582,2 +593,4 @@ `evaluate` can work. | ||
###### `options.recmaPlugins` | ||
Same as for [`compile`][compile]. | ||
@@ -633,2 +646,8 @@ | ||
### `createProcessor(options)` | ||
Create a unified processor to compile MDX to JS. | ||
Has the same options as [`compile`][compile], but returns a configured | ||
[`processor`](https://github.com/unifiedjs/unified#processor). | ||
## MDX syntax | ||
@@ -705,3 +724,3 @@ | ||
These are many more things possible by configuring | ||
[remark plugins][remark-plugins] (and [rehype plugins][rehype-plugins]). | ||
[remark plugins][remark-plugins] and [rehype plugins][rehype-plugins]. | ||
@@ -789,5 +808,5 @@ #### Caveats | ||
Note that when using [`evaluate`][eval], `imports` are not supported but exports | ||
can still be used to define things in MDX (except `export … from`, which also | ||
imports). | ||
Note that when using [`evaluate`][eval], `import`s are not supported but | ||
`export`s can still be used to define things in MDX (except `export … from`, | ||
which also imports). | ||
@@ -917,3 +936,3 @@ ### Expressions | ||
Install `xdm` and use it directly. | ||
Install `xdm` and use `xdm/rollup.js`. | ||
Add something along these lines to your `rollup.config.js`: | ||
@@ -923,3 +942,3 @@ | ||
import path from 'path' | ||
import {compile} from 'xdm' | ||
import xdm from 'xdm/rollup.js' | ||
@@ -930,9 +949,3 @@ export default { | ||
// … | ||
{ | ||
async transform(contents, fp) { | ||
if (path.extname(fp) !== '.mdx') return null | ||
var file = await compile({contents, path: fp}, {/* Options… */}) | ||
return {code: file.contents, map: file.map} | ||
} | ||
} | ||
xdm({/* Options… */}) | ||
] | ||
@@ -942,4 +955,2 @@ } | ||
Source maps are supported when [`SourceMapGenerator`][sm] is passed in. | ||
If you use modern JavaScript features you might want to use Babel through | ||
@@ -957,9 +968,3 @@ [`@rollup/plugin-babel`](https://github.com/rollup/plugins/tree/master/packages/babel) | ||
// … | ||
{ | ||
async transform(contents, fp) { | ||
if (path.extname(fp) !== '.mdx') return null | ||
var file = await compile({contents, path: fp}, {/* Options… */}) | ||
return {code: file.contents, map: file.map} | ||
} | ||
}, | ||
xdm({/* Options… */}), | ||
babel({ | ||
@@ -974,2 +979,17 @@ // Also run on what used to be `.mdx` (but is now JS): | ||
Source maps are supported when [`SourceMapGenerator`][sm] is passed in. | ||
`options` are the same as from [`compile`][compile], with the additions of: | ||
###### `options.extensions` | ||
List of extensions to support (`Array.<string>`, default: `['.mdx']`). | ||
###### `options.include` | ||
###### `options.exclude` | ||
List of [`picomatch`][pico] patterns to include and/or exclude | ||
(`string`, `RegExp`, `Array.<string|RegExp>`, default: `[]`). | ||
#### Webpack | ||
@@ -1015,30 +1035,10 @@ | ||
be extended. | ||
Install `xdm` and add something along these lines to your `snowpack.config.js`: | ||
Unfortunately, `snowpack.config.js` is currently, ironically, CommonJS. | ||
So figuring out a way to `import('xdm/rollup.js')` and use it in Snowpack, is | ||
left as an exercise to the reader. | ||
```js | ||
var path = require('path') | ||
var {compile} = require('xdm') | ||
module.exports = { | ||
// … | ||
packageOptions: { | ||
rollup: { | ||
plugins: [ | ||
{ | ||
async transform(contents, fp) { | ||
if (path.extname(fp) !== '.mdx') return null | ||
var file = await compile({contents, path: fp}, {/* Options… */}) | ||
return {code: file.contents, map: file.map} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
#### Vite | ||
[Vite](https://vitejs.dev) supports [Rollup][] plugins directly in `plugins` in | ||
your `vite.config.js`: | ||
your `vite.config.js`. | ||
@@ -1869,3 +1869,4 @@ #### WMR | ||
**xdm** is a unified pipeline — wrapped so that most folks don’t need to know | ||
about unified: [`core.js#L76-L101`](https://github.com/wooorm/xdm/blob/e4c2340b41d3354617aa42350306fd35cb57967d/lib/core.js#L76-L101). | ||
about unified: | ||
[`core.js#L76-L102`](https://github.com/wooorm/xdm/blob/main/lib/core.js#L76-L102). | ||
The processor goes through these steps: | ||
@@ -1880,3 +1881,4 @@ | ||
6. Do the work needed to get a component | ||
7. Serialize esast as JavaScript | ||
7. Transform through recma (JS ecosystem) | ||
8. Serialize esast as JavaScript | ||
@@ -1922,4 +1924,4 @@ The *input* is MDX (serialized markdown with embedded JSX, ESM, and | ||
Then we go to JavaScript: esast (JS; an AST which is compatible with estree but | ||
looks a bit more like other unist ASTs). | ||
Then we go to JavaScript: [esast](https://github.com/syntax-tree/esast) (JS; an | ||
AST which is compatible with estree but looks a bit more like other unist ASTs). | ||
This transformation is done by | ||
@@ -2027,6 +2029,8 @@ [`hast-util-to-estree`](https://github.com/syntax-tree/hast-util-to-estree). | ||
[webpack]: #webpack | ||
[esbuild]: #esbuild | ||
[rollup]: #rollup | ||
[webpack]: #webpack | ||
[caveats]: #caveats | ||
@@ -2037,1 +2041,3 @@ | ||
[acorn]: https://github.com/acornjs/acorn | ||
[pico]: https://github.com/micromatch/picomatch#globbing-features |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118903
38
1832
2023
17
+ Added@rollup/pluginutils@^4.0.0
+ Added@rollup/pluginutils@4.2.1(transitive)
+ Addedpicomatch@2.3.1(transitive)