@tinyvolt/pie 0.0.0-stage → 0.2.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.
Files changed (73) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +68 -2
  4. package/functions/midpoint.ts +29 -0
  5. package/functions/square.ts +20 -0
  6. package/init/index.js +87 -0
  7. package/init/site-agents.md +257 -0
  8. package/init/site-gitignore +28 -0
  9. package/init/site-tsconfig.json +20 -0
  10. package/init/site-vite.config.ts +6 -0
  11. package/layouts/header.tsx +16 -0
  12. package/layouts/index.tsx +10 -0
  13. package/nodes/square.tsx +36 -0
  14. package/package.json +63 -4
  15. package/pages/about.md +5 -0
  16. package/pages/example.md +7 -0
  17. package/pages/python-editor.md +29 -0
  18. package/public/favicon.ico +0 -0
  19. package/public/logo.svg +5 -0
  20. package/site.yml +1 -0
  21. package/src/app.css +387 -0
  22. package/src/app.tsx +30 -0
  23. package/src/authorStyles.ts +3 -0
  24. package/src/components/Header.tsx +16 -0
  25. package/src/components/PageMeta.tsx +40 -0
  26. package/src/components/PageTitle.tsx +10 -0
  27. package/src/components/SplitPane.tsx +170 -0
  28. package/src/components/icons.tsx +94 -0
  29. package/src/entry-client.tsx +4 -0
  30. package/src/entry-server.tsx +21 -0
  31. package/src/global.d.ts +7 -0
  32. package/src/lib/appDir.ts +4 -0
  33. package/src/lib/autodiff/index.ts +49 -0
  34. package/src/lib/layouts/layout.ts +20 -0
  35. package/src/lib/layouts/vitePlugin.ts +39 -0
  36. package/src/lib/markdown/components/CanvasPage.tsx +25 -0
  37. package/src/lib/markdown/components/MarkdownBody.tsx +42 -0
  38. package/src/lib/markdown/components/PythonEditor.tsx +254 -0
  39. package/src/lib/markdown/frontmatter.ts +83 -0
  40. package/src/lib/markdown/pages.ts +23 -0
  41. package/src/lib/markdown/render.ts +62 -0
  42. package/src/lib/markdown/vitePlugin.ts +94 -0
  43. package/src/lib/nodes/components/CenteredGrid.tsx +59 -0
  44. package/src/lib/nodes/components/NodeCanvas.tsx +58 -0
  45. package/src/lib/nodes/execute.ts +166 -0
  46. package/src/lib/nodes/functions/Node2NodeFunc.ts +307 -0
  47. package/src/lib/nodes/functions/array.ts +36 -0
  48. package/src/lib/nodes/functions/autodiff.ts +64 -0
  49. package/src/lib/nodes/functions/basic.ts +298 -0
  50. package/src/lib/nodes/functions/registry.ts +46 -0
  51. package/src/lib/nodes/functions/sideEffects.ts +193 -0
  52. package/src/lib/nodes/functions/vitePlugin.ts +40 -0
  53. package/src/lib/nodes/hooks/useCanvasResize.ts +38 -0
  54. package/src/lib/nodes/jax.ts +191 -0
  55. package/src/lib/nodes/render/renderLogic.tsx +306 -0
  56. package/src/lib/nodes/state/NameGenerator.ts +31 -0
  57. package/src/lib/nodes/state/Node.ts +211 -0
  58. package/src/lib/nodes/state/NodeStore.ts +110 -0
  59. package/src/lib/nodes/tensors/broadcast.ts +101 -0
  60. package/src/lib/nodes/tensors/tensor.ts +42 -0
  61. package/src/lib/python/lift.ts +105 -0
  62. package/src/lib/python/py/__pycache__/pie.cpython-314.pyc +0 -0
  63. package/src/lib/python/py/pie.py +231 -0
  64. package/src/lib/python/py/pie_mpl.py +15 -0
  65. package/src/lib/python/py/show_figures.py +5 -0
  66. package/src/lib/python/runner.ts +202 -0
  67. package/src/lib/python/worker.ts +109 -0
  68. package/src/lib/site/vitePlugin.ts +58 -0
  69. package/src/routes/[...slug].tsx +39 -0
  70. package/src/routes/index.tsx +24 -0
  71. package/src/routes/pages/index.tsx +20 -0
  72. package/styles/theme.css +27 -0
  73. package/vite.ts +96 -0
