@van1s1mys/ai-router-plugin-webpack 1.0.0
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/dist/index.cjs +92 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +84 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AiRouterPlugin: () => AiRouterPlugin,
|
|
34
|
+
getRoutes: () => getRoutes
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
38
|
+
var import_path = __toESM(require("path"), 1);
|
|
39
|
+
var import_shared = require("@ai-router/shared");
|
|
40
|
+
var PLUGIN_NAME = "AiRouterPlugin";
|
|
41
|
+
var AiRouterPlugin = class {
|
|
42
|
+
/**
|
|
43
|
+
* @param options - Directories to scan, file extensions, exclusions, and manual routes.
|
|
44
|
+
*/
|
|
45
|
+
constructor(options = {}) {
|
|
46
|
+
this.options = options;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Called by webpack to apply the plugin to the compiler.
|
|
50
|
+
* @param compiler - webpack compiler instance.
|
|
51
|
+
*/
|
|
52
|
+
apply(compiler) {
|
|
53
|
+
const root = compiler.context;
|
|
54
|
+
const cacheDir = import_path.default.join(root, "node_modules", ".cache", "ai-router");
|
|
55
|
+
const generatedFile = import_path.default.join(cacheDir, "routes.js");
|
|
56
|
+
const generatedDts = import_path.default.join(cacheDir, "routes.d.ts");
|
|
57
|
+
const writeRoutes = () => {
|
|
58
|
+
import_fs.default.mkdirSync(cacheDir, { recursive: true });
|
|
59
|
+
const routes = (0, import_shared.scanRoutes)(root, this.options);
|
|
60
|
+
import_fs.default.writeFileSync(generatedFile, (0, import_shared.generateRoutesModule)(routes), "utf-8");
|
|
61
|
+
import_fs.default.writeFileSync(
|
|
62
|
+
generatedDts,
|
|
63
|
+
"export declare const routes: Array<{ path: string; title: string; description?: string }>;\n",
|
|
64
|
+
"utf-8"
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
compiler.hooks.beforeCompile.tap(PLUGIN_NAME, () => {
|
|
68
|
+
writeRoutes();
|
|
69
|
+
});
|
|
70
|
+
compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (factory) => {
|
|
71
|
+
factory.hooks.beforeResolve.tap(PLUGIN_NAME, (resolveData) => {
|
|
72
|
+
if (resolveData.request === import_shared.VIRTUAL_MODULE_ID) {
|
|
73
|
+
resolveData.request = generatedFile;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
if (this.options.dirs?.length) {
|
|
78
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {
|
|
79
|
+
writeRoutes();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function getRoutes(root, options = {}) {
|
|
85
|
+
return (0, import_shared.scanRoutes)(root, options);
|
|
86
|
+
}
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
AiRouterPlugin,
|
|
90
|
+
getRoutes
|
|
91
|
+
});
|
|
92
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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.d.cts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as _ai_router_shared from '@ai-router/shared';
|
|
2
|
+
import { PluginOptions } from '@ai-router/shared';
|
|
3
|
+
export { PluginOptions } from '@ai-router/shared';
|
|
4
|
+
|
|
5
|
+
/** Minimal webpack compiler interface — avoids a hard dependency on `@types/webpack`. */
|
|
6
|
+
interface WebpackCompiler {
|
|
7
|
+
context: string;
|
|
8
|
+
options: {
|
|
9
|
+
resolve?: {
|
|
10
|
+
alias?: Record<string, string>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
hooks: {
|
|
14
|
+
beforeCompile: {
|
|
15
|
+
tap(name: string, fn: () => void): void;
|
|
16
|
+
};
|
|
17
|
+
environment: {
|
|
18
|
+
tap(name: string, fn: () => void): void;
|
|
19
|
+
};
|
|
20
|
+
watchRun: {
|
|
21
|
+
tap(name: string, fn: () => void): void;
|
|
22
|
+
};
|
|
23
|
+
normalModuleFactory: {
|
|
24
|
+
tap(name: string, fn: (factory: NormalModuleFactory) => void): void;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Minimal NormalModuleFactory interface for request rewriting. */
|
|
29
|
+
interface NormalModuleFactory {
|
|
30
|
+
hooks: {
|
|
31
|
+
beforeResolve: {
|
|
32
|
+
tap(name: string, fn: (resolveData: {
|
|
33
|
+
request: string;
|
|
34
|
+
}) => void): void;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* webpack plugin that generates route config from the file system.
|
|
40
|
+
*
|
|
41
|
+
* Writes a generated module to `node_modules/.cache/ai-router/routes.js`
|
|
42
|
+
* and aliases `virtual:ai-router` to it. Re-generates on watch rebuilds.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* // Auto-scan + manual overrides
|
|
47
|
+
* new AiRouterPlugin({
|
|
48
|
+
* dirs: ['src/pages'],
|
|
49
|
+
* routes: [
|
|
50
|
+
* { path: '/pricing', title: 'Pricing', description: 'cost, plans' },
|
|
51
|
+
* ],
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare class AiRouterPlugin {
|
|
56
|
+
private options;
|
|
57
|
+
/**
|
|
58
|
+
* @param options - Directories to scan, file extensions, exclusions, and manual routes.
|
|
59
|
+
*/
|
|
60
|
+
constructor(options?: PluginOptions);
|
|
61
|
+
/**
|
|
62
|
+
* Called by webpack to apply the plugin to the compiler.
|
|
63
|
+
* @param compiler - webpack compiler instance.
|
|
64
|
+
*/
|
|
65
|
+
apply(compiler: WebpackCompiler): void;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Standalone helper to get routes without the webpack plugin.
|
|
69
|
+
*
|
|
70
|
+
* Useful for custom build scripts or SSR setups where you need
|
|
71
|
+
* the route array directly.
|
|
72
|
+
*
|
|
73
|
+
* @param root - Absolute path to the project root.
|
|
74
|
+
* @param options - Scanning configuration.
|
|
75
|
+
* @returns Array of discovered routes.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const routes = getRoutes(__dirname, { dirs: ['src/pages'] });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function getRoutes(root: string, options?: PluginOptions): _ai_router_shared.ScannedRoute[];
|
|
83
|
+
|
|
84
|
+
export { AiRouterPlugin, getRoutes };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as _ai_router_shared from '@ai-router/shared';
|
|
2
|
+
import { PluginOptions } from '@ai-router/shared';
|
|
3
|
+
export { PluginOptions } from '@ai-router/shared';
|
|
4
|
+
|
|
5
|
+
/** Minimal webpack compiler interface — avoids a hard dependency on `@types/webpack`. */
|
|
6
|
+
interface WebpackCompiler {
|
|
7
|
+
context: string;
|
|
8
|
+
options: {
|
|
9
|
+
resolve?: {
|
|
10
|
+
alias?: Record<string, string>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
hooks: {
|
|
14
|
+
beforeCompile: {
|
|
15
|
+
tap(name: string, fn: () => void): void;
|
|
16
|
+
};
|
|
17
|
+
environment: {
|
|
18
|
+
tap(name: string, fn: () => void): void;
|
|
19
|
+
};
|
|
20
|
+
watchRun: {
|
|
21
|
+
tap(name: string, fn: () => void): void;
|
|
22
|
+
};
|
|
23
|
+
normalModuleFactory: {
|
|
24
|
+
tap(name: string, fn: (factory: NormalModuleFactory) => void): void;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Minimal NormalModuleFactory interface for request rewriting. */
|
|
29
|
+
interface NormalModuleFactory {
|
|
30
|
+
hooks: {
|
|
31
|
+
beforeResolve: {
|
|
32
|
+
tap(name: string, fn: (resolveData: {
|
|
33
|
+
request: string;
|
|
34
|
+
}) => void): void;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* webpack plugin that generates route config from the file system.
|
|
40
|
+
*
|
|
41
|
+
* Writes a generated module to `node_modules/.cache/ai-router/routes.js`
|
|
42
|
+
* and aliases `virtual:ai-router` to it. Re-generates on watch rebuilds.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* // Auto-scan + manual overrides
|
|
47
|
+
* new AiRouterPlugin({
|
|
48
|
+
* dirs: ['src/pages'],
|
|
49
|
+
* routes: [
|
|
50
|
+
* { path: '/pricing', title: 'Pricing', description: 'cost, plans' },
|
|
51
|
+
* ],
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare class AiRouterPlugin {
|
|
56
|
+
private options;
|
|
57
|
+
/**
|
|
58
|
+
* @param options - Directories to scan, file extensions, exclusions, and manual routes.
|
|
59
|
+
*/
|
|
60
|
+
constructor(options?: PluginOptions);
|
|
61
|
+
/**
|
|
62
|
+
* Called by webpack to apply the plugin to the compiler.
|
|
63
|
+
* @param compiler - webpack compiler instance.
|
|
64
|
+
*/
|
|
65
|
+
apply(compiler: WebpackCompiler): void;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Standalone helper to get routes without the webpack plugin.
|
|
69
|
+
*
|
|
70
|
+
* Useful for custom build scripts or SSR setups where you need
|
|
71
|
+
* the route array directly.
|
|
72
|
+
*
|
|
73
|
+
* @param root - Absolute path to the project root.
|
|
74
|
+
* @param options - Scanning configuration.
|
|
75
|
+
* @returns Array of discovered routes.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const routes = getRoutes(__dirname, { dirs: ['src/pages'] });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function getRoutes(root: string, options?: PluginOptions): _ai_router_shared.ScannedRoute[];
|
|
83
|
+
|
|
84
|
+
export { AiRouterPlugin, getRoutes };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import {
|
|
5
|
+
scanRoutes,
|
|
6
|
+
generateRoutesModule,
|
|
7
|
+
VIRTUAL_MODULE_ID
|
|
8
|
+
} from "@ai-router/shared";
|
|
9
|
+
var PLUGIN_NAME = "AiRouterPlugin";
|
|
10
|
+
var AiRouterPlugin = class {
|
|
11
|
+
/**
|
|
12
|
+
* @param options - Directories to scan, file extensions, exclusions, and manual routes.
|
|
13
|
+
*/
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.options = options;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Called by webpack to apply the plugin to the compiler.
|
|
19
|
+
* @param compiler - webpack compiler instance.
|
|
20
|
+
*/
|
|
21
|
+
apply(compiler) {
|
|
22
|
+
const root = compiler.context;
|
|
23
|
+
const cacheDir = path.join(root, "node_modules", ".cache", "ai-router");
|
|
24
|
+
const generatedFile = path.join(cacheDir, "routes.js");
|
|
25
|
+
const generatedDts = path.join(cacheDir, "routes.d.ts");
|
|
26
|
+
const writeRoutes = () => {
|
|
27
|
+
fs.mkdirSync(cacheDir, { recursive: true });
|
|
28
|
+
const routes = scanRoutes(root, this.options);
|
|
29
|
+
fs.writeFileSync(generatedFile, generateRoutesModule(routes), "utf-8");
|
|
30
|
+
fs.writeFileSync(
|
|
31
|
+
generatedDts,
|
|
32
|
+
"export declare const routes: Array<{ path: string; title: string; description?: string }>;\n",
|
|
33
|
+
"utf-8"
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
compiler.hooks.beforeCompile.tap(PLUGIN_NAME, () => {
|
|
37
|
+
writeRoutes();
|
|
38
|
+
});
|
|
39
|
+
compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (factory) => {
|
|
40
|
+
factory.hooks.beforeResolve.tap(PLUGIN_NAME, (resolveData) => {
|
|
41
|
+
if (resolveData.request === VIRTUAL_MODULE_ID) {
|
|
42
|
+
resolveData.request = generatedFile;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
if (this.options.dirs?.length) {
|
|
47
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {
|
|
48
|
+
writeRoutes();
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
function getRoutes(root, options = {}) {
|
|
54
|
+
return scanRoutes(root, options);
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
AiRouterPlugin,
|
|
58
|
+
getRoutes
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,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,KAAK,KAAK,MAAM,gBAAgB,UAAU,WAAW;AACtE,UAAM,gBAAgB,KAAK,KAAK,UAAU,WAAW;AACrD,UAAM,eAAe,KAAK,KAAK,UAAU,aAAa;AAEtD,UAAM,cAAc,MAAM;AACxB,SAAG,UAAU,UAAU,EAAE,WAAW,KAAK,CAAC;AAE1C,YAAM,SAAS,WAAW,MAAM,KAAK,OAAO;AAC5C,SAAG,cAAc,eAAe,qBAAqB,MAAM,GAAG,OAAO;AACrE,SAAG;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,mBAAmB;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,SAAO,WAAW,MAAM,OAAO;AACjC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@van1s1mys/ai-router-plugin-webpack",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "webpack plugin for ai-router — auto-scan pages and generate route config",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"webpack",
|
|
30
|
+
"webpack-plugin",
|
|
31
|
+
"ai-router"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@ai-router/shared": "workspace:*"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"webpack": ">=5.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|