Image without description
  • Jacco Meijer
  • |
  • Mar 19, 2024

Esbuild as a static site generator for MDX

Static site generators gain popularity. This blog is about using Esbuild as a static site generator for MDX.

11ty and Github pages

The last post summarized how the Contentful-Gatsby-Netlify trio could be simplified by using Github pages and 11ty.

Image without description
  • Jacco Meijer
  • |
  • Mar 18, 2024

11ty and Github pages

Simplifying the Contentful-Gatsby-Netlfy trio.

This post continues the quest for simplicity from there.

The proposition is, if only Esbuild can be used to generate static HTML, this would be even simpler than using 11ty.

Javascript bundler landscape

Nobody really liked solving the Webpack and Babel configuration puzzles. Occasionally they blocked the actual project.

The bundler landscape matured with the rise of Parcel and Rollup and in 2020 Evan Wallace, one of the co-founders of Figma, released Esbuild.

Written in Go and 100 times faster than Webpack it is a welcome addition to the Javascript bundler landscape

Esbuild

Esbuild supports TypeScript, JSX and tree-shaking. Besides that, Esbuild contains a live reloading webserver that triggers a full rebuild on a browser refresh.

Esbuild generates Javascript bundles but a static site generator generates HTML. The extensible nature of Esbuild allows for a simple plugin to take care of transforming the final bundle to HTML.

MDX

Setting up Esbuild and @mdx-js/esbuild is straightforward and clearly explained on mdxjs.com.

This Esbuild/MDX setup makes Esbuild transform MDX to JSX/Javascript bundles. Additional functionality can be added by using plugins from the rich MDX ecosystem. The example below shows how to add support for:

  • Front Matter (as described in previous post)
  • Code highlighting
  • GitHub Flavored Markdown Spec
import mdx from '@mdx-js/esbuild'
import rehypeHighlight from 'rehype-highlight'
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
import remarkGfm from 'remark-gfm'

const plugins = [
  mdx({
    jsxImportSource: 'preact',
    remarkPlugins: [
      [remarkGfm],
      [remarkFrontmatter, ['yaml', 'toml']],
      [remarkMdxFrontmatter, { name: 'frontmatter' }],
    ],
    rehypePlugins: [rehypeHighlight],
  }),
]

Live reloading webserver

Setting up the Esbuild webserver is explained here. For live reloading a line of Javascript must be added to the page template.

Using Javascript the Esbuild webserver is started like this:

const ctx = await esbuild.context({
  entryPoints: ['index.js'],
  format: 'esm',
  outdir: 'build',
  plugins: [mdx({/** options **/})]
})

await ctx.watch()
const { port } = await ctx.serve({ servedir: 'build' })

console.log(`Live reload server started at http://localhost:${port}\n`)

Conclusion

Esbuild is a fantastic tool. It is fast, capable and extendable. With 'something' that renders JSX bundles to static HTML Esbuild could act as a simple static site generator.

JSX Render plugin

The 'something' from the last paragraph is described as a 'JSX Render plugin'. This plugin must transform generated Javascript bundles to HTML pages.

More on that in the next post!

Image without description
  • Jacco Meijer
  • |
  • Mar 20, 2024

Render JSX plugin for Esbuild

Transform Esbuild generated JSX bundles to HTML pages.


Other posts

Image without description
  • Jacco Meijer
  • |
  • Mar 21, 2024

UI Library with MDX documentation

Using the simple Render JSX plugin for Esbuild this post shows how to setup a simple UI library.

Image without description
  • Jacco Meijer
  • |
  • Mar 20, 2024

Render JSX plugin for Esbuild

Transform Esbuild generated JSX bundles to HTML pages.

Image without description
  • Jacco Meijer
  • |
  • Mar 19, 2024

Esbuild as a static site generator for MDX

Static site generators gain popularity. This blog is about using Esbuild as a static site generator for MDX.

Image without description
  • Jacco Meijer
  • |
  • Mar 18, 2024

11ty and Github pages

Simplifying the Contentful-Gatsby-Netlfy trio.

Image without description
  • Jacco Meijer
  • |
  • Mar 15, 2024

OWASP and CISSP

OWASP recommendations from the independent information security certification CISSP.

Image without description
  • Jacco Meijer
  • |
  • Jun 30, 2022

NPM7 and @npmcli/arborist

@npmcli/arborist is a powerful library that handles the new NPM 7 workspaces. This blog is about a simple make tool that uses the library.

Image without description
  • Jacco Meijer
  • |
  • May 12, 2022

Comparing React app, Nextjs and Gatsby

A new React project starts with a React toolchain. Main tools in the chains are SSR, React server components and GraphQL.

Image without description
  • Jacco Meijer
  • |
  • May 10, 2022

Versioning strategy for NPM modules

It is important to be able to bump the version of a NPM package without side effects.

Image without description
  • Jacco Meijer
  • |
  • Apr 12, 2022

React component themes and CSS variables

Creating React components with flexible themes by using CSS variables.

Image without description
  • Jacco Meijer
  • |
  • Mar 21, 2022

Content modeling with variants

The efficiency of a variant field in a content model.

Image without description
  • Jacco Meijer
  • |
  • Mar 12, 2022

Documentation

Documenting a software project is challenging. Here's a few simple guidelines that help a team writing clear documentation.

Image without description
  • Jacco Meijer
  • |
  • Mar 11, 2022

Javascript history

In 1986 David Ungar and Randall B. Smith developed Self at Xerox PARC. Inspired by Java, Scheme and Self Brendan Eich created Javascript in 1995.

Image without description
  • Jacco Meijer
  • |
  • Mar 10, 2022

On Javascript transpilers, bundlers and modules

There's Javascript transpilers, modules, bundles and bundlers. This is a brief overview of all of these.

Image without description
  • Jacco Meijer
  • |
  • Feb 11, 2022

Agile Scrum

The Agile Scrum framework is flexible enough to be used in many different ways. Here's one way of working.

Image without description
  • Jacco Meijer
  • |
  • Jan 20, 2022

What happened to Wheelroom?

Founded in 2018. Started to fly in 2020 and abandoned in 2021. What happened?

Image without description
  • Jacco Meijer
  • |
  • Jan 19, 2022

Contentful, Netlify and Gatsby four years later

What did we learn from using Contentful for four years?

Image without description
  • Jacco Meijer
  • |
  • Jan 18, 2022

Typescript interface for React UI components

How to define an interface for React UI components that prevents breaking changes.

Image without description
  • Jacco Meijer
  • |
  • Jan 17, 2022

Naming React components

What's in a name? A clear naming strategy helps developers communicate. Most devs rather spend time writing component code than wasting time on a good component name.