From f6e24d1a9755c1b1313cbc75efaaf1ac4d80f663 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 9 Dec 2021 08:44:26 -0800 Subject: [PATCH] Almost done --- .../{cool-app => fast-build}/astro.config.mjs | 0 .../{cool-app => fast-build}/package.json | 6 +- .../src/components/Greeting.vue | 0 .../src/images/penguin.jpg | Bin .../src/images/random.jpg | Bin .../src/pages/index.astro | 0 .../src/styles/global.css | 0 packages/astro/package.json | 2 +- packages/astro/src/@types/astro.ts | 1 + packages/astro/src/core/build/index.ts | 234 +++--------------- packages/astro/src/core/build/internal.ts | 45 ++++ packages/astro/src/core/build/page-data.ts | 122 +++++++++ .../astro/src/core/build/scan-based-build.ts | 69 ++++++ packages/astro/src/core/build/static-build.ts | 193 +++++++++++++++ packages/astro/src/runtime/server/index.ts | 3 +- .../astro/src/vite-plugin-astro/compile.ts | 2 + .../astro/src/vite-plugin-build-css/index.ts | 33 ++- .../astro/src/vite-plugin-build-html/index.ts | 39 +-- .../astro/src/vite-plugin-new-build/index.ts | 16 -- 19 files changed, 501 insertions(+), 264 deletions(-) rename examples/{cool-app => fast-build}/astro.config.mjs (100%) rename examples/{cool-app => fast-build}/package.json (61%) rename examples/{cool-app => fast-build}/src/components/Greeting.vue (100%) rename examples/{cool-app => fast-build}/src/images/penguin.jpg (100%) rename examples/{cool-app => fast-build}/src/images/random.jpg (100%) rename examples/{cool-app => fast-build}/src/pages/index.astro (100%) rename examples/{cool-app => fast-build}/src/styles/global.css (100%) create mode 100644 packages/astro/src/core/build/internal.ts create mode 100644 packages/astro/src/core/build/page-data.ts create mode 100644 packages/astro/src/core/build/scan-based-build.ts create mode 100644 packages/astro/src/core/build/static-build.ts delete mode 100644 packages/astro/src/vite-plugin-new-build/index.ts diff --git a/examples/cool-app/astro.config.mjs b/examples/fast-build/astro.config.mjs similarity index 100% rename from examples/cool-app/astro.config.mjs rename to examples/fast-build/astro.config.mjs diff --git a/examples/cool-app/package.json b/examples/fast-build/package.json similarity index 61% rename from examples/cool-app/package.json rename to examples/fast-build/package.json index 88bb609e0..4abd5fc13 100644 --- a/examples/cool-app/package.json +++ b/examples/fast-build/package.json @@ -1,11 +1,11 @@ { - "name": "@example/cool-app", + "name": "@example/fast-build", "version": "0.0.1", "private": true, "scripts": { - "dev": "astro dev", + "dev": "astro dev --experimental-static-build", "start": "astro dev", - "build": "astro build", + "build": "astro build --experimental-static-build", "preview": "astro preview" }, "devDependencies": { diff --git a/examples/cool-app/src/components/Greeting.vue b/examples/fast-build/src/components/Greeting.vue similarity index 100% rename from examples/cool-app/src/components/Greeting.vue rename to examples/fast-build/src/components/Greeting.vue diff --git a/examples/cool-app/src/images/penguin.jpg b/examples/fast-build/src/images/penguin.jpg similarity index 100% rename from examples/cool-app/src/images/penguin.jpg rename to examples/fast-build/src/images/penguin.jpg diff --git a/examples/cool-app/src/images/random.jpg b/examples/fast-build/src/images/random.jpg similarity index 100% rename from examples/cool-app/src/images/random.jpg rename to examples/fast-build/src/images/random.jpg diff --git a/examples/cool-app/src/pages/index.astro b/examples/fast-build/src/pages/index.astro similarity index 100% rename from examples/cool-app/src/pages/index.astro rename to examples/fast-build/src/pages/index.astro diff --git a/examples/cool-app/src/styles/global.css b/examples/fast-build/src/styles/global.css similarity index 100% rename from examples/cool-app/src/styles/global.css rename to examples/fast-build/src/styles/global.css diff --git a/packages/astro/package.json b/packages/astro/package.json index 4617c0a6b..1335cb8c3 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -56,7 +56,7 @@ "test": "mocha --parallel --timeout 15000" }, "dependencies": { - "@astrojs/compiler": "^0.5.4", + "@astrojs/compiler": "^0.6.0", "@astrojs/language-server": "^0.8.2", "@astrojs/markdown-remark": "^0.5.0", "@astrojs/prism": "0.3.0", diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 70b00b24f..cdb2771d5 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -363,6 +363,7 @@ export interface SSRElement { export interface SSRMetadata { renderers: Renderer[]; pathname: string; + experimentalStaticBuild: boolean; } export interface SSRResult { diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 054eee451..85da003fd 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -1,23 +1,18 @@ -import type { AstroConfig, ComponentInstance, GetStaticPathsResult, ManifestData, RouteCache, RouteData, RSSResult } from '../../@types/astro'; +import type { AstroConfig, ManifestData, RouteCache } from '../../@types/astro'; import type { LogOptions } from '../logger'; -import type { AllPagesData, PageBuildData } from './types'; -import type { RenderedChunk } from 'rollup'; +import type { PageBuildData } from './types'; -import { rollupPluginAstroBuildHTML } from '../../vite-plugin-build-html/index.js'; -import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js'; -import { vitePluginNewBuild } from '../../vite-plugin-new-build/index.js'; import fs from 'fs'; import * as colors from 'kleur/colors'; import { performance } from 'perf_hooks'; import vite, { ViteDevServer } from '../vite.js'; -import { fileURLToPath } from 'url'; import { createVite, ViteConfigWithSSR } from '../create-vite.js'; import { debug, defaultLogOptions, info, levels, timerMessage, warn } from '../logger.js'; -import { preload as ssrPreload, renderComponent, getParamsAndProps } from '../ssr/index.js'; -import { generatePaginateFunction } from '../ssr/paginate.js'; -import { createRouteManifest, validateGetStaticPathsModule, validateGetStaticPathsResult } from '../ssr/routing.js'; -import { generateRssFunction } from '../ssr/rss.js'; +import { createRouteManifest } from '../ssr/routing.js'; import { generateSitemap } from '../ssr/sitemap.js'; +import { collectPagesData } from './page-data.js'; +import { build as scanBasedBuild } from './scan-based-build.js'; +import { staticBuild } from './static-build.js'; export interface BuildOptions { mode?: string; @@ -77,155 +72,45 @@ class AstroBuilder { debug(logging, 'build', timerMessage('Vite started', timer.viteStart)); timer.loadStart = performance.now(); - const assets: Record = {}; - const allPages: AllPagesData = {}; - // Collect all routes ahead-of-time, before we start the build. - // NOTE: This enforces that `getStaticPaths()` is only called once per route, - // and is then cached across all future SSR builds. In the past, we've had trouble - // with parallelized builds without guaranteeing that this is called first. - await Promise.all( - this.manifest.routes.map(async (route) => { - // static route: - if (route.pathname) { - allPages[route.component] = { - route, - paths: [route.pathname], - preload: await ssrPreload({ - astroConfig: this.config, - filePath: new URL(`./${route.component}`, this.config.projectRoot), - logging, - mode: 'production', - origin, - pathname: route.pathname, - route, - routeCache: this.routeCache, - viteServer, - }) - .then((routes) => { - const html = `${route.pathname}`.replace(/\/?$/, '/index.html'); - debug(logging, 'build', `├── ${colors.bold(colors.green('✔'))} ${route.component} → ${colors.yellow(html)}`); - return routes; - }) - .catch((err) => { - debug(logging, 'build', `├── ${colors.bold(colors.red('✘'))} ${route.component}`); - throw err; - }), - }; - return; - } - // dynamic route: - const result = await this.getStaticPathsForRoute(route) - .then((routes) => { - const label = routes.paths.length === 1 ? 'page' : 'pages'; - debug(logging, 'build', `├── ${colors.bold(colors.green('✔'))} ${route.component} → ${colors.magenta(`[${routes.paths.length} ${label}]`)}`); - return routes; - }) - .catch((err) => { - debug(logging, 'build', `├── ${colors.bold(colors.red('✗'))} ${route.component}`); - throw err; - }); - if (result.rss?.xml) { - const rssFile = new URL(result.rss.url.replace(/^\/?/, './'), this.config.dist); - if (assets[fileURLToPath(rssFile)]) { - throw new Error(`[getStaticPaths] RSS feed ${result.rss.url} already exists.\nUse \`rss(data, {url: '...'})\` to choose a unique, custom URL. (${route.component})`); - } - assets[fileURLToPath(rssFile)] = result.rss.xml; - } - allPages[route.component] = { - route, - paths: result.paths, - preload: await ssrPreload({ - astroConfig: this.config, - filePath: new URL(`./${route.component}`, this.config.projectRoot), - logging, - mode: 'production', - origin, - pathname: result.paths[0], - route, - routeCache: this.routeCache, - viteServer, - }), - }; - }) - ); + const { assets, allPages } = await collectPagesData({ + astroConfig: this.config, + logging: this.logging, + manifest: this.manifest, + origin, + routeCache: this.routeCache, + viteServer: this.viteServer + }); debug(logging, 'build', timerMessage('All pages loaded', timer.loadStart)); - // Pure CSS chunks are chunks that only contain CSS. - // This is all of them, and chunkToReferenceIdMap maps them to a hash id used to find the final file. - const pureCSSChunks = new Set(); - const chunkToReferenceIdMap = new Map(); - - // This is a mapping of pathname to the string source of all collected - // inline