Skip to content

Commit b99f990

Browse files
author
Rishi Raj Jain
authored
fix: "chunks" directory appears in build output, if custom modules are imported in Astro files (#4868)
1 parent 32f489b commit b99f990

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

.changeset/yellow-donuts-destroy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
remove all the ssr generated folders in static build if only empty

packages/astro/src/core/build/static-build.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import glob from 'fast-glob';
22
import fs from 'fs';
3+
import path from 'path';
34
import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
45
import { fileURLToPath } from 'url';
56
import * as vite from 'vite';
@@ -246,12 +247,34 @@ async function cleanSsrOutput(opts: StaticBuildOptions) {
246247
const files = await glob('**/*.mjs', {
247248
cwd: fileURLToPath(out),
248249
});
249-
await Promise.all(
250-
files.map(async (filename) => {
251-
const url = new URL(filename, out);
252-
await fs.promises.rm(url);
253-
})
254-
);
250+
if (files.length) {
251+
// Remove all the SSR generated .mjs files
252+
await Promise.all(
253+
files.map(async (filename) => {
254+
const url = new URL(filename, out);
255+
await fs.promises.rm(url);
256+
})
257+
);
258+
// Map directories heads from the .mjs files
259+
const directories: Set<string> = new Set();
260+
files.forEach((i) => {
261+
const splitFilePath = i.split(path.sep);
262+
// If the path is more than just a .mjs filename itself
263+
if (splitFilePath.length > 1) {
264+
directories.add(splitFilePath[0]);
265+
}
266+
});
267+
// Attempt to remove only those folders which are empty
268+
await Promise.all(
269+
Array.from(directories).map(async (filename) => {
270+
const url = new URL(filename, out);
271+
const folder = await fs.promises.readdir(url);
272+
if (!folder.length) {
273+
await fs.promises.rmdir(url, { recursive: true });
274+
}
275+
})
276+
);
277+
}
255278
// Clean out directly if the outDir is outside of root
256279
if (out.toString() !== opts.settings.config.outDir.toString()) {
257280
// Copy assets before cleaning directory if outside root

0 commit comments

Comments
 (0)