babel-plugin-pegjs-inline-precompile
raw JSON → 0.1.1 verified Sat Apr 25 auth: no javascript
Babel plugin that precompiles inline PEG.js grammar expressions at build time. Version 0.1.1, limited release cadence. Replaces runtime parsing with a compile-time step using a tagged template literal `peg`, requiring the complementary `pegjs-inline-precompile` runtime helper. Differentiates from other PEG.js integrations by minimizing runtime overhead and bundling size.
Common errors
error Module not found: Can't resolve 'pegjs-inline-precompile' ↓
cause Missing runtime package dependency.
fix
Run 'npm install pegjs-inline-precompile' and add to dependencies.
error Error: The 'pegjs-inline-precompile' plugin requires a matching runtime package, but it is not loaded. ↓
cause Babel plugin unable to locate the runtime module.
fix
Install 'pegjs-inline-precompile' and ensure it is resolvable in node_modules.
error ReferenceError: peg is not defined ↓
cause Missing import of 'peg' from 'pegjs-inline-precompile'.
fix
Add 'import peg from 'pegjs-inline-precompile';' at top of file.
Warnings
gotcha The plugin requires the 'pegjs-inline-precompile' runtime package to be installed; otherwise, the `peg` import will fail. ↓
fix Ensure 'pegjs-inline-precompile' is in your dependencies, not just devDependencies.
gotcha The plugin itself does not include PEG.js; you must install 'pegjs' separately if you need the PEG.js runtime for dynamic grammar compilation. ↓
fix Install 'pegjs' as a dependency if you plan to use PEG.js outside of the precompilation context.
gotcha The plugin only processes tagged template literals with the exact identifier name 'peg'; custom names will not be precompiled. ↓
fix Always use `peg` as the tag function name in your source code.
Install
npm install babel-plugin-pegjs-inline-precompile yarn add babel-plugin-pegjs-inline-precompile pnpm add babel-plugin-pegjs-inline-precompile Imports
- peg wrong
import { peg } from 'pegjs-inline-precompile';correctimport peg from 'pegjs-inline-precompile'; - peg wrong
const { peg } = require('pegjs-inline-precompile');correctconst peg = require('pegjs-inline-precompile'); - plugin wrong
{ "plugins": ["pegjs-inline-precompile"] }correct// .babelrc { "plugins": ["babel-plugin-pegjs-inline-precompile"] }
Quickstart
// Install dependencies
// npm install babel-plugin-pegjs-inline-precompile pegjs pegjs-inline-precompile
// .babelrc
{
"plugins": ["babel-plugin-pegjs-inline-precompile"]
}
// source.js
import peg from 'pegjs-inline-precompile';
const parser = peg`
// Grammar
Expression = Term ("+" Term)*
Term = Number ("*" Number)*
Number = [0-9]+
`;
export default function evaluate(input) {
return parser.parse(input);
}