Skip to content

Commit

Permalink
BuildPipeline -> BuildEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Jan 31, 2024
1 parent bd1d7a3 commit 4631bbb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 132 deletions.
7 changes: 3 additions & 4 deletions packages/astro/src/assets/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs, { readFileSync } from 'node:fs';
import { basename, join } from 'node:path/posix';
import type PQueue from 'p-queue';
import type { AstroConfig } from '../../@types/astro.js';
import type { BuildPipeline } from '../../core/build/buildPipeline.js';
import type { BuildEnvironment } from '../../core/build/environment.js';
import { getOutDirWithinCwd } from '../../core/build/common.js';
import { getTimeStat } from '../../core/build/util.js';
import { AstroError } from '../../core/errors/errors.js';
Expand Down Expand Up @@ -47,11 +47,10 @@ type AssetEnv = {
type ImageData = { data: Uint8Array; expires: number };

export async function prepareAssetsGenerationEnv(
pipeline: BuildPipeline,
environment: BuildEnvironment,
totalCount: number
): Promise<AssetEnv> {
const config = pipeline.getConfig();
const logger = pipeline.getLogger();
const { config, logger } = environment;
let useCache = true;
const assetsCacheDir = new URL('assets/', config.cacheDir);
const count = { total: totalCount, current: 1 };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { AstroConfig, AstroSettings, SSRLoadedRenderer } from '../../@types/astro.js';
import type { SSRLoadedRenderer } from '../../@types/astro.js';
import { getOutputDirectory, isServerLikeOutput } from '../../prerender/utils.js';
import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import type { SSRManifest } from '../app/types.js';
import type { Logger } from '../logger/core.js';
import { Pipeline } from '../pipeline.js';
import { routeIsFallback, routeIsRedirect } from '../redirects/helpers.js';
import { Environment } from '../render/index.js';
import { createAssetLink } from '../render/ssr-element.js';
Expand All @@ -18,78 +16,39 @@ import type { PageBuildData, StaticBuildOptions } from './types.js';
import { i18nHasFallback } from './util.js';

/**
* This pipeline is responsible to gather the files emitted by the SSR build and generate the pages by executing these files.
* This build environment is responsible to gather the files emitted by the SSR build and generate the pages by executing these files.
*/
export class BuildPipeline extends Pipeline {
#internals: BuildInternals;
#staticBuildOptions: StaticBuildOptions;
#manifest: SSRManifest;

export class BuildEnvironment extends Environment {
constructor(
staticBuildOptions: StaticBuildOptions,
internals: BuildInternals,
manifest: SSRManifest
readonly options: StaticBuildOptions,
readonly internals: BuildInternals,
readonly manifest: SSRManifest,
readonly config = options.settings.config,
readonly settings = options.settings
) {
const ssr = isServerLikeOutput(staticBuildOptions.settings.config);
const resolveCache = new Map<string, string>();
super(
new Environment(
staticBuildOptions.logger,
manifest,
staticBuildOptions.mode,
manifest.renderers,
async function resolve(specifier) {
if (resolveCache.has(specifier)) {
return resolveCache.get(specifier)!;
}
const hashedFilePath = manifest.entryModules[specifier];
if (typeof hashedFilePath !== 'string' || hashedFilePath === '') {
// If no "astro:scripts/before-hydration.js" script exists in the build,
// then we can assume that no before-hydration scripts are needed.
if (specifier === BEFORE_HYDRATION_SCRIPT_ID) {
resolveCache.set(specifier, '');
return '';
}
throw new Error(`Cannot find the built path for ${specifier}`);
}
const assetLink = createAssetLink(hashedFilePath, manifest.base, manifest.assetsPrefix);
resolveCache.set(specifier, assetLink);
return assetLink;
},
ssr,
true,
staticBuildOptions.routeCache
)
);
this.#internals = internals;
this.#staticBuildOptions = staticBuildOptions;
this.#manifest = manifest;
}

getInternals(): Readonly<BuildInternals> {
return this.#internals;
}

getSettings(): Readonly<AstroSettings> {
return this.#staticBuildOptions.settings;
}

getStaticBuildOptions(): Readonly<StaticBuildOptions> {
return this.#staticBuildOptions;
}

getConfig(): AstroConfig {
return this.#staticBuildOptions.settings.config;
}

getManifest(): SSRManifest {
return this.#manifest;
}

getLogger(): Logger {
return this.env.logger;
async function resolve(specifier: string) {
if (resolveCache.has(specifier)) {
return resolveCache.get(specifier)!;
}
const hashedFilePath = manifest.entryModules[specifier];
if (typeof hashedFilePath !== 'string' || hashedFilePath === '') {
// If no "astro:scripts/before-hydration.js" script exists in the build,
// then we can assume that no before-hydration scripts are needed.
if (specifier === BEFORE_HYDRATION_SCRIPT_ID) {
resolveCache.set(specifier, '');
return '';
}
throw new Error(`Cannot find the built path for ${specifier}`);
}
const assetLink = createAssetLink(hashedFilePath, manifest.base, manifest.assetsPrefix);
resolveCache.set(specifier, assetLink);
return assetLink;
}
const serverLike = isServerLikeOutput(config);
const streaming = true;
super(options.logger, manifest, options.mode, manifest.renderers, resolve, serverLike, streaming, options.routeCache)
}

/**
* The SSR build emits two important files:
* - dist/server/manifest.mjs
Expand Down Expand Up @@ -141,7 +100,7 @@ export class BuildPipeline extends Pipeline {
retrieveRoutesToGenerate(): Map<PageBuildData, string> {
const pages = new Map<PageBuildData, string>();

for (const [entrypoint, filePath] of this.#internals.entrySpecifierToBundleMap) {
for (const [entrypoint, filePath] of this.internals.entrySpecifierToBundleMap) {
// virtual pages can be emitted with different prefixes:
// - the classic way are pages emitted with prefix ASTRO_PAGE_RESOLVED_MODULE_ID -> plugin-pages
// - pages emitted using `build.split`, in this case pages are emitted with prefix RESOLVED_SPLIT_MODULE_ID
Expand All @@ -150,7 +109,7 @@ export class BuildPipeline extends Pipeline {
entrypoint.includes(RESOLVED_SPLIT_MODULE_ID)
) {
const [, pageName] = entrypoint.split(':');
const pageData = this.#internals.pagesByComponent.get(
const pageData = this.internals.pagesByComponent.get(
`${pageName.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.')}`
);
if (!pageData) {
Expand All @@ -163,12 +122,12 @@ export class BuildPipeline extends Pipeline {
}
}

for (const [path, pageData] of this.#internals.pagesByComponent.entries()) {
for (const [path, pageData] of this.internals.pagesByComponent.entries()) {
if (routeIsRedirect(pageData.route)) {
pages.set(pageData, path);
} else if (
routeIsFallback(pageData.route) &&
(i18nHasFallback(this.getConfig()) ||
(i18nHasFallback(this.config) ||
(routeIsFallback(pageData.route) && pageData.route.route === '/'))
) {
// The original component is transformed during the first build, so we have to retrieve
Expand All @@ -179,7 +138,7 @@ export class BuildPipeline extends Pipeline {
// Here, we take the component path and transform it in the virtual module name
const moduleSpecifier = getVirtualModulePageNameFromPath(path);
// We retrieve the original JS module
const filePath = this.#internals.entrySpecifierToBundleMap.get(moduleSpecifier);
const filePath = this.internals.entrySpecifierToBundleMap.get(moduleSpecifier);
if (filePath) {
// it exists, added it to pages to render, using the file path that we jus retrieved
pages.set(pageData, filePath);
Expand Down
Loading

0 comments on commit 4631bbb

Please sign in to comment.