| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import path from 'node:path';
- import { platforms } from './platforms.js';
- /** @type {import('./index.js').default} */
- export default function (options) {
- return {
- name: '@sveltejs/adapter-static',
- /** @param {import('./internal.js').Builder2_0_0} builder */
- async adapt(builder) {
- if (!options?.fallback && builder.config.kit.router?.type !== 'hash') {
- const dynamic_routes = builder.routes.filter((route) => route.prerender !== true);
- if (dynamic_routes.length > 0 && options?.strict !== false) {
- const prefix = path.relative('.', builder.config.kit.files.routes);
- const has_param_routes = builder.routes.some((route) => route.id.includes('['));
- const config_option =
- has_param_routes || JSON.stringify(builder.config.kit.prerender.entries) !== '["*"]'
- ? ` - adjust the \`prerender.entries\` config option ${
- has_param_routes
- ? '(routes with parameters are not part of entry points by default)'
- : ''
- } — see https://svelte.dev/docs/kit/configuration#prerender for more info.`
- : '';
- builder.log.error(
- `@sveltejs/adapter-static: all routes must be fully prerenderable, but found the following routes that are dynamic:
- ${dynamic_routes.map((route) => ` - ${path.posix.join(prefix, route.id)}`).join('\n')}
- You have the following options:
- - set the \`fallback\` option — see https://svelte.dev/docs/kit/single-page-apps#usage for more info.
- - add \`export const prerender = true\` to your root \`+layout.js/.ts\` or \`+layout.server.js/.ts\` file. This will try to prerender all pages.
- - add \`export const prerender = true\` to any \`+server.js/ts\` files that are not fetched by page \`load\` functions.
- ${config_option}
- - pass \`strict: false\` to \`adapter-static\` to ignore this error. Only do this if you are sure you don't need the routes in question in your final app, as they will be unavailable. See https://github.com/sveltejs/kit/tree/main/packages/adapter-static#strict for more info.
- If this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need a server for dynamic rendering, and can run on just a static file server.
- See https://svelte.dev/docs/kit/page-options#prerender for more details`
- );
- throw new Error('Encountered dynamic routes');
- }
- }
- const platform = platforms.find((platform) => platform.test());
- if (platform) {
- if (options) {
- builder.log.warn(
- `Detected ${platform.name}. Please remove adapter-static options to enable zero-config mode`
- );
- } else {
- builder.log.info(`Detected ${platform.name}, using zero-config mode`);
- }
- }
- const {
- // @ts-ignore
- pages = 'build',
- assets = pages,
- fallback,
- precompress
- } = options ?? platform?.defaults ?? /** @type {import('./index.js').AdapterOptions} */ ({});
- builder.rimraf(assets);
- builder.rimraf(pages);
- builder.generateEnvModule();
- builder.writeClient(assets);
- builder.writePrerendered(pages);
- if (fallback) {
- await builder.generateFallback(path.join(pages, fallback));
- }
- if (precompress) {
- builder.log.minor('Compressing assets and pages');
- if (pages === assets) {
- await builder.compress(assets);
- } else {
- await Promise.all([builder.compress(assets), builder.compress(pages)]);
- }
- }
- if (pages === assets) {
- builder.log(`Wrote site to "${pages}"`);
- } else {
- builder.log(`Wrote pages to "${pages}" and assets to "${assets}"`);
- }
- if (!options) platform?.done(builder);
- }
- };
- }
|