fix: "chunks" directory appears in build output, if custom modules are imported in Astro files (#4868)
This commit is contained in:
parent
32f489b136
commit
b99f9902b7
2 changed files with 34 additions and 6 deletions
5
.changeset/yellow-donuts-destroy.md
Normal file
5
.changeset/yellow-donuts-destroy.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
remove all the ssr generated folders in static build if only empty
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue