@pithyjs/vite-plugin 0.2.0-beta.23 → 0.2.0-beta.25

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.
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/buildPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAmDF,MAAM,eAAe,GAAmD;IACtE,YAAY,EAAE,IAAI;IAClB,eAAe,EAAE,OAAO;IACxB,gBAAgB,EAAE,QAAQ;IAC1B,SAAS,EAAE,QAAQ;IACnB,MAAM,EAAE,QAAQ;CACjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,oJAAoJ;AACpJ;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAS,cAAc,CACrB,EAAU,EACV,aAGQ,EACR,OAAoB,IAAI,GAAG,EAAE;IAE7B,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;IAC9B,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAEb,MAAM,IAAI,GAAG,aAAa,CAAC,EAAE,CAAC,CAAC;IAC/B,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IAExB,qEAAqE;IACrE,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAC;IAEzE,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CACrC,cAAc,CAAC,QAAQ,EAAE,aAAa,EAAE,IAAI,CAAC,CAC9C,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,UAA6B,EAAE;IACxD,MAAM,MAAM,GAC6B;QACvC,GAAG,eAAe;QAClB,GAAG,OAAO;KACX,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,aAAa;QAC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AAI,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;wCAChC,OAAO,cAAc,CAAC,EAAE,EAAE,aAAa,CAAC;4CACtC,CAAC,CAAC,SAAS;4CACX,CAAC,CAAC,QAAQ,CAAC;oCACf,CAAC;oCAED,OAAO,SAAS,CAAC;gCACnB,CAAC;gCACH,CAAC,CAAC,SAAS;yBACd;qBACF;oBACD,6DAA6D;oBAC7D,mEAAmE;oBACnE,mBAAmB;oBACnB,GAAG,CAAC,MAAM,CAAC,SAAS,KAAK,SAAS;wBAChC,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE;wBACjC,CAAC,CAAC,EAAE,CAAC;oBACP,MAAM,EAAE,MAAM,CAAC,MAAM;iBACtB;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.vite.build-plugin\",\n \"title\": \"PithyJS Build Optimization Plugin\",\n \"category\": \"plugin\",\n \"description\": \"Build optimization plugin providing chunk naming strategies, vendor splitting, and route-based code splitting.\",\n \"since\": \"0.0.1\",\n \"stability\": \"stable\",\n \"testing\": {\n \"unit\": { \"required\": false, \"covered\": false },\n \"integration\": { \"required\": true, \"covered\": false },\n \"e2e\": { \"required\": false, \"covered\": false }\n }\n}\n*/\n\nimport type { Plugin, UserConfig } from 'vite';\n\n/**\n * Build plugin options\n */\ninterface PithyBuildOptions {\n /**\n * Enable vendor chunk splitting for framework packages\n * @default true\n */\n vendorChunks?: boolean;\n\n /**\n * Prefix for lazy-loaded component chunks\n * @default \"lazy-\"\n */\n lazyChunkPrefix?: string;\n\n /**\n * Prefix for route chunks\n * @default \"route-\"\n */\n routeChunkPrefix?: string;\n\n /**\n * Routes directory name (used for route chunk detection)\n * @default \"routes\"\n */\n routesDir?: string;\n\n /**\n * Emit source maps (`build.sourcemap`). Leave unset to keep the app's own\n * `build.sourcemap`, or Vite's default (off).\n *\n * Off by default because a map embeds the original source (`sourcesContent`)\n * and a production build publishes it next to the bundle. Pass `true` only\n * when shipping that source is intended.\n *\n * @default undefined (the app's `build.sourcemap`, else off)\n */\n sourcemap?: boolean;\n\n /**\n * Build target\n * @default \"esnext\"\n */\n target?: string;\n}\n\nconst DEFAULT_OPTIONS: Required<Omit<PithyBuildOptions, 'sourcemap'>> = {\n vendorChunks: true,\n lazyChunkPrefix: 'lazy-',\n routeChunkPrefix: 'route-',\n routesDir: 'routes',\n target: 'esnext',\n};\n\n/**\n * Vite plugin for PithyJS build optimizations.\n *\n * Provides:\n * - Chunk naming strategy for lazy-loaded components\n * - Route-based chunk naming\n * - Framework vendor chunk splitting\n * - Optimized asset naming\n *\n * @param options - Build configuration options\n * @returns Vite plugin\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { pithyBuild } from '@pithyjs/vite-plugin';\n *\n * export default defineConfig({\n * plugins: [\n * pithyBuild(),\n * ],\n * });\n * ```\n */\n/** @codexApi {\"parent\":\"pithy.vite.build-plugin\",\"name\":\"pithyBuild\",\"stability\":\"stable\",\"signature\":\"(options?: PithyBuildOptions) => Plugin\"} */\n/**\n * Whether every path from an entry to `id` crosses at least one `import()`.\n *\n * This is the question the `vendor` catch-all has to ask. A module reached only\n * through a dynamic import is code the app deliberately deferred; naming a\n * chunk for it merges it into a chunk the entry preloads and the deferral is\n * undone. Shiki is the case that exposed this — its WASM engine and ~300\n * grammars — but `parse5` is the same shape, and so is every future lazy\n * dependency. Hence a general rule rather than a list of names.\n *\n * STATIC IMPORTERS ALONE ARE NOT ENOUGH, which is the subtle half: a lazy\n * package's own closure has static importers — `hast-util-to-html` is\n * statically imported by `@shikijs/core`, which is itself only dynamically\n * imported. Asking \"does it have a static importer?\" keeps the closure in\n * `vendor` and leaves the bug in place. The walk has to continue upward until\n * it reaches an entry or a dynamic boundary.\n *\n * A module with no importers at all is an entry, so not deferred. A module\n * shared with eagerly-reachable code returns false and stays in `vendor`,\n * which is what stops it being duplicated into every chunk that wants it.\n */\nfunction isDeferredOnly(\n id: string,\n getModuleInfo: (id: string) => {\n importers: readonly string[];\n dynamicImporters: readonly string[];\n } | null,\n seen: Set<string> = new Set()\n): boolean {\n if (seen.has(id)) return true;\n seen.add(id);\n\n const info = getModuleInfo(id);\n if (!info) return false;\n\n // No static importer, but something `import()`s it: a deferred root.\n if (info.importers.length === 0) return info.dynamicImporters.length > 0;\n\n return info.importers.every(importer =>\n isDeferredOnly(importer, getModuleInfo, seen)\n );\n}\n\nexport function pithyBuild(options: PithyBuildOptions = {}): Plugin {\n const config: Required<Omit<PithyBuildOptions, 'sourcemap'>> &\n Pick<PithyBuildOptions, 'sourcemap'> = {\n ...DEFAULT_OPTIONS,\n ...options,\n };\n\n return {\n name: 'pithy:build',\n // Apply only during build\n apply: 'build',\n\n config(): UserConfig {\n return {\n build: {\n rollupOptions: {\n output: {\n // Chunk naming strategy\n chunkFileNames: chunkInfo => {\n const routesPattern = `/${config.routesDir}/`;\n\n // Route chunks get route-based names\n if (chunkInfo.facadeModuleId?.includes(routesPattern)) {\n const routePath =\n chunkInfo.facadeModuleId\n .split(routesPattern)[1]\n ?.replace(/\\+page\\.(ts|js|pithy)$/, '')\n .replace(/\\//g, '-')\n .replace(/-$/, '')\n // Sanitize dynamic route segments: [id] -> _id_, [...slug] -> _slug_\n .replace(/\\[\\.\\.\\.([^\\]]+)\\]/g, '_$1_')\n .replace(/\\[([^\\]]+)\\]/g, '_$1_') || 'index';\n return `assets/${config.routeChunkPrefix}${routePath}-[hash].js`;\n }\n\n // Vendor chunks keep their names\n if (chunkInfo.name?.startsWith('vendor')) {\n return `assets/[name]-[hash].js`;\n }\n\n // Dynamic entry chunks (from dynamic imports) are lazy-loaded components\n // Detect by: isDynamicEntry=true AND not a route AND not a vendor\n if (chunkInfo.isDynamicEntry && chunkInfo.facadeModuleId) {\n const moduleId = chunkInfo.facadeModuleId;\n const isVendor =\n moduleId.includes('node_modules') ||\n moduleId.includes('@pithyjs/') ||\n moduleId.includes('packages/core/') ||\n moduleId.includes('packages/signals/') ||\n moduleId.includes('packages/router/') ||\n moduleId.includes('packages/design-system/');\n\n if (!isVendor) {\n // This is a lazy-loaded component chunk\n return `assets/${config.lazyChunkPrefix}[name]-[hash].js`;\n }\n }\n\n // Default chunk naming\n return 'assets/[name]-[hash].js';\n },\n\n // Entry chunks\n entryFileNames: 'assets/[name]-[hash].js',\n\n // Assets like CSS\n assetFileNames: 'assets/[name]-[hash].[ext]',\n\n // Manual chunk splitting for better caching\n manualChunks: config.vendorChunks\n ? (id, { getModuleInfo }) => {\n // Core framework chunks - cached long-term\n // These patterns work both for local development (packages/)\n // and npm installs (node_modules/@pithyjs/)\n if (\n id.includes('@pithyjs/core') ||\n id.includes('packages/core/')\n ) {\n return 'vendor-pithy-core';\n }\n if (\n id.includes('@pithyjs/signals') ||\n id.includes('packages/signals/')\n ) {\n return 'vendor-pithy-signals';\n }\n if (\n id.includes('@pithyjs/router') ||\n id.includes('packages/router/')\n ) {\n return 'vendor-pithy-router';\n }\n if (\n id.includes('@pithyjs/design-system') ||\n id.includes('packages/design-system/')\n ) {\n return 'vendor-pithy-design';\n }\n\n // Node modules vendor chunk — EXCEPT anything the app only\n // reaches through `import()`. `vendor` is imported by the\n // entry and preloaded by the HTML, so naming a chunk for a\n // deferred module undoes the deferral.\n if (id.includes('node_modules')) {\n return isDeferredOnly(id, getModuleInfo)\n ? undefined\n : 'vendor';\n }\n\n return undefined;\n }\n : undefined,\n },\n },\n // Only when asked: a value here would override the app's own\n // `build.sourcemap`, and a default of `true` published every app's\n // original source.\n ...(config.sourcemap !== undefined\n ? { sourcemap: config.sourcemap }\n : {}),\n target: config.target,\n },\n };\n },\n };\n}\n"]}
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/buildPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAmDF,MAAM,eAAe,GAAmD;IACtE,YAAY,EAAE,IAAI;IAClB,eAAe,EAAE,OAAO;IACxB,gBAAgB,EAAE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S;wBAChC,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE;wBACjC,CAAC,CAAC,EAAE,CAAC;oBACP,MAAM,EAAE,MAAM,CAAC,MAAM;iBACtB;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.vite.build-plugin\",\n \"title\": \"PithyJS Build Optimization Plugin\",\n \"category\": \"plugin\",\n \"description\": \"Build optimization plugin providing chunk naming strategies, vendor splitting, and route-based code splitting.\",\n \"since\": \"0.0.1\",\n \"stability\": \"stable\",\n \"testing\": {\n \"unit\": { \"required\": false, \"covered\": false },\n \"integration\": { \"required\": true, \"covered\": false },\n \"e2e\": { \"required\": false, \"covered\": false }\n }\n}\n*/\n\nimport type { Plugin, UserConfig } from 'vite';\n\n/**\n * Build plugin options\n */\ninterface PithyBuildOptions {\n /**\n * Enable vendor chunk splitting for framework packages\n * @default true\n */\n vendorChunks?: boolean;\n\n /**\n * Prefix for lazy-loaded component chunks\n * @default \"lazy-\"\n */\n lazyChunkPrefix?: string;\n\n /**\n * Prefix for route chunks\n * @default \"route-\"\n */\n routeChunkPrefix?: string;\n\n /**\n * Routes directory name (used for route chunk detection)\n * @default \"routes\"\n */\n routesDir?: string;\n\n /**\n * Emit source maps (`build.sourcemap`). Leave unset to keep the app's own\n * `build.sourcemap`, or Vite's default (off).\n *\n * Off by default because a map embeds the original source (`sourcesContent`)\n * and a production build publishes it next to the bundle. Pass `true` only\n * when shipping that source is intended.\n *\n * @default undefined (the app's `build.sourcemap`, else off)\n */\n sourcemap?: boolean;\n\n /**\n * Build target\n * @default \"esnext\"\n */\n target?: string;\n}\n\nconst DEFAULT_OPTIONS: Required<Omit<PithyBuildOptions, 'sourcemap'>> = {\n vendorChunks: true,\n lazyChunkPrefix: 'lazy-',\n routeChunkPrefix: 'route-',\n routesDir: 'routes',\n target: 'esnext',\n};\n\n/**\n * Whether every path from an entry to `id` crosses at least one `import()`.\n *\n * This is the question the `vendor` catch-all has to ask. A module reached only\n * through a dynamic import is code the app deliberately deferred; naming a\n * chunk for it merges it into a chunk the entry preloads and the deferral is\n * undone. Shiki is the case that exposed this — its WASM engine and ~300\n * grammars — but `parse5` is the same shape, and so is every future lazy\n * dependency. Hence a general rule rather than a list of names.\n *\n * STATIC IMPORTERS ALONE ARE NOT ENOUGH, which is the subtle half: a lazy\n * package's own closure has static importers — `hast-util-to-html` is\n * statically imported by `@shikijs/core`, which is itself only dynamically\n * imported. Asking \"does it have a static importer?\" keeps the closure in\n * `vendor` and leaves the bug in place. The walk has to continue upward until\n * it reaches an entry or a dynamic boundary.\n *\n * A module with no importers at all is an entry, so not deferred. A module\n * shared with eagerly-reachable code returns false and stays in `vendor`,\n * which is what stops it being duplicated into every chunk that wants it.\n */\nfunction isDeferredOnly(\n id: string,\n getModuleInfo: (id: string) => {\n importers: readonly string[];\n dynamicImporters: readonly string[];\n } | null,\n seen: Set<string> = new Set()\n): boolean {\n if (seen.has(id)) return true;\n seen.add(id);\n\n const info = getModuleInfo(id);\n if (!info) return false;\n\n // No static importer, but something `import()`s it: a deferred root.\n if (info.importers.length === 0) return info.dynamicImporters.length > 0;\n\n return info.importers.every(importer =>\n isDeferredOnly(importer, getModuleInfo, seen)\n );\n}\n\n/**\n * Vite plugin for PithyJS build optimizations.\n *\n * Provides:\n * - Chunk naming strategy for lazy-loaded components\n * - Route-based chunk naming\n * - Framework vendor chunk splitting\n * - Optimized asset naming\n *\n * @param options - Build configuration options\n * @returns Vite plugin\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { pithyBuild } from '@pithyjs/vite-plugin';\n *\n * export default defineConfig({\n * plugins: [\n * pithyBuild(),\n * ],\n * });\n * ```\n */\n/** @codexApi {\"parent\":\"pithy.vite.build-plugin\",\"name\":\"pithyBuild\",\"stability\":\"stable\",\"signature\":\"(options?: PithyBuildOptions) => Plugin\"} */\nexport function pithyBuild(options: PithyBuildOptions = {}): Plugin {\n const config: Required<Omit<PithyBuildOptions, 'sourcemap'>> &\n Pick<PithyBuildOptions, 'sourcemap'> = {\n ...DEFAULT_OPTIONS,\n ...options,\n };\n\n return {\n name: 'pithy:build',\n // Apply only during build\n apply: 'build',\n\n config(): UserConfig {\n return {\n build: {\n rollupOptions: {\n output: {\n // Chunk naming strategy\n chunkFileNames: chunkInfo => {\n const routesPattern = `/${config.routesDir}/`;\n\n // Route chunks get route-based names\n if (chunkInfo.facadeModuleId?.includes(routesPattern)) {\n const routePath =\n chunkInfo.facadeModuleId\n .split(routesPattern)[1]\n ?.replace(/\\+page\\.(ts|js|pithy)$/, '')\n .replace(/\\//g, '-')\n .replace(/-$/, '')\n // Sanitize dynamic route segments: [id] -> _id_, [...slug] -> _slug_\n .replace(/\\[\\.\\.\\.([^\\]]+)\\]/g, '_$1_')\n .replace(/\\[([^\\]]+)\\]/g, '_$1_') || 'index';\n return `assets/${config.routeChunkPrefix}${routePath}-[hash].js`;\n }\n\n // Vendor chunks keep their names\n if (chunkInfo.name?.startsWith('vendor')) {\n return `assets/[name]-[hash].js`;\n }\n\n // Dynamic entry chunks (from dynamic imports) are lazy-loaded components\n // Detect by: isDynamicEntry=true AND not a route AND not a vendor\n if (chunkInfo.isDynamicEntry && chunkInfo.facadeModuleId) {\n const moduleId = chunkInfo.facadeModuleId;\n const isVendor =\n moduleId.includes('node_modules') ||\n moduleId.includes('@pithyjs/') ||\n moduleId.includes('packages/core/') ||\n moduleId.includes('packages/signals/') ||\n moduleId.includes('packages/router/') ||\n moduleId.includes('packages/design-system/');\n\n if (!isVendor) {\n // This is a lazy-loaded component chunk\n return `assets/${config.lazyChunkPrefix}[name]-[hash].js`;\n }\n }\n\n // Default chunk naming\n return 'assets/[name]-[hash].js';\n },\n\n // Entry chunks\n entryFileNames: 'assets/[name]-[hash].js',\n\n // Assets like CSS\n assetFileNames: 'assets/[name]-[hash].[ext]',\n\n // Manual chunk splitting for better caching\n manualChunks: config.vendorChunks\n ? (id, { getModuleInfo }) => {\n // Core framework chunks - cached long-term\n // These patterns work both for local development (packages/)\n // and npm installs (node_modules/@pithyjs/)\n if (\n id.includes('@pithyjs/core') ||\n id.includes('packages/core/')\n ) {\n return 'vendor-pithy-core';\n }\n if (\n id.includes('@pithyjs/signals') ||\n id.includes('packages/signals/')\n ) {\n return 'vendor-pithy-signals';\n }\n if (\n id.includes('@pithyjs/router') ||\n id.includes('packages/router/')\n ) {\n return 'vendor-pithy-router';\n }\n if (\n id.includes('@pithyjs/design-system') ||\n id.includes('packages/design-system/')\n ) {\n return 'vendor-pithy-design';\n }\n\n // Node modules vendor chunk — EXCEPT anything the app only\n // reaches through `import()`. `vendor` is imported by the\n // entry and preloaded by the HTML, so naming a chunk for a\n // deferred module undoes the deferral.\n if (id.includes('node_modules')) {\n return isDeferredOnly(id, getModuleInfo)\n ? undefined\n : 'vendor';\n }\n\n return undefined;\n }\n : undefined,\n },\n },\n // Only when asked: a value here would override the app's own\n // `build.sourcemap`, and a default of `true` published every app's\n // original source.\n ...(config.sourcemap !== undefined\n ? { sourcemap: config.sourcemap }\n : {}),\n target: config.target,\n },\n };\n },\n };\n}\n"]}
@@ -40,9 +40,10 @@ export declare function pithyPlugin(): Plugin;
40
40
  *
41
41
  * @param code - The `.pithy` source.
42
42
  * @param id - Its module id; a Vite query suffix is ignored.
43
+ * @param tsconfigRaw - esbuild tsconfig JSON, in place of the file lookup.
43
44
  * @returns The JavaScript module and its source map.
44
45
  */
45
- export declare function compilePithyModule(code: string, id: string): Promise<{
46
+ export declare function compilePithyModule(code: string, id: string, tsconfigRaw?: string): Promise<{
46
47
  code: string;
47
48
  map: Awaited<ReturnType<typeof transformWithEsbuild>>['map'];
48
49
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/pithyPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAIF,OAAO,EAAE,oBAAoB,EAAE,KAAK,MAAM,EAAmB,MAAM,MAAM,CAAC;AAE1E;;;;;;;;;;;;;;;;;;GAkBG;AACH,oHAAoH;AACpH,wBAAgB,WAAW,IAAI,MAAM,CAsCpC;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACtC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,OAAO,CAAC;IACT,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;CAC9D,CAAC,CAYD"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/pithyPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAIF,OAAO,EAAE,oBAAoB,EAAE,KAAK,MAAM,EAAmB,MAAM,MAAM,CAAC;AAE1E;;;;;;;;;;;;;;;;;;GAkBG;AACH,oHAAoH;AACpH,wBAAgB,WAAW,IAAI,MAAM,CAsCpC;AAED;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CACtC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC;IACT,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;CAC9D,CAAC,CAaD"}
@@ -79,9 +79,10 @@ export function pithyPlugin() {
79
79
  *
80
80
  * @param code - The `.pithy` source.
81
81
  * @param id - Its module id; a Vite query suffix is ignored.
82
+ * @param tsconfigRaw - esbuild tsconfig JSON, in place of the file lookup.
82
83
  * @returns The JavaScript module and its source map.
83
84
  */
84
- export async function compilePithyModule(code, id) {
85
+ export async function compilePithyModule(code, id, tsconfigRaw) {
85
86
  const filePath = id.split('?', 1)[0];
86
87
  const parsed = parsePithyFile(code);
87
88
  const filename = path.basename(filePath);
@@ -91,6 +92,7 @@ export async function compilePithyModule(code, id) {
91
92
  const tsCode = compileToModule(parsed, { filename, scopeId });
92
93
  const { code: jsCode, map } = await transformWithEsbuild(tsCode, id, {
93
94
  loader: 'ts',
95
+ ...(tsconfigRaw === undefined ? {} : { tsconfigRaw }),
94
96
  });
95
97
  return { code: jsCode, map };
96
98
  }
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/pithyPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAgC,MAAM,MAAM,CAAC;AAE1E;;;;;;;;;;;;;;;;;;GAkBG;AACH,oHAAoH;AACpH,MAAM,UAAU,WAAW;IACzB,OAAO;QACL,IAAI,EAAE,mBAAmB;QACzB,OAAO,EAAE,KAAK,EAAE,iCAAiC;QAEjD;;;;;;;WAOG;QACH,KAAK,CAAC,SAAS,CAAC,IAAY,EAAE,EAAU;YACtC,iFAAiF;YACjF,0EAA0E;YAC1E,4EAA4E;YAC5E,iFAAiF;YACjF,0CAA0C;YAC1C,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;gBAAE,OAAO;YACpD,OAAO,kBAAkB,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACtC,CAAC;QAED;;;;;;WAMG;QACH,eAAe,CAAC,GAAe;YAC7B,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;gBAChC,KAAK,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;oBAC9B,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;gBAC/C,CAAC;YACH,CAAC;QACH,CAAC;K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"plugin\",\n \"description\": \"Vite plugin that transforms .pithy single-file components into compiled TypeScript modules with HMR support.\",\n \"since\": \"0.0.1\",\n \"stability\": \"stable\",\n \"testing\": {\n \"unit\": { \"required\": true, \"covered\": false },\n \"integration\": { \"required\": true, \"covered\": false },\n \"e2e\": { \"required\": false, \"covered\": false }\n }\n}\n*/\n\nimport path from 'path';\nimport { compileToModule, parsePithyFile } from '@pithyjs/compiler';\nimport { transformWithEsbuild, type Plugin, type HmrContext } from 'vite';\n\n/**\n * Vite plugin to support `.pithy` single-file components (SFC).\n *\n * - Transforms `.pithy` files into compiled JavaScript modules (the generated\n * TypeScript is transpiled to JS via esbuild before being returned).\n * - Automatically scopes styles and templates.\n * - Supports hot module replacement (HMR) for faster development.\n *\n * @returns A Vite plugin that handles .pithy file transformation and HMR\n *\n * @example\n * ```ts\n * import { pithyPlugin } from '@pithyjs/vite-plugin';\n *\n * export default defineConfig({\n * plugins: [pithyPlugin()],\n * });\n * ```\n */\n/** @codexApi {\"parent\":\"pithy.vite.plugin\",\"name\":\"pithyPlugin\",\"stability\":\"stable\",\"signature\":\"() => Plugin\"} */\nexport function pithyPlugin(): Plugin {\n return {\n name: 'vite-plugin-pithy',\n enforce: 'pre', // Run early before other plugins\n\n /**\n * Transforms a `.pithy` file into a compiled JavaScript module: the compiler\n * emits a TypeScript module, which is transpiled to JS via esbuild here.\n *\n * @param code - Raw source code of the `.pithy` file\n * @param id - Full path or virtual ID of the file\n * @returns Compiled JavaScript code and a source map\n */\n async transform(code: string, id: string) {\n // Vite appends query suffixes to ids (e.g. `.pithy?import`, `.pithy?v=…`). Match\n // and derive the filename/scope id from the bare path (before the `?`) so\n // `.pithy` modules are always transformed and the scope id is stable across\n // reloads. `#` is left intact — module ids carry queries, not URL fragments, and\n // `#` can be a legitimate path character.\n if (!id.split('?', 1)[0].endsWith('.pithy')) return;\n return compilePithyModule(code, id);\n },\n\n /**\n * Handles hot module replacement (HMR) for `.pithy` files.\n *\n * Invalidates the module graph so Vite reloads updated components automatically.\n *\n * @param ctx - Vite HMR context\n */\n handleHotUpdate(ctx: HmrContext): void {\n if (ctx.file.endsWith('.pithy')) {\n for (const mod of ctx.modules) {\n ctx.server.moduleGraph.invalidateModule(mod);\n }\n }\n },\n };\n}\n\n/**\n * Compiles a `.pithy` file to the JavaScript module the rest of the chain sees.\n *\n * @param code - The `.pithy` source.\n * @param id - Its module id; a Vite query suffix is ignored.\n * @returns The JavaScript module and its source map.\n */\nexport async function compilePithyModule(\n code: string,\n id: string\n): Promise<{\n code: string;\n map: Awaited<ReturnType<typeof transformWithEsbuild>>['map'];\n}> {\n const filePath = id.split('?', 1)[0];\n const parsed = parsePithyFile(code);\n const filename = path.basename(filePath);\n const scopeId = generateScopeId(filePath);\n // The <script lang=\"ts\"> body may hold TS-only syntax and Vite's esbuild pass\n // does not run on `.pithy` ids, so transpile the generated module here.\n const tsCode = compileToModule(parsed, { filename, scopeId });\n const { code: jsCode, map } = await transformWithEsbuild(tsCode, id, {\n loader: 'ts',\n });\n return { code: jsCode, map };\n}\n\n/**\n * Generates a short, per-file-unique scope ID from a file path.\n *\n * Uses a djb2 hash of the WHOLE path rather than a base64 prefix: sibling files\n * share a long common path prefix, so slicing the first base64 chars produced\n * identical IDs for every component — making their scoped styles collide. The hash\n * spreads across the full path, and base36 keeps the id lowercase (the runtime\n * lowercases the `data-scope-<id>` attribute name, so the CSS must match).\n *\n * Example: \"src/components/Button.pithy\" → \"pithy-1a2b3c\"\n *\n * @param filepath - Full file path\n * @returns A short scope ID like `pithy-abc123`\n */\nfunction generateScopeId(filepath: string): string {\n let hash = 5381;\n for (let i = 0; i < filepath.length; i++) {\n hash = (((hash << 5) + hash) ^ filepath.charCodeAt(i)) >>> 0;\n }\n return 'pithy-' + hash.toString(36);\n}\n"]}
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/pithyPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF,OAAO,IAAI,MAAM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CAAC,CAAC;IACzC,MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC1C,8EAA8E;IAC9E,wEAAwE;IACxE,MAAM,MAAM,GAAG,eAAe,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAC9D,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,oBAAoB,CAAC,MAAM,EAAE,EAAE,EAAE;QACnE,MAAM,EAAE,IAAI;QACZ,GAAG,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC;KACtD,CAAC,CAAC;IACH,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAC/B,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAS,eAAe,CAAC,QAAgB;IACvC,IAAI,IAAI,GAAG,IAAI,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AACtC,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.vite.plugin\",\n \"title\": \"Vite Pithy Plugin\",\n \"category\": \"plugin\",\n \"description\": \"Vite plugin that transforms .pithy single-file components into compiled TypeScript modules with HMR support.\",\n \"since\": \"0.0.1\",\n \"stability\": \"stable\",\n \"testing\": {\n \"unit\": { \"required\": true, \"covered\": false },\n \"integration\": { \"required\": true, \"covered\": false },\n \"e2e\": { \"required\": false, \"covered\": false }\n }\n}\n*/\n\nimport path from 'path';\nimport { compileToModule, parsePithyFile } from '@pithyjs/compiler';\nimport { transformWithEsbuild, type Plugin, type HmrContext } from 'vite';\n\n/**\n * Vite plugin to support `.pithy` single-file components (SFC).\n *\n * - Transforms `.pithy` files into compiled JavaScript modules (the generated\n * TypeScript is transpiled to JS via esbuild before being returned).\n * - Automatically scopes styles and templates.\n * - Supports hot module replacement (HMR) for faster development.\n *\n * @returns A Vite plugin that handles .pithy file transformation and HMR\n *\n * @example\n * ```ts\n * import { pithyPlugin } from '@pithyjs/vite-plugin';\n *\n * export default defineConfig({\n * plugins: [pithyPlugin()],\n * });\n * ```\n */\n/** @codexApi {\"parent\":\"pithy.vite.plugin\",\"name\":\"pithyPlugin\",\"stability\":\"stable\",\"signature\":\"() => Plugin\"} */\nexport function pithyPlugin(): Plugin {\n return {\n name: 'vite-plugin-pithy',\n enforce: 'pre', // Run early before other plugins\n\n /**\n * Transforms a `.pithy` file into a compiled JavaScript module: the compiler\n * emits a TypeScript module, which is transpiled to JS via esbuild here.\n *\n * @param code - Raw source code of the `.pithy` file\n * @param id - Full path or virtual ID of the file\n * @returns Compiled JavaScript code and a source map\n */\n async transform(code: string, id: string) {\n // Vite appends query suffixes to ids (e.g. `.pithy?import`, `.pithy?v=…`). Match\n // and derive the filename/scope id from the bare path (before the `?`) so\n // `.pithy` modules are always transformed and the scope id is stable across\n // reloads. `#` is left intact — module ids carry queries, not URL fragments, and\n // `#` can be a legitimate path character.\n if (!id.split('?', 1)[0].endsWith('.pithy')) return;\n return compilePithyModule(code, id);\n },\n\n /**\n * Handles hot module replacement (HMR) for `.pithy` files.\n *\n * Invalidates the module graph so Vite reloads updated components automatically.\n *\n * @param ctx - Vite HMR context\n */\n handleHotUpdate(ctx: HmrContext): void {\n if (ctx.file.endsWith('.pithy')) {\n for (const mod of ctx.modules) {\n ctx.server.moduleGraph.invalidateModule(mod);\n }\n }\n },\n };\n}\n\n/**\n * Compiles a `.pithy` file to the JavaScript module the rest of the chain sees.\n *\n * @param code - The `.pithy` source.\n * @param id - Its module id; a Vite query suffix is ignored.\n * @param tsconfigRaw - esbuild tsconfig JSON, in place of the file lookup.\n * @returns The JavaScript module and its source map.\n */\nexport async function compilePithyModule(\n code: string,\n id: string,\n tsconfigRaw?: string\n): Promise<{\n code: string;\n map: Awaited<ReturnType<typeof transformWithEsbuild>>['map'];\n}> {\n const filePath = id.split('?', 1)[0];\n const parsed = parsePithyFile(code);\n const filename = path.basename(filePath);\n const scopeId = generateScopeId(filePath);\n // The <script lang=\"ts\"> body may hold TS-only syntax and Vite's esbuild pass\n // does not run on `.pithy` ids, so transpile the generated module here.\n const tsCode = compileToModule(parsed, { filename, scopeId });\n const { code: jsCode, map } = await transformWithEsbuild(tsCode, id, {\n loader: 'ts',\n ...(tsconfigRaw === undefined ? {} : { tsconfigRaw }),\n });\n return { code: jsCode, map };\n}\n\n/**\n * Generates a short, per-file-unique scope ID from a file path.\n *\n * Uses a djb2 hash of the WHOLE path rather than a base64 prefix: sibling files\n * share a long common path prefix, so slicing the first base64 chars produced\n * identical IDs for every component — making their scoped styles collide. The hash\n * spreads across the full path, and base36 keeps the id lowercase (the runtime\n * lowercases the `data-scope-<id>` attribute name, so the CSS must match).\n *\n * Example: \"src/components/Button.pithy\" → \"pithy-1a2b3c\"\n *\n * @param filepath - Full file path\n * @returns A short scope ID like `pithy-abc123`\n */\nfunction generateScopeId(filepath: string): string {\n let hash = 5381;\n for (let i = 0; i < filepath.length; i++) {\n hash = (((hash << 5) + hash) ^ filepath.charCodeAt(i)) >>> 0;\n }\n return 'pithy-' + hash.toString(36);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pithyjs/vite-plugin",
3
- "version": "0.2.0-beta.23",
3
+ "version": "0.2.0-beta.25",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/PithyJS/pithyjs.git",
@@ -34,9 +34,9 @@
34
34
  "@babel/types": "^7.27.3",
35
35
  "fast-glob": "^3.3.0",
36
36
  "parse5": "^7.3.0",
37
- "@pithyjs/compiler": "0.1.0-beta.9",
37
+ "@pithyjs/compiler": "0.1.0-beta.11",
38
38
  "@pithyjs/design-system": "0.1.0-beta.12",
39
- "@pithyjs/router": "0.2.0-beta.21",
39
+ "@pithyjs/router": "0.2.0-beta.23",
40
40
  "@pithyjs/shared": "0.1.0-beta.4"
41
41
  },
42
42
  "devDependencies": {