@@ -0,0 +1,20 @@
1
+ import { For } from 'solid-js';
2
+ import { PageMeta } from '~/components/PageMeta';
3
+ import { slugs } from '~/lib/markdown/pages';
4
+
5
+ export default function Pages() {
6
+ return (
7
+ <main>
8
+ <PageMeta title="Pages" />
9
+ <ul>
10
+ <For each={slugs}>
11
+ {slug => (
12
+ <li>
13
+ <a href={`/${slug}`}>{slug}</a>
14
+ </li>
15
+ )}
16
+ </For>
17
+ </ul>
18
+ </main>
19
+ );
20
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * Site theme. Change these values to restyle the site.
3
+ * Every `.css` file in `/styles` loads after the built-in `src/app.css`,
4
+ * so rules added here (or in another file in this folder) override the defaults.
5
+ */
6
+ :root {
7
+ /* Colors */
8
+ --color-text: #1a1a1a;
9
+ --color-bg: #fff;
10
+ --color-muted: #555; /* nav links, blockquotes */
11
+ --color-subtle: #666; /* buttons, editor status and notes */
12
+ --color-hover: #000; /* links and buttons on hover */
13
+ --color-border: #ddd;
14
+ --color-border-hover: #999;
15
+ --color-code-bg: #f4f4f4;
16
+ --color-error: #c00;
17
+
18
+ /* Fonts */
19
+ --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; /* header, editor */
20
+ --font-body: Georgia, "Times New Roman", serif; /* page text */
21
+ --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; /* code */
22
+ --font-size-body: 1.125rem;
23
+ --line-height: 1.6;
24
+
25
+ /* Layout */
26
+ --content-width: 40rem; /* page text column, about 70 characters */
27
+ }
package/vite.ts ADDED
@@ -0,0 +1,96 @@
1
+ // `pie()`: every Vite plugin a pie-ssg site needs. A site's `vite.config.ts` is `plugins: [pie()]`.
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import type { Plugin, PluginOption } from 'vite';
5
+ import { nitro } from 'nitro/vite';
6
+ import type { NitroModule } from 'nitro/types';
7
+ import { solidStart } from '@solidjs/start/config';
8
+ import { APP_DIR } from './src/lib/appDir';
9
+ import { checkPageRoutes, markdownPages } from './src/lib/markdown/vitePlugin';
10
+ import { checkFunctions } from './src/lib/nodes/functions/vitePlugin';
11
+ import { checkLayouts } from './src/lib/layouts/vitePlugin';
12
+ import { parseSite, siteConfig } from './src/lib/site/vitePlugin';
13
+
14
+ /**
15
+ * `~/...` in a site's own files (`functions/`, `nodes/`, `layouts/`) means the app's `src/`.
16
+ * SolidStart's `~` alias skips files outside the app's package, which a site's files are.
17
+ */
18
+ function siteAlias(): Plugin {
19
+ return {
20
+ name: 'pie-site-alias',
21
+ enforce: 'pre',
22
+ async resolveId(id, importer, options) {
23
+ if (id !== '~' && !id.startsWith('~/')) return null;
24
+ const target = path.join(APP_DIR, id.slice(1));
25
+ return (await this.resolve(target, importer, { ...options, skipSelf: true })) ?? target;
26
+ },
27
+ };
28
+ }
29
+
30
+ /** Pre-bundles SolidStart's `@jridgewell/trace-mapping`, reached through `@tinyvolt/pie` in a site. */
31
+ function optimizeDeps(): Plugin {
32
+ return {
33
+ name: 'pie-optimize-deps',
34
+ config(config) {
35
+ const inRepo = path.resolve(config.root ?? process.cwd()) === path.dirname(APP_DIR);
36
+ const dep = '@solidjs/start > @jridgewell/trace-mapping';
37
+ return { optimizeDeps: { include: [inRepo ? dep : `@tinyvolt/pie > ${dep}`] } };
38
+ },
39
+ };
40
+ }
41
+
42
+ /**
43
+ * Bundles one copy of `solid-js`: the site's. `@solidjs/start` can install its own, newer copy,
44
+ * and two copies in the browser break `<Suspense>` ("Cannot read properties of null (reading 'push')").
45
+ */
46
+ function dedupeSolid(): Plugin {
47
+ return {
48
+ name: 'pie-dedupe-solid',
49
+ config() {
50
+ return { resolve: { dedupe: ['solid-js'] } };
51
+ },
52
+ };
53
+ }
54
+
55
+ /**
56
+ * After prerendering, writes `sitemap.xml` (every prerendered page) and `robots.txt` (pointing to it).
57
+ * Both need `site.yml`'s `url`; without it they are skipped with a warning. A site's own `public/` file wins.
58
+ */
59
+ function seoFiles(): NitroModule {
60
+ return {
61
+ name: 'pie-seo-files',
62
+ setup(nitro) {
63
+ nitro.hooks.hook('prerender:done', ({ prerenderedRoutes }) => {
64
+ const { url } = parseSite('site.yml', fs.readFileSync(path.join(nitro.options.rootDir, 'site.yml'), 'utf8'));
65
+ if (!url) {
66
+ nitro.logger.warn('site.yml has no `url`: skipped sitemap.xml, robots.txt, canonical links and og:url. Add e.g. `url: https://example.com` before deploying.');
67
+ return;
68
+ }
69
+ const pages = prerenderedRoutes.filter(route => route.fileName?.endsWith('.html')).map(route => route.route).sort();
70
+ const escape = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
71
+ const write = (name: string, contents: string) => {
72
+ const file = path.join(nitro.options.output.publicDir, name);
73
+ if (!fs.existsSync(file)) fs.writeFileSync(file, contents);
74
+ };
75
+ write('sitemap.xml', [
76
+ '<?xml version="1.0" encoding="UTF-8"?>',
77
+ '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
78
+ ...pages.map(page => ` <url><loc>${escape(url + page)}</loc></url>`),
79
+ '</urlset>',
80
+ '',
81
+ ].join('\n'));
82
+ write('robots.txt', `User-agent: *\nAllow: /\n\nSitemap: ${url}/sitemap.xml\n`);
83
+ });
84
+ },
85
+ };
86
+ }
87
+
88
+ export function pie(): PluginOption[] {
89
+ return [
90
+ siteAlias(), optimizeDeps(), dedupeSolid(),
91
+ markdownPages(), checkPageRoutes(), checkFunctions(), checkLayouts(), siteConfig(),
92
+ // SolidStart resolves `appRoot` against the working directory.
93
+ solidStart({ appRoot: path.relative(process.cwd(), APP_DIR) }),
94
+ nitro({ prerender: { crawlLinks: true, routes: ['/', '/pages'] }, modules: [seoFiles()] }),
95
+ ];
96
+ }