micromark Build Tool
micromark-build is a small, specialized Command Line Interface (CLI) tool designed to optimize and build development source code for micromark extensions into production-ready output. It achieves this by applying a series of Babel transformations such as `babel-plugin-unassert`, `babel-plugin-undebug`, and `babel-plugin-inline-constants`. These plugins are responsible for removing development-specific assertions (like `assert` or `uvu/assert`), debug calls (from `debug`), and for inlining constant values from specified modules (e.g., `micromark-util-symbol`). This optimization process results in smaller and faster code for deployment in browser or Node.js environments. The current stable version is 2.0.3, with new releases typically coordinated within the broader micromark monorepo. Its primary differentiating factor is its tailored optimization focus specifically for the micromark ecosystem, making it an essential utility for developers creating micromark extensions who need to balance development-time debugging with production-ready performance and minimal bundle size.
Common errors
-
Error: Command failed with exit code 1
cause This generic error often indicates that `micromark-build` could not find the expected `dev/` source directory, or encountered other file system or processing errors during its run.fixVerify that your project structure includes a `dev/` folder containing your development source code. Ensure that `micromark-build` and its internal dependencies are correctly installed and accessible in your environment (`npm install`). Check for any syntax errors in your source files that might halt the Babel transformation. -
Error: Cannot find module 'micromark-build' from '...' or 'TypeError: micromarkBuild is not a function'
cause These errors occur when attempting to `import` or `require` `micromark-build` directly into your JavaScript/TypeScript code.fixAs `micromark-build` is a CLI tool, it should not be imported or required as a programmatic module. Instead, execute it from your `package.json` scripts, for example, by adding `"build": "micromark-build"` to your `scripts` section and running `npm run build`.
Warnings
- breaking Projects within the unified collective, including `micromark-build`, are only compatible with maintained versions of Node.js. Major releases typically drop support for unmaintained Node.js versions.
- gotcha micromark-build is a highly specialized tool tailored for the `micromark` ecosystem, specifically for optimizing micromark extensions. It is not a generic JavaScript build tool and is not recommended for use outside this context.
- gotcha To correctly use `micromark-build` and leverage its development-time features (assertions, debug messages), you must define an `exports` map in your `package.json` with a `development` condition. To run your project using the development files, Node.js must be launched with the `--conditions development` flag.
Install
-
npm install micromark-build -
yarn add micromark-build -
pnpm add micromark-build
Imports
- micromark-build (CLI)
import { micromarkBuild } from 'micromark-build'npm install micromark-build --save-dev // Then, in package.json scripts: "build": "micromark-build"
- Development Condition
import { development } from 'micromark-build'node --conditions development ./dev/index.js
- TypeScript Types (related)
import type { BuildOptions } from 'micromark-build'import type { CompileContext } from 'micromark-util-types'
Quickstart
{
"name": "my-micromark-extension",
"version": "1.0.0",
"description": "My custom micromark extension.",
"type": "module",
"main": "index.js",
"scripts": {
"build": "micromark-build"
},
"exports": {
"development": "./dev/index.js",
"default": "./index.js"
},
"devDependencies": {
"micromark-build": "^2.0.0",
"devlop": "^1.0.0",
"micromark-util-symbol": "^2.0.0",
"uvu": "^0.5.0"
}
}
// Example: my-micromark-extension/dev/index.js
// (This file is processed by `micromark-build` into lib/index.js)
import { codes } from 'micromark-util-symbol';
import { assert } from 'uvu/assert';
import { debug } from 'devlop';
const log = debug('my-extension');
export function myExtension() {
assert.ok(true, 'This assertion will be removed in production!');
log('Debugging my extension setup.');
return { tokenizer: { 92: onBackslash } }; // ASCII 92 is '\\'
}
function onBackslash(code) {
log('Handling backslash character.');
// `codes.backslash` will be inlined in production by micromark-build.
return code === codes.backslash ? 1 : 0;
}