Skip to content
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

Handle builds with outDir outside of cwd #4736

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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/kind-rats-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Handle builds with outDir outside of current working directory
15 changes: 15 additions & 0 deletions packages/astro/src/core/build/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import npath from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
import type { AstroConfig, RouteType } from '../../@types/astro';
import { appendForwardSlash } from '../../core/path.js';

const STATUS_CODE_PAGES = new Set(['/404', '/500']);
const FALLBACK_OUT_DIR_NAME = './.astro/';

function getOutRoot(astroConfig: AstroConfig): URL {
return new URL('./', astroConfig.outDir);
Expand Down Expand Up @@ -59,3 +61,16 @@ export function getOutFile(
}
}
}

/**
* Ensures the `outDir` is within `process.cwd()`. If not it will fallback to `<cwd>/.astro`.
* This is used for static `ssrBuild` so the output can access node_modules when we import
* the output files. A hardcoded fallback dir is fine as it would be cleaned up after build.
*/
export function getOutDirWithinCwd(outDir: URL): URL {
if (fileURLToPath(outDir).startsWith(process.cwd())) {
return outDir;
} else {
return new URL(FALLBACK_OUT_DIR_NAME, pathToFileURL(process.cwd() + npath.sep));
}
}
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { createLinkStylesheetElementSet, createModuleScriptsSet } from '../rende
import { createRequest } from '../request.js';
import { matchRoute } from '../routing/match.js';
import { getOutputFilename } from '../util.js';
import { getOutFile, getOutFolder } from './common.js';
import { getOutDirWithinCwd, getOutFile, getOutFolder } from './common.js';
import { eachPageData, getPageDataByComponent, sortedCSS } from './internal.js';
import type { PageBuildData, SingleFileBuiltModule, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
Expand Down Expand Up @@ -103,7 +103,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn

const ssr = opts.astroConfig.output === 'server';
const serverEntry = opts.buildConfig.serverEntry;
const outFolder = ssr ? opts.buildConfig.server : opts.astroConfig.outDir;
const outFolder = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.astroConfig.outDir);
const ssrEntryURL = new URL('./' + serverEntry + `?time=${Date.now()}`, outFolder);
const ssrEntry = await import(ssrEntryURL.toString());
const builtPaths = new Set<string>();
Expand Down
13 changes: 10 additions & 3 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { runHookBuildSetup } from '../../integrations/index.js';
import { PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import type { ViteConfigWithSSR } from '../create-vite';
import { info } from '../logger/core.js';
import { getOutDirWithinCwd } from './common.js';
import { generatePages } from './generate.js';
import { trackPageData } from './internal.js';
import type { PageBuildData, StaticBuildOptions } from './types';
Expand Down Expand Up @@ -110,7 +111,7 @@ Learn more: https://docs.astro.build/en/guides/server-side-rendering/
async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) {
const { astroConfig, viteConfig } = opts;
const ssr = astroConfig.output === 'server';
const out = ssr ? opts.buildConfig.server : astroConfig.outDir;
const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(astroConfig.outDir);

const viteBuildConfig: ViteConfigWithSSR = {
...viteConfig,
Expand Down Expand Up @@ -245,13 +246,19 @@ async function clientBuild(
}

async function cleanSsrOutput(opts: StaticBuildOptions) {
const out = getOutDirWithinCwd(opts.astroConfig.outDir);
// Clean out directly if the outDir is outside of root
if (out.toString() !== opts.astroConfig.outDir.toString()) {
await fs.promises.rm(out, { recursive: true });
return;
}
// The SSR output is all .mjs files, the client output is not.
const files = await glob('**/*.mjs', {
cwd: fileURLToPath(opts.astroConfig.outDir),
cwd: fileURLToPath(out),
});
await Promise.all(
files.map(async (filename) => {
const url = new URL(filename, opts.astroConfig.outDir);
const url = new URL(filename, out);
await fs.promises.rm(url);
})
);
Expand Down