polen 0.9.0-next.2 → 0.9.0-next.4

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.
Files changed (110) hide show
  1. package/build/api/server/report-error.d.ts +2 -0
  2. package/build/api/server/report-error.d.ts.map +1 -0
  3. package/build/api/server/report-error.js +47 -0
  4. package/build/api/server/report-error.js.map +1 -0
  5. package/build/api/vite/data/navbar.d.ts +9 -0
  6. package/build/api/vite/data/navbar.d.ts.map +1 -0
  7. package/build/api/vite/data/navbar.js +6 -0
  8. package/build/api/vite/data/navbar.js.map +1 -0
  9. package/build/api/vite/plugins/core.d.ts.map +1 -1
  10. package/build/api/vite/plugins/core.js +36 -104
  11. package/build/api/vite/plugins/core.js.map +1 -1
  12. package/build/api/vite/plugins/pages.d.ts +25 -0
  13. package/build/api/vite/plugins/pages.d.ts.map +1 -0
  14. package/build/api/vite/plugins/pages.js +273 -0
  15. package/build/api/vite/plugins/pages.js.map +1 -0
  16. package/build/api/vite/plugins/serve.d.ts.map +1 -1
  17. package/build/api/vite/plugins/serve.js +5 -26
  18. package/build/api/vite/plugins/serve.js.map +1 -1
  19. package/build/cli/commands/dev.js +9 -1
  20. package/build/cli/commands/dev.js.map +1 -1
  21. package/build/lib/debug/environment-variable.d.ts +1 -0
  22. package/build/lib/debug/environment-variable.d.ts.map +1 -1
  23. package/build/lib/debug/environment-variable.js +30 -15
  24. package/build/lib/debug/environment-variable.js.map +1 -1
  25. package/build/lib/extensible-data/extensible-data.d.ts +17 -0
  26. package/build/lib/extensible-data/extensible-data.d.ts.map +1 -0
  27. package/build/lib/extensible-data/extensible-data.js +24 -0
  28. package/build/lib/extensible-data/extensible-data.js.map +1 -0
  29. package/build/lib/extensible-data/index.d.ts +2 -0
  30. package/build/lib/extensible-data/index.d.ts.map +1 -0
  31. package/build/lib/extensible-data/index.js +2 -0
  32. package/build/lib/extensible-data/index.js.map +1 -0
  33. package/build/lib/kit-temp.d.ts +2 -0
  34. package/build/lib/kit-temp.d.ts.map +1 -1
  35. package/build/lib/kit-temp.js +10 -1
  36. package/build/lib/kit-temp.js.map +1 -1
  37. package/build/lib/react-router-loader/react-router-loader.d.ts.map +1 -1
  38. package/build/lib/react-router-loader/react-router-loader.js +0 -1
  39. package/build/lib/react-router-loader/react-router-loader.js.map +1 -1
  40. package/build/lib/vite-plugin-json/index.d.ts +2 -0
  41. package/build/lib/vite-plugin-json/index.d.ts.map +1 -0
  42. package/build/lib/vite-plugin-json/index.js +2 -0
  43. package/build/lib/vite-plugin-json/index.js.map +1 -0
  44. package/build/lib/vite-plugin-json/vite-plugin-json.d.ts +64 -0
  45. package/build/lib/vite-plugin-json/vite-plugin-json.d.ts.map +1 -0
  46. package/build/lib/vite-plugin-json/vite-plugin-json.js +59 -0
  47. package/build/lib/vite-plugin-json/vite-plugin-json.js.map +1 -0
  48. package/build/lib/vite-plugin-reactive-data/index.d.ts +2 -0
  49. package/build/lib/vite-plugin-reactive-data/index.d.ts.map +1 -0
  50. package/build/lib/vite-plugin-reactive-data/index.js +2 -0
  51. package/build/lib/vite-plugin-reactive-data/index.js.map +1 -0
  52. package/build/lib/vite-plugin-reactive-data/vite-plugin-reactive-data.d.ts +39 -0
  53. package/build/lib/vite-plugin-reactive-data/vite-plugin-reactive-data.d.ts.map +1 -0
  54. package/build/lib/vite-plugin-reactive-data/vite-plugin-reactive-data.js +92 -0
  55. package/build/lib/vite-plugin-reactive-data/vite-plugin-reactive-data.js.map +1 -0
  56. package/build/lib/vite-plugins/build-logger.d.ts.map +1 -1
  57. package/build/lib/vite-plugins/build-logger.js +9 -8
  58. package/build/lib/vite-plugins/build-logger.js.map +1 -1
  59. package/build/project-data.d.ts +0 -11
  60. package/build/project-data.d.ts.map +1 -1
  61. package/build/template/components/Link.d.ts.map +1 -1
  62. package/build/template/components/Link.jsx +4 -3
  63. package/build/template/components/Link.jsx.map +1 -1
  64. package/build/template/routes/changelog.jsx +1 -1
  65. package/build/template/routes/changelog.jsx.map +1 -1
  66. package/build/template/routes/reference.jsx +1 -1
  67. package/build/template/routes/reference.jsx.map +1 -1
  68. package/build/template/routes/root.d.ts.map +1 -1
  69. package/build/template/routes/root.jsx +5 -3
  70. package/build/template/routes/root.jsx.map +1 -1
  71. package/build/template/server/app.js +1 -1
  72. package/build/template/server/app.js.map +1 -1
  73. package/build/template/server/render-page.d.ts.map +1 -1
  74. package/build/template/server/render-page.jsx +4 -1
  75. package/build/template/server/render-page.jsx.map +1 -1
  76. package/build/template/server/ssg/generate.js +1 -1
  77. package/build/template/server/ssg/generate.js.map +1 -1
  78. package/build/template/server/ssg/get-route-paths.js +1 -1
  79. package/build/template/server/ssg/get-route-paths.js.map +1 -1
  80. package/package.json +3 -1
  81. package/src/api/server/report-error.ts +61 -0
  82. package/src/api/vite/data/navbar.ts +15 -0
  83. package/src/api/vite/plugins/core.ts +38 -116
  84. package/src/api/vite/plugins/pages.ts +332 -0
  85. package/src/api/vite/plugins/serve.ts +5 -26
  86. package/src/cli/commands/dev.ts +9 -1
  87. package/src/lib/debug/environment-variable.ts +31 -14
  88. package/src/lib/extensible-data/extensible-data.ts +38 -0
  89. package/src/lib/extensible-data/index.ts +1 -0
  90. package/src/lib/kit-temp.ts +12 -1
  91. package/src/lib/react-router-loader/react-router-loader.ts +0 -1
  92. package/src/lib/vite-plugin-json/index.ts +1 -0
  93. package/src/lib/vite-plugin-json/vite-plugin-json.ts +128 -0
  94. package/src/lib/vite-plugin-reactive-data/index.ts +1 -0
  95. package/src/lib/vite-plugin-reactive-data/vite-plugin-reactive-data.ts +131 -0
  96. package/src/lib/vite-plugins/build-logger.ts +10 -8
  97. package/src/project-data.ts +0 -13
  98. package/src/template/components/Link.tsx +6 -3
  99. package/src/template/routes/changelog.tsx +1 -1
  100. package/src/template/routes/reference.tsx +1 -1
  101. package/src/template/routes/root.tsx +5 -3
  102. package/src/template/server/app.ts +1 -1
  103. package/src/template/server/render-page.tsx +4 -1
  104. package/src/template/server/ssg/generate.ts +1 -1
  105. package/src/template/server/ssg/get-route-paths.ts +1 -1
  106. package/build/api/vite/plugins/pages-tree.d.ts +0 -16
  107. package/build/api/vite/plugins/pages-tree.d.ts.map +0 -1
  108. package/build/api/vite/plugins/pages-tree.js +0 -153
  109. package/build/api/vite/plugins/pages-tree.js.map +0 -1
  110. package/src/api/vite/plugins/pages-tree.ts +0 -187
