Skip to content

Commit 9de63f2

Browse files
authored
feat: updates, and override entryPoints (#54)
* docs: change image bundling wording to make it clear that each _bundle_ needs its own output directory, closes #52 * fix: update esbuild (and dev deps) * fix: move uvu to devDependencies * chore: fix my website address in all contributors * feat: add a test to check overriding `entryPoints` as described in #51, allow overwriting when using `write: true` * docs: document replacing the entry point * tests: swap to dirname for input path * tests: swap to contributing and improve the match * fix(ts): improve the typings
1 parent d2c83bf commit 9de63f2

File tree

5 files changed

+74
-20
lines changed

5 files changed

+74
-20
lines changed

.all-contributorsrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"login": "Arcath",
3838
"name": "Adam Laycock",
3939
"avatar_url": "https://avatars.githubusercontent.com/u/19609?v=4",
40-
"profile": "https://arcath.net",
40+
"profile": "https://www.arcath.net",
4141
"contributions": [
4242
"code",
4343
"test",

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ import {remarkMdxImages} from 'remark-mdx-images'
498498

499499
const {code} = await bundleMDX(mdxSource, {
500500
cwd: '/users/you/site/_content/pages',
501-
xdmOptions: (vFile, options) => {
501+
xdmOptions: options => {
502502
options.remarkPlugins = [remarkMdxImages]
503503

504504
return options
@@ -519,24 +519,30 @@ The `file` loader requires a little more configuration to get working. With the
519519
be set to write files and needs to know where to put them plus the url of the
520520
folder to be used in image sources.
521521

522+
> Each call to `bundleMDX` is isloated from the others. If you set the directory
523+
> the same for everything `bundleMDX` will overwrite images without warning. As
524+
> a result each _bundle_ needs its own output directory.
525+
522526
```js
527+
// For the file `_content/pages/about.mdx`
528+
523529
const {code} = await bundleMDX(mdxSource, {
524530
cwd: '/users/you/site/_content/pages',
525-
xdmOptions: (vFile, options) => {
531+
xdmOptions: options => {
526532
options.remarkPlugins = [remarkMdxImages]
527533

528534
return options
529535
},
530536
esbuildOptions: options => {
531-
// Set the `outdir` to your public directory.
532-
options.outdir = '/users/you/site/public/img'
537+
// Set the `outdir` to a public location for this bundle.
538+
options.outdir = '/users/you/site/public/img/about'
533539
options.loader = {
534540
...options.loader,
535541
// Tell esbuild to use the `file` loader for pngs
536542
'.png': 'file',
537543
}
538-
// Set the public path to /img/ so image sources start /img/
539-
options.publicPath = '/img/'
544+
// Set the public path to /img/about
545+
options.publicPath = '/img/about'
540546

541547
// Set write to true so that esbuild will output the files.
542548
options.write = true
@@ -546,6 +552,23 @@ const {code} = await bundleMDX(mdxSource, {
546552
})
547553
```
548554

555+
### Replacing the entry point
556+
557+
If your MDX file is on your disk you can save some time and code by having
558+
`esbuild` read the file for you. To do this you can override the `entryPoints`
559+
settings in `esbuildOptions` with the path to your mdx source.
560+
561+
```js
562+
const {code, frontmatter} = await bundleMDX('', {
563+
cwd: '/users/you/site/_content/pages',
564+
esbuildOptions: options => {
565+
options.entryPoints = ['/users/you/site/_content/pages/file.mdx']
566+
567+
return options
568+
},
569+
})
570+
```
571+
549572
### Known Issues
550573

551574
#### Cloudflare Workers
@@ -647,7 +670,7 @@ Thanks goes to these people ([emoji key][emojis]):
647670
<tr>
648671
<td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3?s=100" width="100px;" alt=""/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/kentcdodds/mdx-bundler/commits?author=kentcdodds" title="Code">💻</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=kentcdodds" title="Documentation">📖</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=kentcdodds" title="Tests">⚠️</a></td>
649672
<td align="center"><a href="https://github.com/benwis"><img src="https://avatars.githubusercontent.com/u/6953353?v=4?s=100" width="100px;" alt=""/><br /><sub><b>benwis</b></sub></a><br /><a href="https://github.com/kentcdodds/mdx-bundler/issues?q=author%3Abenwis" title="Bug reports">🐛</a> <a href="https://github.com/kentcdodds/mdx-bundler/pulls?q=is%3Apr+reviewed-by%3Abenwis" title="Reviewed Pull Requests">👀</a></td>
650-
<td align="center"><a href="https://arcath.net"><img src="https://avatars.githubusercontent.com/u/19609?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Adam Laycock</b></sub></a><br /><a href="https://github.com/kentcdodds/mdx-bundler/commits?author=Arcath" title="Code">💻</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=Arcath" title="Tests">⚠️</a> <a href="#ideas-Arcath" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/kentcdodds/mdx-bundler/pulls?q=is%3Apr+reviewed-by%3AArcath" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=Arcath" title="Documentation">📖</a></td>
673+
<td align="center"><a href="https://www.arcath.net"><img src="https://avatars.githubusercontent.com/u/19609?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Adam Laycock</b></sub></a><br /><a href="https://github.com/kentcdodds/mdx-bundler/commits?author=Arcath" title="Code">💻</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=Arcath" title="Tests">⚠️</a> <a href="#ideas-Arcath" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/kentcdodds/mdx-bundler/pulls?q=is%3Apr+reviewed-by%3AArcath" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=Arcath" title="Documentation">📖</a></td>
651674
<td align="center"><a href="http://wooorm.com"><img src="https://avatars.githubusercontent.com/u/944406?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Titus</b></sub></a><br /><a href="#ideas-wooorm" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/kentcdodds/mdx-bundler/pulls?q=is%3Apr+reviewed-by%3Awooorm" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/kentcdodds/mdx-bundler/commits?author=wooorm" title="Code">💻</a></td>
652675
<td align="center"><a href="https://github.com/ChristianMurphy"><img src="https://avatars.githubusercontent.com/u/3107513?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Christian Murphy</b></sub></a><br /><a href="#ideas-ChristianMurphy" title="Ideas, Planning, & Feedback">🤔</a></td>
653676
<td align="center"><a href="https://ped.ro"><img src="https://avatars.githubusercontent.com/u/372831?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Pedro Duarte</b></sub></a><br /><a href="https://github.com/kentcdodds/mdx-bundler/commits?author=peduarte" title="Documentation">📖</a></td>

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,27 @@
4343
"@babel/runtime": "^7.14.0",
4444
"@esbuild-plugins/node-resolve": "^0.1.4",
4545
"@fal-works/esbuild-plugin-global-externals": "^2.1.1",
46-
"esbuild": "^0.11.20",
46+
"esbuild": "^0.12.8",
4747
"gray-matter": "^4.0.3",
4848
"remark-frontmatter": "^3.0.0",
4949
"remark-mdx-frontmatter": "^1.0.1",
50-
"uvu": "^0.5.1",
5150
"xdm": "^1.11.0"
5251
},
5352
"devDependencies": {
54-
"@testing-library/react": "^11.2.6",
55-
"@types/jsdom": "^16.2.10",
53+
"@testing-library/react": "^11.2.7",
54+
"@types/jsdom": "^16.2.11",
5655
"@types/react": "^17.0.5",
57-
"@types/react-dom": "^17.0.3",
56+
"@types/react-dom": "^17.0.7",
5857
"cross-env": "^7.0.3",
59-
"jsdom": "^16.5.3",
60-
"kcd-scripts": "^10.0.0",
58+
"jsdom": "^16.6.0",
59+
"kcd-scripts": "^11.1.0",
6160
"left-pad": "^1.3.0",
6261
"mdx-test-data": "^1.0.1",
6362
"react": "^17.0.2",
6463
"react-dom": "^17.0.2",
6564
"remark-mdx-images": "^1.0.2",
66-
"typescript": "^4.2.4"
65+
"typescript": "^4.3.2",
66+
"uvu": "^0.5.1"
6767
},
6868
"eslintConfig": {
6969
"extends": "./node_modules/kcd-scripts/eslint.js",

src/__tests__/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import './setup-tests.js'
22
import path from 'path'
3+
import {fileURLToPath} from 'url'
34
import {test} from 'uvu'
45
import * as assert from 'uvu/assert'
56
import React from 'react'
@@ -411,4 +412,30 @@ Local Content
411412
)
412413
})
413414

415+
test('should support over-riding the entry point', async () => {
416+
const {code} = await bundleMDX('', {
417+
cwd: process.cwd(),
418+
esbuildOptions: options => {
419+
options.entryPoints = [
420+
path.join(
421+
path.dirname(fileURLToPath(import.meta.url)),
422+
'..',
423+
'..',
424+
'CONTRIBUTING.md',
425+
),
426+
]
427+
options.outdir = path.join(process.cwd(), 'output')
428+
options.write = true
429+
430+
return options
431+
},
432+
})
433+
434+
const Component = getMDXComponent(code)
435+
436+
const {container} = render(React.createElement(Component))
437+
438+
assert.match(container.innerHTML, 'Thanks for being willing to contribute')
439+
})
440+
414441
test.run()

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,15 @@ async function bundleMDX(
170170
}
171171

172172
if (buildOptions.outdir && buildOptions.write) {
173-
const code = await readFile(
174-
path.join(buildOptions.outdir, '_mdx_bundler_entry_point.js'),
175-
)
173+
// We know that this has to be an array of entry point strings, with a single entry
174+
const entryFile = /** @type {{entryPoints: string[]}} */ (buildOptions)
175+
.entryPoints[0]
176+
177+
const fileName = path.basename(entryFile).replace(/\.[^/.]+$/, '.js')
178+
179+
const code = await readFile(path.join(buildOptions.outdir, fileName))
176180

177-
await unlink(path.join(buildOptions.outdir, '_mdx_bundler_entry_point.js'))
181+
await unlink(path.join(buildOptions.outdir, fileName))
178182

179183
return {
180184
code: `${code};return Component.default;`,

0 commit comments

Comments
 (0)