summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/yellow-donuts-destroy.md5
-rw-r--r--packages/astro/src/core/build/static-build.ts35
2 files changed, 34 insertions, 6 deletions
diff --git a/.changeset/yellow-donuts-destroy.md b/.changeset/yellow-donuts-destroy.md
new file mode 100644
index 000000000..762309e0b
--- /dev/null
+++ b/.changeset/yellow-donuts-destroy.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+remove all the ssr generated folders in static build if only empty
diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts
index 40237659f..8acad8fc5 100644
--- a/packages/astro/src/core/build/static-build.ts
+++ b/packages/astro/src/core/build/static-build.ts
@@ -1,5 +1,6 @@
import glob from 'fast-glob';
import fs from 'fs';
+import path from 'path';
import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
import { fileURLToPath } from 'url';
import * as vite from 'vite';
@@ -246,12 +247,34 @@ async function cleanSsrOutput(opts: StaticBuildOptions) {
const files = await glob('**/*.mjs', {
cwd: fileURLToPath(out),
});
- await Promise.all(
- files.map(async (filename) => {
- const url = new URL(filename, out);
- await fs.promises.rm(url);
- })
- );
+ if (files.length) {
+ // Remove all the SSR generated .mjs files
+ await Promise.all(
+ files.map(async (filename) => {
+ const url = new URL(filename, out);
+ await fs.promises.rm(url);
+ })
+ );
+ // Map directories heads from the .mjs files
+ const directories: Set<string> = new Set();
+ files.forEach((i) => {
+ const splitFilePath = i.split(path.sep);
+ // If the path is more than just a .mjs filename itself
+ if (splitFilePath.length > 1) {
+ directories.add(splitFilePath[0]);
+ }
+ });
+ // Attempt to remove only those folders which are empty
+ await Promise.all(
+ Array.from(directories).map(async (filename) => {
+ const url = new URL(filename, out);
+ const folder = await fs.promises.readdir(url);
+ if (!folder.length) {
+ await fs.promises.rmdir(url, { recursive: true });
+ }
+ })
+ );
+ }
// Clean out directly if the outDir is outside of root
if (out.toString() !== opts.settings.config.outDir.toString()) {
// Copy assets before cleaning directory if outside root