astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
65 lines (64 loc) • 2.18 kB
JavaScript
export function getTimeStat(timeStart, timeEnd) {
const buildTime = timeEnd - timeStart;
if (buildTime < 1e3) {
return `${Math.round(buildTime)}ms`;
} else if (buildTime < 6e4) {
return `${(buildTime / 1e3).toFixed(2)}s`;
}
const mins = Math.floor(buildTime / 6e4);
const secs = Math.round(buildTime % 6e4 / 1e3);
return `${mins}m ${secs}s`;
}
/**
* Given the Astro configuration, it tells if a slash should be appended or not
*/
export function shouldAppendForwardSlash(trailingSlash, buildFormat) {
switch (trailingSlash) {
case "always": return true;
case "never": return false;
case "ignore": {
switch (buildFormat) {
case "directory": return true;
case "preserve":
case "file": return false;
}
}
}
}
/**
* Matches any character that is NOT alphanumeric, underscore, dot, hyphen, or forward slash.
* Rollup's built-in `sanitizeFileName` misses characters like `!` and `~` that can leak
* from Vite module IDs into chunk names (e.g. `page.!{005}.js`).
*/
const UNSAFE_CHUNK_CHAR_RE = /[^\w.\-/]/g;
/**
* Replaces characters in a chunk name that are not safe for filesystem paths or URLs.
* Characters like `!` and `~` can leak from Vite module IDs into Rollup chunk names
* and break deploys on platforms like Netlify.
*/
export function cleanChunkName(name) {
return encodeName(name.replace(UNSAFE_CHUNK_CHAR_RE, "_"));
}
function encodeName(name) {
// Detect if the chunk name has as % sign that is not encoded.
// This is borrowed from Node core: https://github.com/nodejs/node/blob/3838b579e44bf0c2db43171c3ce0da51eb6b05d5/lib/internal/url.js#L1382-L1391
// We do this because you cannot import a module with this character in it.
for (let i = 0; i < name.length; i++) {
if (name[i] === "%") {
const third = name.codePointAt(i + 2) | 32;
if (name[i + 1] !== "2" || third !== 102) {
return `${name.replace(/%/g, "_percent_")}`;
}
}
}
return name;
}
export function viteBuildReturnToRolldownOutputs(viteBuildReturn) {
const result = [];
if (Array.isArray(viteBuildReturn)) {
result.push(...viteBuildReturn);
} else if ("output" in viteBuildReturn) {
result.push(viteBuildReturn);
}
return result;
}