mdx-bundler
Advanced tools
Comparing version 5.2.1 to 6.0.0
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export function bundleMDX(mdxSource: string, { files, xdmOptions, esbuildOptions, globals, cwd, grayMatterOptions }?: import('./types').BundleMDXOptions): Promise<{ | ||
export function bundleMDX(mdxSource: string, { files, xdmOptions, esbuildOptions, globals, cwd, grayMatterOptions, }?: import('./types').BundleMDXOptions): Promise<{ | ||
code: string; | ||
@@ -14,5 +14,19 @@ frontmatter: { | ||
errors: esbuild.Message[]; | ||
matter: grayMatter.GrayMatterFile<string>; | ||
matter: grayMatter.GrayMatterFile<any>; | ||
}>; | ||
/** | ||
* | ||
* @param {string} mdxPath - The file path to bundle. | ||
* @param {import('./types').BundleMDXOptions} options | ||
* @returns | ||
*/ | ||
export function bundleMDXFile(mdxPath: string, { files, xdmOptions, esbuildOptions, globals, cwd, grayMatterOptions, }?: import('./types').BundleMDXOptions): Promise<{ | ||
code: string; | ||
frontmatter: { | ||
[key: string]: any; | ||
}; | ||
errors: esbuild.Message[]; | ||
matter: grayMatter.GrayMatterFile<any>; | ||
}>; | ||
import * as esbuild from "esbuild"; | ||
import grayMatter from "gray-matter"; |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.bundleMDX = bundleMDX; | ||
exports.bundleMDXFile = bundleMDXFile; | ||
@@ -67,6 +68,4 @@ var _fs = _interopRequireDefault(require("fs")); | ||
default: xdmESBuild | ||
}] = await Promise.all([await import('xdm/esbuild.js')]); // extract the frontmatter | ||
}] = await Promise.all([await import('xdm/esbuild.js')]); | ||
const matter = (0, _grayMatter.default)(mdxSource, grayMatterOptions({})); | ||
const entryPath = _path.default.join(cwd, `./_mdx_bundler_entry_point-${(0, _uuid.v4)()}.mdx`); | ||
@@ -203,3 +202,15 @@ /** @type Record<string, string> */ | ||
minify: true | ||
}); | ||
}); // Extract the front matter from the source or the entry point | ||
/** @type grayMatter.GrayMatterFile<any> */ | ||
let matter; // We have to be a bit specific here to ensure type safety | ||
if (buildOptions.entryPoints && Array.isArray(buildOptions.entryPoints) && buildOptions.entryPoints[0] !== entryPath) { | ||
//The user has replaced the entrypoint, we can assume this means `mdxSource` is empty | ||
matter = _grayMatter.default.read(buildOptions.entryPoints[0], grayMatterOptions({})); | ||
} else { | ||
matter = (0, _grayMatter.default)(mdxSource, grayMatterOptions({})); | ||
} | ||
const bundled = await esbuild.build(buildOptions); | ||
@@ -237,2 +248,30 @@ | ||
throw new Error("You must either specify `write: false` or `write: true` and `outdir: '/path'` in your esbuild options"); | ||
} | ||
/** | ||
* | ||
* @param {string} mdxPath - The file path to bundle. | ||
* @param {import('./types').BundleMDXOptions} options | ||
* @returns | ||
*/ | ||
async function bundleMDXFile(mdxPath, { | ||
files = {}, | ||
xdmOptions = options => options, | ||
esbuildOptions = options => options, | ||
globals = {}, | ||
cwd, | ||
grayMatterOptions = options => options | ||
} = {}) { | ||
return bundleMDX('', { | ||
files, | ||
xdmOptions, | ||
esbuildOptions: options => { | ||
options.entryPoints = [mdxPath]; | ||
return esbuildOptions(options); | ||
}, | ||
globals, | ||
cwd: cwd ? cwd : _path.default.dirname(mdxPath), | ||
grayMatterOptions | ||
}); | ||
} |
{ | ||
"name": "mdx-bundler", | ||
"version": "5.2.1", | ||
"version": "6.0.0", | ||
"description": "Compile and bundle your MDX files and their dependencies. FAST.", | ||
@@ -43,10 +43,10 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@babel/runtime": "^7.14.6", | ||
"@babel/runtime": "^7.15.3", | ||
"@esbuild-plugins/node-resolve": "^0.1.4", | ||
"@fal-works/esbuild-plugin-global-externals": "^2.1.2", | ||
"gray-matter": "^4.0.3", | ||
"remark-frontmatter": "^3.0.0", | ||
"remark-frontmatter": "^4.0.0", | ||
"remark-mdx-frontmatter": "^1.0.1", | ||
"uuid": "^8.3.2", | ||
"xdm": "^1.12.2" | ||
"xdm": "^2.0.0" | ||
}, | ||
@@ -59,3 +59,3 @@ "peerDependencies": { | ||
"@types/jsdom": "^16.2.13", | ||
"@types/react": "^17.0.14", | ||
"@types/react": "^17.0.17", | ||
"@types/react-dom": "^17.0.9", | ||
@@ -65,5 +65,5 @@ "@types/uuid": "^8.3.1", | ||
"cross-env": "^7.0.3", | ||
"esbuild": "^0.12.15", | ||
"jsdom": "^16.6.0", | ||
"kcd-scripts": "^11.1.0", | ||
"esbuild": "^0.12.20", | ||
"jsdom": "^16.7.0", | ||
"kcd-scripts": "^11.2.0", | ||
"left-pad": "^1.3.0", | ||
@@ -70,0 +70,0 @@ "mdx-test-data": "^1.0.1", |
@@ -596,19 +596,20 @@ <div align="center"> | ||
### Replacing the entry point | ||
### bundleMDXFile | ||
If your MDX file is on your disk you can save some time and code by having | ||
`esbuild` read the file for you. To do this you can override the `entryPoints` | ||
settings in `esbuildOptions` with the path to your mdx source. | ||
`esbuild` read the file for you. To do this mdx-bundler provides the function | ||
`bundleMDXFile` which works the same as `bundleMDX` except it's first option is | ||
the path to the mdx file instead of the mdx source. | ||
```js | ||
const {code, frontmatter} = await bundleMDX('', { | ||
cwd: '/users/you/site/_content/pages', | ||
esbuildOptions: options => { | ||
options.entryPoints = ['/users/you/site/_content/pages/file.mdx'] | ||
import {bundleMDXFile} from 'mdx-bundler' | ||
return options | ||
}, | ||
}) | ||
const {code, frontmatter} = await bundleMDXFile( | ||
'/users/you/site/content/file.mdx', | ||
) | ||
``` | ||
`cwd` will be automatically set to the `dirname` of the given file path, you can | ||
still override this. All other options work the same as they do for `bundleMDX`. | ||
### Known Issues | ||
@@ -615,0 +616,0 @@ |
47253
453
765
+ Added@types/acorn@4.0.6(transitive)
+ Added@types/mdurl@1.0.5(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedcharacter-entities-html4@2.1.0(transitive)
+ Addedcharacter-entities-legacy@3.0.0(transitive)
+ Addedcharacter-reference-invalid@2.0.1(transitive)
+ Addedestree-util-visit@1.2.1(transitive)
+ Addedfault@2.0.1(transitive)
+ Addedis-alphabetical@2.0.1(transitive)
+ Addedis-alphanumerical@2.0.1(transitive)
+ Addedis-decimal@2.0.1(transitive)
+ Addedis-hexadecimal@2.0.1(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedmdast-util-definitions@5.1.2(transitive)
+ Addedmdast-util-frontmatter@1.0.1(transitive)
+ Addedmdast-util-mdx@1.1.0(transitive)
+ Addedmdast-util-mdx-jsx@1.2.0(transitive)
+ Addedmdast-util-to-hast@11.3.0(transitive)
+ Addedmicromark-extension-frontmatter@1.1.1(transitive)
+ Addedmicromark-extension-mdx-expression@1.0.8(transitive)
+ Addedmicromark-extension-mdx-jsx@1.0.5(transitive)
+ Addedmicromark-extension-mdx-md@1.0.1(transitive)
+ Addedmicromark-extension-mdxjs@1.0.1(transitive)
+ Addedmicromark-extension-mdxjs-esm@1.0.5(transitive)
+ Addedmicromark-factory-mdx-expression@1.0.9(transitive)
+ Addedmicromark-util-events-to-acorn@1.2.3(transitive)
+ Addedparse-entities@4.0.2(transitive)
+ Addedremark-frontmatter@4.0.1(transitive)
+ Addedremark-parse@10.0.2(transitive)
+ Addedremark-rehype@9.1.0(transitive)
+ Addedsource-map@0.7.4(transitive)
+ Addedstringify-entities@4.0.4(transitive)
+ Addedtrough@2.2.0(transitive)
+ Addedunified@10.1.2(transitive)
+ Addedunist-builder@3.0.1(transitive)
+ Addedunist-util-generated@2.0.1(transitive)
+ Addedunist-util-remove-position@4.0.2(transitive)
+ Addedvfile@5.3.7(transitive)
+ Addedvfile-message@3.1.4(transitive)
+ Addedxdm@2.1.0(transitive)
- Removedbail@1.0.5(transitive)
- Removedcharacter-entities@1.2.4(transitive)
- Removedcharacter-entities-html4@1.1.4(transitive)
- Removedcharacter-entities-legacy@1.1.4(transitive)
- Removedcharacter-reference-invalid@1.1.4(transitive)
- Removedfault@1.0.4(transitive)
- Removedis-alphabetical@1.0.4(transitive)
- Removedis-alphanumerical@1.0.4(transitive)
- Removedis-decimal@1.0.4(transitive)
- Removedis-hexadecimal@1.0.4(transitive)
- Removedis-plain-obj@2.1.0(transitive)
- Removedlongest-streak@2.0.4(transitive)
- Removedmdast-util-definitions@4.0.0(transitive)
- Removedmdast-util-from-markdown@0.8.5(transitive)
- Removedmdast-util-frontmatter@0.2.0(transitive)
- Removedmdast-util-mdx@0.1.1(transitive)
- Removedmdast-util-mdx-expression@0.1.1(transitive)
- Removedmdast-util-mdx-jsx@0.1.4(transitive)
- Removedmdast-util-mdxjs-esm@0.1.1(transitive)
- Removedmdast-util-to-hast@10.2.0(transitive)
- Removedmdast-util-to-markdown@0.6.5(transitive)
- Removedmdast-util-to-string@2.0.0(transitive)
- Removedmicromark@2.11.4(transitive)
- Removedmicromark-extension-frontmatter@0.2.2(transitive)
- Removedmicromark-extension-mdx-expression@0.3.2(transitive)
- Removedmicromark-extension-mdx-jsx@0.3.3(transitive)
- Removedmicromark-extension-mdx-md@0.1.1(transitive)
- Removedmicromark-extension-mdxjs@0.3.0(transitive)
- Removedmicromark-extension-mdxjs-esm@0.3.1(transitive)
- Removedmin-indent@1.0.1(transitive)
- Removedparse-entities@2.0.0(transitive)
- Removedremark-frontmatter@3.0.0(transitive)
- Removedremark-parse@9.0.0(transitive)
- Removedremark-rehype@8.1.0(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedstringify-entities@3.1.0(transitive)
- Removedstrip-indent@3.0.0(transitive)
- Removedtrough@1.0.5(transitive)
- Removedunified@9.2.2(transitive)
- Removedunist-builder@2.0.3(transitive)
- Removedunist-util-generated@1.1.6(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-position@3.1.0(transitive)
- Removedunist-util-remove-position@3.0.0(transitive)
- Removedunist-util-stringify-position@2.0.3(transitive)
- Removedunist-util-visit@2.0.33.1.0(transitive)
- Removedunist-util-visit-parents@3.1.14.1.1(transitive)
- Removedvfile@4.2.1(transitive)
- Removedvfile-message@2.0.4(transitive)
- Removedxdm@1.12.2(transitive)
- Removedxtend@4.0.2(transitive)
- Removedzwitch@1.0.5(transitive)
Updated@babel/runtime@^7.15.3
Updatedremark-frontmatter@^4.0.0
Updatedxdm@^2.0.0