fix: "chunks" directory appears in build output, if custom modules are imported in Astro files (#4868)

This commit is contained in:
Rishi Raj Jain 2022-09-26 15:31:44 +05:30 committed by GitHub
parent 32f489b136
commit b99f9902b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
remove all the ssr generated folders in static build if only empty

View file

@ -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