Registry / devops / esbuild-peggy

esbuild-peggy

JSON →
library1.0.7jsnpmunverified

esbuild plugin that allows importing PEG.js/Peggy grammar files (.pegjs/.peggy) directly in JavaScript/TypeScript bundles. Current stable version is 1.0.7, requiring Node >=20 and npm >=10. It automatically rebuilds parsers when grammar files change, fully supports esbuild watch mode, and ships TypeScript types. Differentiates from similar tools (like pegjs-loader for webpack) by targeting esbuild's speed and simplicity.

npm install esbuild-peggy
INSTALL
IMPORT
SIG · ESBUILD-PEGGY
E
esbuild-peggy
devopsjavascriptv1.0.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

peggyPlugin
import { peggyPlugin } from 'esbuild-peggy'
const peggyPlugin = require('esbuild-peggy')
The package is ESM-only; require() will fail. Use import syntax.
peggyPlugin (with options)
import { peggyPlugin } from 'esbuild-peggy'; peggyPlugin({ cache: true })
peggyPlugin() without options
Options are passed as an object to peggyPlugin(), mapped to Peggy's ParserOptions.
default import of grammar
import parser from './grammar.pegjs'
import { parse } from './grammar.pegjs'
The plugin returns the parser object as default export. Named exports are not supported.
TypeScript usage
import parser from './grammar.pegjs'; parser.parse('input') as number
Declaring type manually
The plugin ships types, so the parser is typed automatically.

Configures esbuild with the peggy plugin, bundles a TypeScript file that imports a PEG.js grammar, and parses an input.

// esbuild.config.js import { build } from 'esbuild'; import { peggyPlugin } from 'esbuild-peggy'; await build({ entryPoints: ['./src/index.ts'], bundle: true, outfile: './dist/bundle.js', plugins: [ peggyPlugin({ // Optional Peggy parser options cache: true }) ], loader: { '.pegjs': 'js', '.peggy': 'js' } }); // After building, import grammar in your source: // src/index.ts import parser from './grammar.pegjs'; const result = parser.parse('123'); console.log(result); // 123
Debug
Known issues
gotchaThe plugin requires both 'esbuild' and 'peggy' as peer dependencies. If missing, the plugin will fail at build time.
fix
Install peer dependencies: npm install --save-dev esbuild peggy
affects: >=1.0.0
gotchaThe plugin is ESM-only (no CommonJS). Using require() to import peggyPlugin throws an error.
fix
Use import syntax or use dynamic import: const { peggyPlugin } = await import('esbuild-peggy');
affects: >=1.0.0
gotchaNode >=20 required. Older Node versions will fail to run the plugin.
fix
Upgrade Node to version 20 or higher.
affects: >=1.0.0
deprecatedPEG.js is deprecated; upstream recommends using Peggy.js. The plugin supports both .pegjs and .peggy extensions.
fix
Use .peggy extensions and Peggy's API for future-proofing.
affects: >=1.0.0
gotchaThe .pegjs/.peggy files must be added to esbuild's loader config as 'js', or the plugin won't process them.
fix
Add loader: { '.pegjs': 'js', '.peggy': 'js' } to your esbuild config.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'esbuild-peggy'
esbuild-peggy is not installed or missing from node_modules.
fix
npm install --save-dev esbuild-peggy
TypeError: peggyPlugin is not a function
Importing the plugin incorrectly, likely using require() instead of import.
fix
Use 'import { peggyPlugin } from "esbuild-peggy"' instead of require().
Error: Build failed with 1 error: error: No loader is configured for ".pegjs" files
Missing loader configuration for .pegjs/.peggy files in esbuild.
fix
Add loader: { '.pegjs': 'js', '.peggy': 'js' } to the build options.
Error: The module "./grammar.pegjs" was resolved to '', but the plugin couldn't handle it.
The plugin is not registered in the esbuild plugins array.
fix
Add plugins: [ peggyPlugin() ] to the build configuration.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies
esbuildrequiredPeer dependency; the plugin integrates with esbuild's build system.
peggyrequiredPeer dependency; the plugin uses Peggy to generate parsers from grammars.
Agent activity
4 hits · last 30 days
node
4
Resources
esbuild-peggy — npm install esbuild-peggy · libregistry