|
1 | 1 | import glob from 'fast-glob';
|
2 | 2 | import fs from 'fs';
|
| 3 | +import path from 'path'; |
3 | 4 | import { bgGreen, bgMagenta, black, dim } from 'kleur/colors';
|
4 | 5 | import { fileURLToPath } from 'url';
|
5 | 6 | import * as vite from 'vite';
|
@@ -246,12 +247,34 @@ async function cleanSsrOutput(opts: StaticBuildOptions) {
|
246 | 247 | const files = await glob('**/*.mjs', {
|
247 | 248 | cwd: fileURLToPath(out),
|
248 | 249 | });
|
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 | + } |
255 | 278 | // Clean out directly if the outDir is outside of root
|
256 | 279 | if (out.toString() !== opts.settings.config.outDir.toString()) {
|
257 | 280 | // Copy assets before cleaning directory if outside root
|
|
0 commit comments