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 glob from 'fast-glob';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
|
import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import * as vite from 'vite';
|
import * as vite from 'vite';
|
||||||
|
@ -246,12 +247,34 @@ async function cleanSsrOutput(opts: StaticBuildOptions) {
|
||||||
const files = await glob('**/*.mjs', {
|
const files = await glob('**/*.mjs', {
|
||||||
cwd: fileURLToPath(out),
|
cwd: fileURLToPath(out),
|
||||||
});
|
});
|
||||||
await Promise.all(
|
if (files.length) {
|
||||||
files.map(async (filename) => {
|
// Remove all the SSR generated .mjs files
|
||||||
const url = new URL(filename, out);
|
await Promise.all(
|
||||||
await fs.promises.rm(url);
|
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
|
// Clean out directly if the outDir is outside of root
|
||||||
if (out.toString() !== opts.settings.config.outDir.toString()) {
|
if (out.toString() !== opts.settings.config.outDir.toString()) {
|
||||||
// Copy assets before cleaning directory if outside root
|
// Copy assets before cleaning directory if outside root
|
||||||
|
|
Loading…
Reference in a new issue