@van1s1mys/ai-router-plugin-webpack 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -0
- package/dist/index.cjs +0 -1
- package/dist/index.js +0 -1
- package/package.json +1 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @van1s1mys/ai-router-plugin-webpack
|
|
2
|
+
|
|
3
|
+
webpack plugin for [@van1s1mys/ai-router](https://www.npmjs.com/package/@van1s1mys/ai-router) — auto-scans your pages directory and aliases `virtual:ai-router` to a generated routes module.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @van1s1mys/ai-router @van1s1mys/ai-router-plugin-webpack
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// webpack.config.js
|
|
15
|
+
const { AiRouterPlugin } = require('@van1s1mys/ai-router-plugin-webpack');
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
plugins: [
|
|
19
|
+
new AiRouterPlugin(),
|
|
20
|
+
],
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// app.ts
|
|
26
|
+
import { SmartRouter } from '@van1s1mys/ai-router';
|
|
27
|
+
import { routes } from 'virtual:ai-router';
|
|
28
|
+
|
|
29
|
+
const router = new SmartRouter({ routes });
|
|
30
|
+
await router.ready;
|
|
31
|
+
|
|
32
|
+
const result = await router.search('how much does it cost?');
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Options
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
new AiRouterPlugin({
|
|
39
|
+
// Directories to scan (default: auto-detect)
|
|
40
|
+
dirs: ['src/pages'],
|
|
41
|
+
|
|
42
|
+
// File extensions to include
|
|
43
|
+
extensions: ['.tsx', '.jsx', '.vue', '.svelte', '.astro', '.md', '.mdx'],
|
|
44
|
+
|
|
45
|
+
// Patterns to exclude
|
|
46
|
+
exclude: ['_layout', 'api/'],
|
|
47
|
+
|
|
48
|
+
// Additional manual routes merged with scanned ones
|
|
49
|
+
routes: [
|
|
50
|
+
{ path: '/pricing', title: 'Pricing', description: 'cost, plans' },
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Standalone helper
|
|
56
|
+
|
|
57
|
+
Use `getRoutes()` outside of webpack (e.g. in custom build scripts or SSR):
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { getRoutes } from '@van1s1mys/ai-router-plugin-webpack';
|
|
61
|
+
|
|
62
|
+
const routes = getRoutes(__dirname, { dirs: ['src/pages'] });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Route annotations
|
|
66
|
+
|
|
67
|
+
Add `@ai-route` comments to page files for richer metadata:
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
// @ai-route title="Pricing" description="plans, cost, billing, subscription"
|
|
71
|
+
|
|
72
|
+
export default function PricingPage() { ... }
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## How it works
|
|
76
|
+
|
|
77
|
+
The plugin writes a generated module to `node_modules/.cache/ai-router/routes.js` and rewrites `virtual:ai-router` imports to point to it. Routes are re-generated on watch rebuilds.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
[MIT](../../LICENSE) © [IvanMalkS](https://github.com/IvanMalkS)
|
package/dist/index.cjs
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * webpack plugin for ai-router.\n *\n * Scans page directories at build time and aliases `virtual:ai-router`\n * to a generated routes module in `node_modules/.cache/ai-router/`.\n *\n * @example\n * ```js\n * // webpack.config.js\n * const { AiRouterPlugin } = require('ai-router-plugin-webpack');\n *\n * module.exports = {\n * plugins: [\n * new AiRouterPlugin({ dirs: ['src/pages'] }),\n * ],\n * };\n * ```\n *\n * ```ts\n * // app.ts\n * import { routes } from 'virtual:ai-router';\n * import { SmartRouter } from 'ai-router';\n *\n * const router = new SmartRouter({ routes });\n * ```\n *\n * @module\n */\n\nimport fs from 'fs';\nimport path from 'path';\nimport {\n scanRoutes,\n generateRoutesModule,\n VIRTUAL_MODULE_ID,\n type PluginOptions,\n} from '@ai-router/shared';\n\nexport type { PluginOptions } from '@ai-router/shared';\n\n/** @internal */\nconst PLUGIN_NAME = 'AiRouterPlugin';\n\n/** Minimal webpack compiler interface — avoids a hard dependency on `@types/webpack`. */\ninterface WebpackCompiler {\n context: string;\n options: {\n resolve?: {\n alias?: Record<string, string>;\n };\n };\n hooks: {\n beforeCompile: { tap(name: string, fn: () => void): void };\n environment: { tap(name: string, fn: () => void): void };\n watchRun: { tap(name: string, fn: () => void): void };\n normalModuleFactory: {\n tap(name: string, fn: (factory: NormalModuleFactory) => void): void;\n };\n };\n}\n\n/** Minimal NormalModuleFactory interface for request rewriting. */\ninterface NormalModuleFactory {\n hooks: {\n beforeResolve: {\n tap(name: string, fn: (resolveData: { request: string }) => void): void;\n };\n };\n}\n\n/**\n * webpack plugin that generates route config from the file system.\n *\n * Writes a generated module to `node_modules/.cache/ai-router/routes.js`\n * and aliases `virtual:ai-router` to it. Re-generates on watch rebuilds.\n *\n * @example\n * ```js\n * // Auto-scan + manual overrides\n * new AiRouterPlugin({\n * dirs: ['src/pages'],\n * routes: [\n * { path: '/pricing', title: 'Pricing', description: 'cost, plans' },\n * ],\n * });\n * ```\n */\nexport class AiRouterPlugin {\n private options: PluginOptions;\n\n /**\n * @param options - Directories to scan, file extensions, exclusions, and manual routes.\n */\n constructor(options: PluginOptions = {}) {\n this.options = options;\n }\n\n /**\n * Called by webpack to apply the plugin to the compiler.\n * @param compiler - webpack compiler instance.\n */\n apply(compiler: WebpackCompiler) {\n const root: string = compiler.context;\n const cacheDir = path.join(root, 'node_modules', '.cache', 'ai-router');\n const generatedFile = path.join(cacheDir, 'routes.js');\n const generatedDts = path.join(cacheDir, 'routes.d.ts');\n\n const writeRoutes = () => {\n fs.mkdirSync(cacheDir, { recursive: true });\n\n const routes = scanRoutes(root, this.options);\n fs.writeFileSync(generatedFile, generateRoutesModule(routes), 'utf-8');\n fs.writeFileSync(\n generatedDts,\n 'export declare const routes: Array<{ path: string; title: string; description?: string }>;\\n',\n 'utf-8',\n );\n };\n\n // Generate before compilation starts\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, () => {\n writeRoutes();\n });\n\n // Rewrite `virtual:ai-router` → generated file path before webpack\n // attempts to resolve or read it. This avoids the \"UnhandledSchemeError\"\n // that webpack 5 throws for the `virtual:` URI scheme.\n compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (factory) => {\n factory.hooks.beforeResolve.tap(PLUGIN_NAME, (resolveData) => {\n if (resolveData.request === VIRTUAL_MODULE_ID) {\n resolveData.request = generatedFile;\n }\n });\n });\n\n // Re-generate on watch rebuild\n if (this.options.dirs?.length) {\n compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {\n writeRoutes();\n });\n }\n }\n}\n\n/**\n * Standalone helper to get routes without the webpack plugin.\n *\n * Useful for custom build scripts or SSR setups where you need\n * the route array directly.\n *\n * @param root - Absolute path to the project root.\n * @param options - Scanning configuration.\n * @returns Array of discovered routes.\n *\n * @example\n * ```ts\n * const routes = getRoutes(__dirname, { dirs: ['src/pages'] });\n * ```\n */\nexport function getRoutes(root: string, options: PluginOptions = {}) {\n return scanRoutes(root, options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6BA,gBAAe;AACf,kBAAiB;AACjB,oBAKO;AAKP,IAAM,cAAc;AA8Cb,IAAM,iBAAN,MAAqB;AAAA;AAAA;AAAA;AAAA,EAM1B,YAAY,UAAyB,CAAC,GAAG;AACvC,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,UAA2B;AAC/B,UAAM,OAAe,SAAS;AAC9B,UAAM,WAAW,YAAAA,QAAK,KAAK,MAAM,gBAAgB,UAAU,WAAW;AACtE,UAAM,gBAAgB,YAAAA,QAAK,KAAK,UAAU,WAAW;AACrD,UAAM,eAAe,YAAAA,QAAK,KAAK,UAAU,aAAa;AAEtD,UAAM,cAAc,MAAM;AACxB,gBAAAC,QAAG,UAAU,UAAU,EAAE,WAAW,KAAK,CAAC;AAE1C,YAAM,aAAS,0BAAW,MAAM,KAAK,OAAO;AAC5C,gBAAAA,QAAG,cAAc,mBAAe,oCAAqB,MAAM,GAAG,OAAO;AACrE,gBAAAA,QAAG;AAAA,QACD;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAGA,aAAS,MAAM,cAAc,IAAI,aAAa,MAAM;AAClD,kBAAY;AAAA,IACd,CAAC;AAKD,aAAS,MAAM,oBAAoB,IAAI,aAAa,CAAC,YAAY;AAC/D,cAAQ,MAAM,cAAc,IAAI,aAAa,CAAC,gBAAgB;AAC5D,YAAI,YAAY,YAAY,iCAAmB;AAC7C,sBAAY,UAAU;AAAA,QACxB;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAGD,QAAI,KAAK,QAAQ,MAAM,QAAQ;AAC7B,eAAS,MAAM,SAAS,IAAI,aAAa,MAAM;AAC7C,oBAAY;AAAA,MACd,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAiBO,SAAS,UAAU,MAAc,UAAyB,CAAC,GAAG;AACnE,aAAO,0BAAW,MAAM,OAAO;AACjC;","names":["path","fs"]}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * webpack plugin for ai-router.\n *\n * Scans page directories at build time and aliases `virtual:ai-router`\n * to a generated routes module in `node_modules/.cache/ai-router/`.\n *\n * @example\n * ```js\n * // webpack.config.js\n * const { AiRouterPlugin } = require('ai-router-plugin-webpack');\n *\n * module.exports = {\n * plugins: [\n * new AiRouterPlugin({ dirs: ['src/pages'] }),\n * ],\n * };\n * ```\n *\n * ```ts\n * // app.ts\n * import { routes } from 'virtual:ai-router';\n * import { SmartRouter } from 'ai-router';\n *\n * const router = new SmartRouter({ routes });\n * ```\n *\n * @module\n */\n\nimport fs from 'fs';\nimport path from 'path';\nimport {\n scanRoutes,\n generateRoutesModule,\n VIRTUAL_MODULE_ID,\n type PluginOptions,\n} from '@ai-router/shared';\n\nexport type { PluginOptions } from '@ai-router/shared';\n\n/** @internal */\nconst PLUGIN_NAME = 'AiRouterPlugin';\n\n/** Minimal webpack compiler interface — avoids a hard dependency on `@types/webpack`. */\ninterface WebpackCompiler {\n context: string;\n options: {\n resolve?: {\n alias?: Record<string, string>;\n };\n };\n hooks: {\n beforeCompile: { tap(name: string, fn: () => void): void };\n environment: { tap(name: string, fn: () => void): void };\n watchRun: { tap(name: string, fn: () => void): void };\n normalModuleFactory: {\n tap(name: string, fn: (factory: NormalModuleFactory) => void): void;\n };\n };\n}\n\n/** Minimal NormalModuleFactory interface for request rewriting. */\ninterface NormalModuleFactory {\n hooks: {\n beforeResolve: {\n tap(name: string, fn: (resolveData: { request: string }) => void): void;\n };\n };\n}\n\n/**\n * webpack plugin that generates route config from the file system.\n *\n * Writes a generated module to `node_modules/.cache/ai-router/routes.js`\n * and aliases `virtual:ai-router` to it. Re-generates on watch rebuilds.\n *\n * @example\n * ```js\n * // Auto-scan + manual overrides\n * new AiRouterPlugin({\n * dirs: ['src/pages'],\n * routes: [\n * { path: '/pricing', title: 'Pricing', description: 'cost, plans' },\n * ],\n * });\n * ```\n */\nexport class AiRouterPlugin {\n private options: PluginOptions;\n\n /**\n * @param options - Directories to scan, file extensions, exclusions, and manual routes.\n */\n constructor(options: PluginOptions = {}) {\n this.options = options;\n }\n\n /**\n * Called by webpack to apply the plugin to the compiler.\n * @param compiler - webpack compiler instance.\n */\n apply(compiler: WebpackCompiler) {\n const root: string = compiler.context;\n const cacheDir = path.join(root, 'node_modules', '.cache', 'ai-router');\n const generatedFile = path.join(cacheDir, 'routes.js');\n const generatedDts = path.join(cacheDir, 'routes.d.ts');\n\n const writeRoutes = () => {\n fs.mkdirSync(cacheDir, { recursive: true });\n\n const routes = scanRoutes(root, this.options);\n fs.writeFileSync(generatedFile, generateRoutesModule(routes), 'utf-8');\n fs.writeFileSync(\n generatedDts,\n 'export declare const routes: Array<{ path: string; title: string; description?: string }>;\\n',\n 'utf-8',\n );\n };\n\n // Generate before compilation starts\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, () => {\n writeRoutes();\n });\n\n // Rewrite `virtual:ai-router` → generated file path before webpack\n // attempts to resolve or read it. This avoids the \"UnhandledSchemeError\"\n // that webpack 5 throws for the `virtual:` URI scheme.\n compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (factory) => {\n factory.hooks.beforeResolve.tap(PLUGIN_NAME, (resolveData) => {\n if (resolveData.request === VIRTUAL_MODULE_ID) {\n resolveData.request = generatedFile;\n }\n });\n });\n\n // Re-generate on watch rebuild\n if (this.options.dirs?.length) {\n compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {\n writeRoutes();\n });\n }\n }\n}\n\n/**\n * Standalone helper to get routes without the webpack plugin.\n *\n * Useful for custom build scripts or SSR setups where you need\n * the route array directly.\n *\n * @param root - Absolute path to the project root.\n * @param options - Scanning configuration.\n * @returns Array of discovered routes.\n *\n * @example\n * ```ts\n * const routes = getRoutes(__dirname, { dirs: ['src/pages'] });\n * ```\n */\nexport function getRoutes(root: string, options: PluginOptions = {}) {\n return scanRoutes(root, options);\n}\n"],"mappings":";AA6BA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AAKP,IAAM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