vue-route-generator is a utility library for generating Vue Router configuration arrays based on a file-system-driven page component structure, mirroring conventions seen in frameworks like Nuxt. It allows developers to define routes implicitly by organizing `.vue` files within a specified directory, eliminating the need for manual route array definitions. The current stable version is `1.0.0`, which introduced compatibility with Vue 3. While not on a fixed release cadence, it receives updates for bug fixes and feature enhancements, with major versions primarily driven by Vue ecosystem shifts. A key differentiator is its focus on generating static JavaScript code at build time, rather than performing runtime route resolution, and its support for custom route metadata embedded directly within Vue Single File Components via a `<route>` block. It serves as a lower-level tool compared to related projects like `vue-auto-routing`, offering more granular control over the generation process.
npm install vue-route-generatorVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `generateRoutes` to create a Vue Router configuration string from a mock `pages` directory. It then prints the generated code to the console, showing the resulting route array with dynamic imports.
For Vue 2 projects, remain on `0.x` versions. For Vue 3, update `vue-router` to its Vue 3 compatible version (v4) and ensure other Vue dependencies are aligned.
Thoroughly review generated routes after upgrading. Check the changelog for specific changes to routing behavior and adjust your file structure or `<route>` blocks as needed.
Ensure the content of the `<route>` block is valid JSON. Use it to *merge* additional properties or *override* default generated properties, understanding the precedence rules. Utilize editor extensions like Vetur with `"vetur.grammar.customBlocks": { "route": "json" }` for syntax validation.If dynamic imports cause issues, set `dynamicImport: false` in the `generateRoutes` config. This will generate `require()` statements (or direct imports if ESM). Ensure your bundler supports the generated import syntax for your target environments.
Run `npm install vue-route-generator` or `yarn add vue-route-generator` in your project directory.
Verify the `pages` option in `generateRoutes` points to the correct directory. Check file names and directory structure against Nuxt-style routing conventions (e.g., `_id.vue` for dynamic segments). Ensure no files are unintentionally ignored (e.g., those starting/ending with `__`).
Check the output of `generateRoutes` (e.g., `console.log(generatedCode)`). Ensure `fs.writeFileSync` is correctly saving the output to the specified file and that the file is subsequently imported correctly as a default export.
Add the following configuration to your VS Code `settings.json`: `"vetur.grammar.customBlocks": { "route": "json" }` to enable JSON syntax highlighting within the `<route>` block.No dependency data recorded yet.