Skip to content

Commit

Permalink
fix: "chunks" directory appears in build output, if custom modules ar…
Browse files Browse the repository at this point in the history
…e imported in Astro files (#4868)
  • Loading branch information
rishi-raj-jain committed Sep 26, 2022
1 parent 32f489b commit b99f990
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-donuts-destroy.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

remove all the ssr generated folders in static build if only empty
35 changes: 29 additions & 6 deletions 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';
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b99f990

Please sign in to comment.