vite-plugin-peggy-loader

raw JSON →
2.0.1 verified Mon Apr 27 auth: no javascript

Vite plugin that automatically compiles Peggy (PEG.js) grammar files (.pegjs, .peggy) into JavaScript parser modules during the build process. Version 2.0.1 supports Vite and accepts all standard Peggy options (cache, optimize, trace, dependencies, allowedStartRules). Released under MIT license. Alternatives include manual compilation or webpack's peggy-loader; this integrates seamlessly with Vite's plugin system and respects Vite's ESM transform.

error Cannot find module 'vite-plugin-peggy-loader' or its corresponding type declarations.
cause Missing npm install or types not included.
fix
Run npm install vite-plugin-peggy-loader. If using TypeScript, ensure tsconfig.json has "moduleResolution": "node" and "esModuleInterop": true.
error Error: No known conditions for "./" specifier in "vite-plugin-peggy-loader" package
cause Using a Node.js version that does not support the `exports` field, or the import path is incorrect.
fix
Use Node >=12.20.0 or <14. It is an ESM package; use dynamic import if CommonJS is required.
error TypeError: peggyLoader is not a function
cause Incorrect import: using named import `{ peggyLoader }` when default export is expected, or vice versa.
fix
Use import peggyLoader from 'vite-plugin-peggy-loader' (default import) or import { peggyLoader } from 'vite-plugin-peggy-loader' (named import) as available from version 2.0.1.
breaking Version 2.0.0 dropped support for PEG.js < 1.2.0. Only Peggy ^1.2.0, ^3.0.2, and ^4.0.0 are supported as peer dependencies.
fix Upgrade your grammar toolchain to Peggy >=1.2.0.
deprecated The option `optimizeParser` was renamed to `optimize` in a previous version.
fix Use the `optimize` option instead of `optimizeParser`.
gotcha Importing `.pegjs` files yields a default export that is an object with a `parse` function, not the raw parser function. This differs from some other loaders.
fix Use `import * as parser from './grammar.pegjs'` and call `parser.parse(input)`.
gotcha The plugin does not support `.peggy` file extension by default; only `.pegjs` is handled.
fix Rename your files to `.pegjs` or modify the plugin's source to include `.peggy`.
gotcha The `dependencies` option requires that the parser's dependencies are available at runtime, but this plugin does not automatically include them in the bundle.
fix Manually import the dependency modules in your code or configure Vite to handle them.
npm install vite-plugin-peggy-loader
yarn add vite-plugin-peggy-loader
pnpm add vite-plugin-peggy-loader

Configures Vite to compile .pegjs files on import using Peggy with caching and speed optimization.

import { defineConfig } from 'vite';
import peggyLoader from 'vite-plugin-peggy-loader';

export default defineConfig({
  plugins: [
    peggyLoader({
      cache: true,
      optimize: 'speed',
      trace: false
    })
  ]
});