UNPKG

@nx/angular-rsbuild

Version:

Rsbuild Plugin for building Angular.

77 lines (76 loc) 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteOutputDir = deleteOutputDir; exports.getOutputHashFormat = getOutputHashFormat; const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); /** * Delete an output directory, but error out if it's the root of the project because * that would remove the entire project and could be dangerous. */ async function deleteOutputDir(root, outputPath, preserveDirs) { const resolvedOutputPath = (0, node_path_1.resolve)(root, outputPath); if (resolvedOutputPath === root) { throw new Error('Output path MUST not be project root directory!'); } const directoriesToEmpty = preserveDirs ? new Set(preserveDirs.map((directory) => (0, node_path_1.join)(resolvedOutputPath, directory))) : undefined; // Avoid removing the actual directory to avoid errors in cases where the output // directory is mounted or symlinked. Instead, the contents are removed. let entries; try { entries = await (0, promises_1.readdir)(resolvedOutputPath); } catch (error) { if (error instanceof Error && 'code' in error && error.code === 'ENOENT') { return; } throw error; } for (const entry of entries) { const fullEntry = (0, node_path_1.join)(resolvedOutputPath, entry); // Leave requested directories. This allows symlinks to continue to function. if (directoriesToEmpty?.has(fullEntry)) { // compute its path relative to `root`, then recurse as in deleteOutputDir resolve is called on root const rel = (0, node_path_1.relative)(root, fullEntry); await deleteOutputDir(root, rel, preserveDirs); continue; } await (0, promises_1.rm)(fullEntry, { force: true, recursive: true, maxRetries: 3 }); } } function getOutputHashFormat(outputHashing = 'none', length = 8) { const hashTemplate = `.[contenthash:${length}]`; switch (outputHashing) { case 'media': return { chunk: '', extract: '', file: hashTemplate, script: '', }; case 'bundles': return { chunk: hashTemplate, extract: hashTemplate, file: '', script: hashTemplate, }; case 'all': return { chunk: hashTemplate, extract: hashTemplate, file: hashTemplate, script: hashTemplate, }; case 'none': default: return { chunk: '', extract: '', file: '', script: '', }; } }