@tinyvolt/pie 0.1.0 → 0.3.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/vite.ts CHANGED
@@ -1,13 +1,15 @@
1
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';
2
3
  import path from 'node:path';
3
4
  import type { Plugin, PluginOption } from 'vite';
4
5
  import { nitro } from 'nitro/vite';
6
+ import type { NitroModule } from 'nitro/types';
5
7
  import { solidStart } from '@solidjs/start/config';
6
8
  import { APP_DIR } from './src/lib/appDir';
7
9
  import { checkPageRoutes, markdownPages } from './src/lib/markdown/vitePlugin';
8
10
  import { checkFunctions } from './src/lib/nodes/functions/vitePlugin';
9
11
  import { checkLayouts } from './src/lib/layouts/vitePlugin';
10
- import { siteConfig } from './src/lib/site/vitePlugin';
12
+ import { parseSite, siteConfig } from './src/lib/site/vitePlugin';
11
13
 
12
14
  /**
13
15
  * `~/...` in a site's own files (`functions/`, `nodes/`, `layouts/`) means the app's `src/`.
@@ -50,12 +52,45 @@ function dedupeSolid(): Plugin {
50
52
  };
51
53
  }
52
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
+
53
88
  export function pie(): PluginOption[] {
54
89
  return [
55
90
  siteAlias(), optimizeDeps(), dedupeSolid(),
56
91
  markdownPages(), checkPageRoutes(), checkFunctions(), checkLayouts(), siteConfig(),
57
92
  // SolidStart resolves `appRoot` against the working directory.
58
93
  solidStart({ appRoot: path.relative(process.cwd(), APP_DIR) }),
59
- nitro({ prerender: { crawlLinks: true, routes: ['/', '/pages'] } }),
94
+ nitro({ prerender: { crawlLinks: true, routes: ['/', '/pages'] }, modules: [seoFiles()] }),
60
95
  ];
61
96
  }
@@ -1,10 +0,0 @@
1
- import { PageTitle } from "~/components/PageTitle";
2
-
3
- export default function About() {
4
- return (
5
- <main>
6
- <PageTitle>About</PageTitle>
7
- <h1>About</h1>
8
- </main>
9
- );
10
- }