@@ -1,153 +0,0 @@
1
- import { polenVirtual } from '#api/vite/vi';
2
- import { reportDiagnostics } from '#lib/file-router/diagnostic-reporter';
3
- import { FileRouter } from '#lib/file-router/index';
4
- import { ViteVirtual } from '#lib/vite-virtual/index';
5
- import { debug } from '#singletons/debug';
6
- import mdx from '@mdx-js/rollup';
7
- import { Path, Str } from '@wollybeard/kit';
8
- import remarkGfm from 'remark-gfm';
9
- const _debug = debug.sub(`vite-plugin-pages`);
10
- export const viProjectPages = polenVirtual([`project`, `pages.jsx`], { allowPluginProcessing: true });
11
- /**
12
- * Pages plugin with tree support
13
- */
14
- export const createPagesPlugin = ({ config, onPagesChange, onTreeChange }) => {
15
- // State management
16
- let pagesCache = null;
17
- let treeCache = null;
18
- // Helper functions
19
- const scanPages = async () => {
20
- if (!pagesCache) {
21
- _debug(`Scanning pages - cache is null, loading fresh data`);
22
- pagesCache = await FileRouter.scan({
23
- dir: config.paths.project.absolute.pages,
24
- glob: `**/*.{md,mdx}`,
25
- });
26
- _debug(`Found ${String(pagesCache.routes.length)} pages`);
27
- }
28
- else {
29
- _debug(`Using cached pages`);
30
- }
31
- return pagesCache;
32
- };
33
- const scanTree = async () => {
34
- if (!treeCache) {
35
- _debug(`Scanning tree - cache is null, loading fresh data`);
36
- const result = await FileRouter.scanTree({
37
- dir: config.paths.project.absolute.pages,
38
- glob: `**/*.{md,mdx}`,
39
- });
40
- treeCache = result.routeTree;
41
- _debug(`Built route tree`);
42
- }
43
- else {
44
- _debug(`Using cached tree`);
45
- }
46
- return treeCache;
47
- };
48
- const clearCache = () => {
49
- _debug(`Clearing pages and tree cache`);
50
- pagesCache = null;
51
- treeCache = null;
52
- };
53
- const isPageFile = (file) => {
54
- return (file.endsWith(`.md`) || file.endsWith(`.mdx`))
55
- && file.includes(config.paths.project.absolute.pages);
56
- };
57
- const generatePagesModule = (pagesScanResult) => {
58
- const $ = {
59
- pages: `pages`,
60
- };
61
- const s = Str.Builder();
62
- s `export const ${$.pages} = []`;
63
- // Generate imports and route objects
64
- for (const route of pagesScanResult.routes) {
65
- const filePathExp = Path.format(route.file.path.absolute);
66
- const pathExp = FileRouter.routeToPathExpression(route);
67
- const ident = Str.Case.camel(`page ` + Str.titlizeSlug(pathExp));
68
- s `
69
- import ${ident} from '${filePathExp}'
70
-
71
- ${$.pages}.push({
72
- path: '${pathExp}',
73
- Component: ${ident}
74
- })
75
- `;
76
- }
77
- return s.render();
78
- };
79
- return [
80
- // Plugin 1: MDX Processing
81
- {
82
- enforce: `pre`,
83
- ...mdx({
84
- jsxImportSource: `polen/react`,
85
- remarkPlugins: [remarkGfm],
86
- }),
87
- },
88
- // Plugin 2: Pages Management
89
- {
90
- name: `polen:pages-tree`,
91
- // Dev server configuration
92
- configureServer(server) {
93
- // Add pages directory to watcher
94
- _debug(`configureServer: watch pages directory`, config.paths.project.absolute.pages);
95
- server.watcher.add(config.paths.project.absolute.pages);
96
- },
97
- // Hot update handling
98
- async handleHotUpdate({ file, server }) {
99
- _debug(`handleHotUpdate`, file);
100
- if (!isPageFile(file))
101
- return;
102
- _debug(`Page file changed:`, file);
103
- // Clear cache
104
- clearCache();
105
- // Invalidate virtual module
106
- const mod = server.moduleGraph.getModuleById(viProjectPages.resolved);
107
- if (mod) {
108
- server.moduleGraph.invalidateModule(mod);
109
- _debug(`Invalidated pages virtual module`);
110
- }
111
- // Notify about pages change (for other plugins that depend on pages)
112
- if (onPagesChange) {
113
- const pages = await scanPages();
114
- // Report any diagnostics
115
- reportDiagnostics(pages.diagnostics);
116
- onPagesChange(pages);
117
- }
118
- // Notify about tree change
119
- if (onTreeChange) {
120
- const tree = await scanTree();
121
- onTreeChange(tree);
122
- }
123
- // Trigger full reload
124
- server.ws.send({ type: `full-reload` });
125
- return [];
126
- },
127
- // Virtual module handling
128
- ...ViteVirtual.IdentifiedLoader.toHooks({
129
- identifier: viProjectPages,
130
- async loader() {
131
- _debug(`Loading viProjectPages virtual module`);
132
- const pagesScanResult = await scanPages();
133
- const tree = await scanTree();
134
- // Report any diagnostics
135
- reportDiagnostics(pagesScanResult.diagnostics);
136
- // Notify about pages (useful for initial load)
137
- onPagesChange?.(pagesScanResult);
138
- onTreeChange?.(tree);
139
- return generatePagesModule(pagesScanResult);
140
- },
141
- }),
142
- },
143
- ];
144
- };
145
- // Helper to get tree
146
- export const getRouteTree = async (config) => {
147
- const result = await FileRouter.scanTree({
148
- dir: config.paths.project.absolute.pages,
149
- glob: `**/*.{md,mdx}`,
150
- });
151
- return result.routeTree;
152
- };
153
- //# sourceMappingURL=pages-tree.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pages-tree.js","sourceRoot":"","sources":["../../../../src/api/vite/plugins/pages-tree.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AACxE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AACzC,OAAO,GAAG,MAAM,gBAAgB,CAAA;AAChC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,SAAS,MAAM,YAAY,CAAA;AAElC,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;AAE7C,MAAM,CAAC,MAAM,cAAc,GAAG,YAAY,CAAC,CAAC,SAAS,EAAE,WAAW,CAAC,EAAE,EAAE,qBAAqB,EAAE,IAAI,EA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AAG,MAAM,QAAQ,EAAE,CAAA;oBAC7B,YAAY,CAAC,IAAI,CAAC,CAAA;gBACpB,CAAC;gBAED,sBAAsB;gBACtB,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAA;gBAEvC,OAAO,EAAE,CAAA;YACX,CAAC;YAED,0BAA0B;YAC1B,GAAG,WAAW,CAAC,gBAAgB,CAAC,OAAO,CAAC;gBACtC,UAAU,EAAE,cAAc;gBAC1B,KAAK,CAAC,MAAM;oBACV,MAAM,CAAC,uCAAuC,CAAC,CAAA;oBAC/C,MAAM,eAAe,GAAG,MAAM,SAAS,EAAE,CAAA;oBACzC,MAAM,IAAI,GAAG,MAAM,QAAQ,EAAE,CAAA;oBAE7B,yBAAyB;oBACzB,iBAAiB,CAAC,eAAe,CAAC,WAAW,CAAC,CAAA;oBAE9C,+CAA+C;oBAC/C,aAAa,EAAE,CAAC,eAAe,CAAC,CAAA;oBAChC,YAAY,EAAE,CAAC,IAAI,CAAC,CAAA;oBAEpB,OAAO,mBAAmB,CAAC,eAAe,CAAC,CAAA;gBAC7C,CAAC;aACF,CAAC;SACH;KACF,CAAA;AACH,CAAC,CAAA;AAED,qBAAqB;AACrB,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAAE,MAAqB,EAAqC,EAAE;IAC7F,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC;QACvC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK;QACxC,IAAI,EAAE,eAAe;KACtB,CAAC,CAAA;IACF,OAAO,MAAM,CAAC,SAAS,CAAA;AACzB,CAAC,CAAA"}
@@ -1,187 +0,0 @@
1
- import type { Config } from '#api/config/index'
2
- import { polenVirtual } from '#api/vite/vi'
3
- import type { Vite } from '#dep/vite/index'
4
- import { reportDiagnostics } from '#lib/file-router/diagnostic-reporter'
5
- import { FileRouter } from '#lib/file-router/index'
6
- import { ViteVirtual } from '#lib/vite-virtual/index'
7
- import { debug } from '#singletons/debug'
8
- import mdx from '@mdx-js/rollup'
9
- import { Path, Str } from '@wollybeard/kit'
10
- import remarkGfm from 'remark-gfm'
11
-
12
- const _debug = debug.sub(`vite-plugin-pages`)
13
-
14
- export const viProjectPages = polenVirtual([`project`, `pages.jsx`], { allowPluginProcessing: true })
15
-
16
- export interface PagesTreePluginOptions {
17
- config: Config.Config
18
- onPagesChange?: (pages: FileRouter.ScanResult) => void
19
- onTreeChange?: (tree: FileRouter.RouteTreeNode) => void
20
- }
21
-
22
- /**
23
- * Pages plugin with tree support
24
- */
25
- export const createPagesPlugin = (
26
- { config, onPagesChange, onTreeChange }: PagesTreePluginOptions,
27
- ): Vite.Plugin[] => {
28
- // State management
29
- let pagesCache: FileRouter.ScanResult | null = null
30
- let treeCache: FileRouter.RouteTreeNode | null = null
31
-
32
- // Helper functions
33
- const scanPages = async () => {
34
- if (!pagesCache) {
35
- _debug(`Scanning pages - cache is null, loading fresh data`)
36
- pagesCache = await FileRouter.scan({
37
- dir: config.paths.project.absolute.pages,
38
- glob: `**/*.{md,mdx}`,
39
- })
40
- _debug(`Found ${String(pagesCache.routes.length)} pages`)
41
- } else {
42
- _debug(`Using cached pages`)
43
- }
44
- return pagesCache
45
- }
46
-
47
- const scanTree = async () => {
48
- if (!treeCache) {
49
- _debug(`Scanning tree - cache is null, loading fresh data`)
50
- const result = await FileRouter.scanTree({
51
- dir: config.paths.project.absolute.pages,
52
- glob: `**/*.{md,mdx}`,
53
- })
54
- treeCache = result.routeTree
55
- _debug(`Built route tree`)
56
- } else {
57
- _debug(`Using cached tree`)
58
- }
59
- return treeCache
60
- }
61
-
62
- const clearCache = () => {
63
- _debug(`Clearing pages and tree cache`)
64
- pagesCache = null
65
- treeCache = null
66
- }
67
-
68
- const isPageFile = (file: string) => {
69
- return (file.endsWith(`.md`) || file.endsWith(`.mdx`))
70
- && file.includes(config.paths.project.absolute.pages)
71
- }
72
-
73
- const generatePagesModule = (pagesScanResult: FileRouter.ScanResult): string => {
74
- const $ = {
75
- pages: `pages`,
76
- }
77
-
78
- const s = Str.Builder()
79
- s`export const ${$.pages} = []`
80
-
81
- // Generate imports and route objects
82
- for (const route of pagesScanResult.routes) {
83
- const filePathExp = Path.format(route.file.path.absolute)
84
- const pathExp = FileRouter.routeToPathExpression(route)
85
- const ident = Str.Case.camel(`page ` + Str.titlizeSlug(pathExp))
86
-
87
- s`
88
- import ${ident} from '${filePathExp}'
89
-
90
- ${$.pages}.push({
91
- path: '${pathExp}',
92
- Component: ${ident}
93
- })
94
- `
95
- }
96
-
97
- return s.render()
98
- }
99
-
100
- return [
101
- // Plugin 1: MDX Processing
102
- {
103
- enforce: `pre` as const,
104
- ...mdx({
105
- jsxImportSource: `polen/react`,
106
- remarkPlugins: [remarkGfm],
107
- }),
108
- },
109
-
110
- // Plugin 2: Pages Management
111
- {
112
- name: `polen:pages-tree`,
113
-
114
- // Dev server configuration
115
- configureServer(server) {
116
- // Add pages directory to watcher
117
- _debug(`configureServer: watch pages directory`, config.paths.project.absolute.pages)
118
- server.watcher.add(config.paths.project.absolute.pages)
119
- },
120
-
121
- // Hot update handling
122
- async handleHotUpdate({ file, server }) {
123
- _debug(`handleHotUpdate`, file)
124
- if (!isPageFile(file)) return
125
-
126
- _debug(`Page file changed:`, file)
127
-
128
- // Clear cache
129
- clearCache()
130
-
131
- // Invalidate virtual module
132
- const mod = server.moduleGraph.getModuleById(viProjectPages.resolved)
133
- if (mod) {
134
- server.moduleGraph.invalidateModule(mod)
135
- _debug(`Invalidated pages virtual module`)
136
- }
137
-
138
- // Notify about pages change (for other plugins that depend on pages)
139
- if (onPagesChange) {
140
- const pages = await scanPages()
141
- // Report any diagnostics
142
- reportDiagnostics(pages.diagnostics)
143
- onPagesChange(pages)
144
- }
145
-
146
- // Notify about tree change
147
- if (onTreeChange) {
148
- const tree = await scanTree()
149
- onTreeChange(tree)
150
- }
151
-
152
- // Trigger full reload
153
- server.ws.send({ type: `full-reload` })
154
-
155
- return []
156
- },
157
-
158
- // Virtual module handling
159
- ...ViteVirtual.IdentifiedLoader.toHooks({
160
- identifier: viProjectPages,
161
- async loader() {
162
- _debug(`Loading viProjectPages virtual module`)
163
- const pagesScanResult = await scanPages()
164
- const tree = await scanTree()
165
-
166
- // Report any diagnostics
167
- reportDiagnostics(pagesScanResult.diagnostics)
168
-
169
- // Notify about pages (useful for initial load)
170
- onPagesChange?.(pagesScanResult)
171
- onTreeChange?.(tree)
172
-
173
- return generatePagesModule(pagesScanResult)
174
- },
175
- }),
176
- },
177
- ]
178
- }
179
-
180
- // Helper to get tree
181
- export const getRouteTree = async (config: Config.Config): Promise<FileRouter.RouteTreeNode> => {
182
- const result = await FileRouter.scanTree({
183
- dir: config.paths.project.absolute.pages,
184
- glob: `**/*.{md,mdx}`,
185
- })
186
- return result.routeTree
187
- }