Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] simplify internal prerender API #5659

Merged
merged 12 commits into from
Jul 21, 2022
15 changes: 11 additions & 4 deletions packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { pathToFileURL, URL } from 'url';
import { mkdirp } from '../../utils/filesystem.js';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { installPolyfills } from '../../node/polyfills.js';
import { is_root_relative, normalize_path, resolve } from '../../utils/url.js';
import { queue } from './queue.js';
Expand Down Expand Up @@ -52,12 +52,12 @@ const REDIRECT = 3;
/**
* @param {{
* config: import('types').ValidatedKitConfig;
* entries: string[];
* files: Set<string>;
* client_out_dir: string;
* manifest_path: string;
* log: Logger;
* }} opts
*/
export async function prerender({ config, entries, files, log }) {
export async function prerender({ config, client_out_dir, manifest_path, log }) {
/** @type {import('types').Prerendered} */
const prerendered = {
pages: new Map(),
Expand Down Expand Up @@ -160,6 +160,7 @@ export async function prerender({ config, entries, files, log }) {
return file;
}

const files = new Set(walk(client_out_dir).map(posixify));
const seen = new Set();
const written = new Set();

Expand Down Expand Up @@ -321,6 +322,12 @@ export async function prerender({ config, entries, files, log }) {
if (config.prerender.enabled) {
for (const entry of config.prerender.entries) {
if (entry === '*') {
/** @type {import('types').ManifestData} */
const { routes } = (await import(pathToFileURL(manifest_path).href)).manifest._;
const entries = routes
.map((route) => (route.type === 'page' ? route.path : ''))
.filter(Boolean);

for (const entry of entries) {
enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these?
}
Expand Down
24 changes: 4 additions & 20 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ function kit() {
server
};

const manifest_path = `${paths.output_dir}/server/manifest.js`;
fs.writeFileSync(
`${paths.output_dir}/server/manifest.js`,
manifest_path,
`export const manifest = ${generate_manifest({
build_data,
relative_path: '.',
Expand All @@ -314,27 +315,10 @@ function kit() {
process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';
log.info('Prerendering');

const static_files = manifest_data.assets.map((asset) => posixify(asset.file));

const files = new Set([
...static_files,
...chunks.map((chunk) => chunk.fileName),
...assets.map((chunk) => chunk.fileName)
]);

// TODO is this right?
static_files.forEach((file) => {
if (file.endsWith('/index.html')) {
files.add(file.slice(0, -11));
}
});

prerendered = await prerender({
config: svelte_config.kit,
entries: manifest_data.routes
.map((route) => (route.type === 'page' ? route.path : ''))
.filter(Boolean),
files,
client_out_dir: vite_config.build.outDir,
manifest_path,
log
});

Expand Down