index.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import path from 'node:path';
  2. import { platforms } from './platforms.js';
  3. /** @type {import('./index.js').default} */
  4. export default function (options) {
  5. return {
  6. name: '@sveltejs/adapter-static',
  7. /** @param {import('./internal.js').Builder2_0_0} builder */
  8. async adapt(builder) {
  9. if (!options?.fallback && builder.config.kit.router?.type !== 'hash') {
  10. const dynamic_routes = builder.routes.filter((route) => route.prerender !== true);
  11. if (dynamic_routes.length > 0 && options?.strict !== false) {
  12. const prefix = path.relative('.', builder.config.kit.files.routes);
  13. const has_param_routes = builder.routes.some((route) => route.id.includes('['));
  14. const config_option =
  15. has_param_routes || JSON.stringify(builder.config.kit.prerender.entries) !== '["*"]'
  16. ? ` - adjust the \`prerender.entries\` config option ${
  17. has_param_routes
  18. ? '(routes with parameters are not part of entry points by default)'
  19. : ''
  20. } — see https://svelte.dev/docs/kit/configuration#prerender for more info.`
  21. : '';
  22. builder.log.error(
  23. `@sveltejs/adapter-static: all routes must be fully prerenderable, but found the following routes that are dynamic:
  24. ${dynamic_routes.map((route) => ` - ${path.posix.join(prefix, route.id)}`).join('\n')}
  25. You have the following options:
  26. - set the \`fallback\` option — see https://svelte.dev/docs/kit/single-page-apps#usage for more info.
  27. - add \`export const prerender = true\` to your root \`+layout.js/.ts\` or \`+layout.server.js/.ts\` file. This will try to prerender all pages.
  28. - add \`export const prerender = true\` to any \`+server.js/ts\` files that are not fetched by page \`load\` functions.
  29. ${config_option}
  30. - 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.
  31. 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.
  32. See https://svelte.dev/docs/kit/page-options#prerender for more details`
  33. );
  34. throw new Error('Encountered dynamic routes');
  35. }
  36. }
  37. const platform = platforms.find((platform) => platform.test());
  38. if (platform) {
  39. if (options) {
  40. builder.log.warn(
  41. `Detected ${platform.name}. Please remove adapter-static options to enable zero-config mode`
  42. );
  43. } else {
  44. builder.log.info(`Detected ${platform.name}, using zero-config mode`);
  45. }
  46. }
  47. const {
  48. // @ts-ignore
  49. pages = 'build',
  50. assets = pages,
  51. fallback,
  52. precompress
  53. } = options ?? platform?.defaults ?? /** @type {import('./index.js').AdapterOptions} */ ({});
  54. builder.rimraf(assets);
  55. builder.rimraf(pages);
  56. builder.generateEnvModule();
  57. builder.writeClient(assets);
  58. builder.writePrerendered(pages);
  59. if (fallback) {
  60. await builder.generateFallback(path.join(pages, fallback));
  61. }
  62. if (precompress) {
  63. builder.log.minor('Compressing assets and pages');
  64. if (pages === assets) {
  65. await builder.compress(assets);
  66. } else {
  67. await Promise.all([builder.compress(assets), builder.compress(pages)]);
  68. }
  69. }
  70. if (pages === assets) {
  71. builder.log(`Wrote site to "${pages}"`);
  72. } else {
  73. builder.log(`Wrote pages to "${pages}" and assets to "${assets}"`);
  74. }
  75. if (!options) platform?.done(builder);
  76. }
  77. };
  78. }