Skip to content

fix: correct URL for entry points #7490

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

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-waves-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix the URL that belongs to `entryPoints` in the hook `astro:build:ssr`. The paths were created with the wrong output directory.
18 changes: 18 additions & 0 deletions packages/astro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@
},
});
```
- [#7220](https://github.com/withastro/astro/pull/7220) [`459b5bd05`](https://github.com/withastro/astro/commit/459b5bd05f562238f7250520efe3cf0fa156bb45) Thanks [@ematipico](https://github.com/ematipico)! - Shipped a new SSR build configuration mode: `split`.

The Astro hook `astro:build:ssr` now receives a new option in their payload, called `entryPoints`.

`entryPoints` is defined as a `Map<RouteData, URL>`, where `RouteData` represents the information of a Astro route and `URL` is the path to the physical file emitted at the end of the build.

```ts
export function integration(): AstroIntegration {
return {
name: "my-integration",
hooks: {
"astro:build:ssr": ({ entryPoints }) => {
// do something with `entryPoints`
}
}
}
}
```

### Patch Changes

Expand Down
15 changes: 0 additions & 15 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,6 @@ export interface CLIFlags {
experimentalRedirects?: boolean;
}

export interface BuildConfig {
/**
* @deprecated Use config.build.client instead.
*/
client: URL;
/**
* @deprecated Use config.build.server instead.
*/
server: URL;
/**
* @deprecated Use config.build.serverEntry instead.
*/
serverEntry: string;
}

Comment on lines -114 to -128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this refactor!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it was long overdue

/**
* Astro global available in all contexts in .astro files
*
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ export function chunkIsPage(
export async function generatePages(opts: StaticBuildOptions, internals: BuildInternals) {
const timer = performance.now();
const ssr = isServerLikeOutput(opts.settings.config);
const serverEntry = opts.buildConfig.serverEntry;
const outFolder = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.settings.config.outDir);
const outFolder = ssr
? opts.settings.config.build.server
: getOutDirWithinCwd(opts.settings.config.outDir);

if (ssr && !hasPrerenderedPages(internals)) return;

Expand Down Expand Up @@ -180,7 +181,6 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn

await runHookBuildGenerated({
config: opts.settings.config,
buildConfig: opts.buildConfig,
logging: opts.logging,
});

Expand Down
15 changes: 1 addition & 14 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { AstroTelemetry } from '@astrojs/telemetry';
import type {
AstroConfig,
AstroSettings,
BuildConfig,
ManifestData,
RuntimeMode,
} from '../../@types/astro';
import type { AstroConfig, AstroSettings, ManifestData, RuntimeMode } from '../../@types/astro';

import fs from 'fs';
import * as colors from 'kleur/colors';
Expand Down Expand Up @@ -123,11 +117,6 @@ class AstroBuilder {

/** Run the build logic. build() is marked private because usage should go through ".run()" */
private async build({ viteConfig }: { viteConfig: vite.InlineConfig }) {
const buildConfig: BuildConfig = {
client: this.settings.config.build.client,
server: this.settings.config.build.server,
serverEntry: this.settings.config.build.serverEntry,
};
await runHookBuildStart({ config: this.settings.config, logging: this.logging });
this.validateConfig();

Expand Down Expand Up @@ -168,7 +157,6 @@ class AstroBuilder {
routeCache: this.routeCache,
teardownCompiler: this.teardownCompiler,
viteConfig,
buildConfig,
};

const { internals } = await viteBuild(opts);
Expand All @@ -188,7 +176,6 @@ class AstroBuilder {
// You're done! Time to clean up.
await runHookBuildDone({
config: this.settings.config,
buildConfig,
pages: pageNames,
routes: Object.values(allPages).map((pd) => pd.route),
logging: this.logging,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ function storeEntryPoint(
const componentPath = getPathFromVirtualModulePageName(RESOLVED_SPLIT_MODULE_ID, moduleKey);
for (const [page, pageData] of Object.entries(options.allPages)) {
if (componentPath == page) {
const publicPath = fileURLToPath(options.settings.config.outDir);
const publicPath = fileURLToPath(options.settings.config.build.server);
internals.entryPoints.set(pageData.route, pathToFileURL(join(publicPath, fileName)));
}
}
Expand Down
22 changes: 13 additions & 9 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as eslexer from 'es-module-lexer';
import glob from 'fast-glob';
import fs from 'fs';
import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
import { extname } from 'node:path';
import { extname, join } from 'node:path';
import path from 'path';
import { fileURLToPath } from 'url';
import * as vite from 'vite';
Expand Down Expand Up @@ -146,7 +146,7 @@ async function ssrBuild(
) {
const { allPages, settings, viteConfig } = opts;
const ssr = isServerLikeOutput(settings.config);
const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir);
const out = ssr ? settings.config.build.server : getOutDirWithinCwd(settings.config.outDir);
const routes = Object.values(allPages).map((pd) => pd.route);
const { lastVitePlugins, vitePlugins } = container.runBeforeHook('ssr', input);

Expand Down Expand Up @@ -227,7 +227,7 @@ async function clientBuild(
const { settings, viteConfig } = opts;
const timer = performance.now();
const ssr = isServerLikeOutput(settings.config);
const out = ssr ? opts.buildConfig.client : getOutDirWithinCwd(settings.config.outDir);
const out = ssr ? settings.config.build.client : getOutDirWithinCwd(settings.config.outDir);

// Nothing to do if there is no client-side JS.
if (!input.size) {
Expand Down Expand Up @@ -289,12 +289,12 @@ async function runPostBuildHooks(
) {
const mutations = await container.runPostHook(ssrReturn, clientReturn);
const config = container.options.settings.config;
const buildConfig = container.options.settings.config.build;
const build = container.options.settings.config.build;
for (const [fileName, mutation] of mutations) {
const root = isServerLikeOutput(config)
? mutation.build === 'server'
? buildConfig.server
: buildConfig.client
? build.server
: build.client
: config.outDir;
const fileURL = new URL(fileName, root);
await fs.promises.mkdir(new URL('./', fileURL), { recursive: true });
Expand All @@ -313,7 +313,9 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter
allStaticFiles.add(internals.pageToBundleMap.get(pageData.moduleSpecifier));
}
const ssr = isServerLikeOutput(opts.settings.config);
const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.settings.config.outDir);
const out = ssr
? opts.settings.config.build.server
: getOutDirWithinCwd(opts.settings.config.outDir);
// The SSR output is all .mjs files, the client output is not.
const files = await glob('**/*.mjs', {
cwd: fileURLToPath(out),
Expand Down Expand Up @@ -394,8 +396,10 @@ async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles = false
async function ssrMoveAssets(opts: StaticBuildOptions) {
info(opts.logging, 'build', 'Rearranging server assets...');
const serverRoot =
opts.settings.config.output === 'static' ? opts.buildConfig.client : opts.buildConfig.server;
const clientRoot = opts.buildConfig.client;
opts.settings.config.output === 'static'
? opts.settings.config.build.client
: opts.settings.config.build.server;
const clientRoot = opts.settings.config.build.client;
const assets = opts.settings.config.build.assets;
const serverAssets = new URL(`./${assets}/`, appendForwardSlash(serverRoot.toString()));
const clientAssets = new URL(`./${assets}/`, appendForwardSlash(clientRoot.toString()));
Expand Down
2 changes: 0 additions & 2 deletions packages/astro/src/core/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { default as vite, InlineConfig } from 'vite';
import type {
AstroConfig,
AstroSettings,
BuildConfig,
ComponentInstance,
ManifestData,
MiddlewareHandler,
Expand Down Expand Up @@ -36,7 +35,6 @@ export type AllPagesData = Record<ComponentPath, PageBuildData>;
export interface StaticBuildOptions {
allPages: AllPagesData;
settings: AstroSettings;
buildConfig: BuildConfig;
logging: LogOptions;
manifest: ManifestData;
mode: RuntimeMode;
Expand Down
9 changes: 2 additions & 7 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
AstroConfig,
AstroRenderer,
AstroSettings,
BuildConfig,
ContentEntryType,
DataEntryType,
HookParameters,
Expand Down Expand Up @@ -323,14 +322,12 @@ export async function runHookBuildSsr({

export async function runHookBuildGenerated({
config,
buildConfig,
logging,
}: {
config: AstroConfig;
buildConfig: BuildConfig;
logging: LogOptions;
}) {
const dir = isServerLikeOutput(config) ? buildConfig.client : config.outDir;
const dir = isServerLikeOutput(config) ? config.build.client : config.outDir;

for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:generated']) {
Expand All @@ -345,18 +342,16 @@ export async function runHookBuildGenerated({

export async function runHookBuildDone({
config,
buildConfig,
pages,
routes,
logging,
}: {
config: AstroConfig;
buildConfig: BuildConfig;
pages: string[];
routes: RouteData[];
logging: LogOptions;
}) {
const dir = isServerLikeOutput(config) ? buildConfig.client : config.outDir;
const dir = isServerLikeOutput(config) ? config.build.client : config.outDir;
await fs.promises.mkdir(dir, { recursive: true });

for (const integration of config.integrations) {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/ssr-split-manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('astro:ssr-manifest, split', () => {
it('should give access to entry points that exists on file system', async () => {
// number of the pages inside src/
expect(entryPoints.size).to.equal(4);
for (const fileUrl in entryPoints.values()) {
for (const fileUrl of entryPoints.values()) {
let filePath = fileURLToPath(fileUrl);
expect(existsSync(filePath)).to.be.true;
}
Expand Down