Skip to content

Commit

Permalink
fix: remove assets from dist/server/ (fix #2034)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Dec 27, 2024
1 parent d93dfe1 commit c1cd2d9
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions vike/node/plugin/plugins/buildConfig/fixServerAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { fixServerAssets_assertCssTarget }
export { fixServerAssets_assertCssTarget_populate }

import fs from 'fs/promises'
import fs_sync from 'fs'
import path from 'path'
import { existsSync } from 'fs'
import { ViteManifest, ViteManifestEntry } from '../../../shared/ViteManifest.js'
Expand Down Expand Up @@ -56,17 +57,18 @@ async function copyAssets(filesToCopy: string[], config: ResolvedConfig) {
const concurrencyLimit = pLimit(10)
await Promise.all(
filesToCopy.map((file) =>
concurrencyLimit(() =>
// TODO: move instead of copying
fs.cp(path.posix.join(outDirServer, file), path.posix.join(outDirClient, file), {
recursive: true
})
)
concurrencyLimit(async () => {
const source = path.posix.join(outDirServer, file)
const target = path.posix.join(outDirClient, file)
await fs.mkdir(path.posix.dirname(target), { recursive: true })
await fs.rename(source, target)
})
)
)
/* We cannot do that because, with some edge case Rollup settings (outputing JavaScript chunks and static assets to the same directoy), this removes JavaScript chunks, see https://github.com/vikejs/vike/issues/1154#issuecomment-1975762404
await fs.rm(assetsDirServer, { recursive: true })
*/
removeEmptyDirectories(assetsDirServer)
}

type Resource = { src: string; hash: string }
Expand Down Expand Up @@ -240,3 +242,27 @@ async function fixServerAssets_assertCssTarget(config: ResolvedConfig) {
function resolveCssTarget(target: TargetConfig) {
return target.css ?? target.global
}

/**
* Recursively remove all empty directories in a given directory.
*/
function removeEmptyDirectories(dirPath: string): void {
// Read the directory contents
const files = fs_sync.readdirSync(dirPath)

// Iterate through the files and subdirectories
for (const file of files) {
const fullPath = path.join(dirPath, file)

// Check if it's a directory
if (fs_sync.statSync(fullPath).isDirectory()) {
// Recursively clean up the subdirectory
removeEmptyDirectories(fullPath)
}
}

// Re-check the directory; remove it if it's now empty
if (fs_sync.readdirSync(dirPath).length === 0) {
fs_sync.rmdirSync(dirPath)
}
}

0 comments on commit c1cd2d9

Please sign in to comment.