UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

60 lines (59 loc) 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pathJoin = pathJoin; exports.pathIsRelative = pathIsRelative; exports.toPosixPath = toPosixPath; exports.assertPosixPath = assertPosixPath; // Utilities for handling file paths. // - Shims `import * from "node:path"` for server runtime. // - Robust shim reference: https://github.com/unjs/pathe const assert_js_1 = require("./assert.js"); const assertIsNotBrowser_js_1 = require("./assertIsNotBrowser.js"); // While this path shim also works on the client-side, let's try to not use it on the client-side in order to minimize KBs sent to the browser. (0, assertIsNotBrowser_js_1.assertIsNotBrowser)(); /**********************/ /****** SHIMS *********/ /**********************/ function pathJoin(path1, path2) { (0, assert_js_1.assert)(!path1.includes('\\')); (0, assert_js_1.assert)(!path2.includes('\\')); let joined = [...path1.split('/'), ...path2.split('/')].filter(Boolean).join('/'); if (path1.startsWith('/')) joined = '/' + joined; return joined; } /* https://github.com/brillout/telefunc/blob/0fd44322acbd07857ae29361ba7c998607f17dd5/telefunc/utils/path-shim.ts#L17-L21 const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/ function pathIsAbsolute(filePath: string) { return IS_ABSOLUTE_RE.test(filePath) } //*/ /**********************/ /****** UTILS *********/ /**********************/ function toPosixPath(path) { const pathPosix = path.split('\\').join('/'); assertPosixPath(pathPosix); return pathPosix; } function assertPosixPath(path) { const errMsg = (msg) => `Not a posix path: ${msg}`; (0, assert_js_1.assert)(path !== null, errMsg('null')); (0, assert_js_1.assert)(typeof path === 'string', errMsg(`typeof path === ${JSON.stringify(typeof path)}`)); (0, assert_js_1.assert)(path !== '', errMsg('(empty string)')); (0, assert_js_1.assert)(path); (0, assert_js_1.assert)(!path.includes('\\'), errMsg(path)); } // See also `import { isImportPathRelative } from './isImportPath.js'` function pathIsRelative(path) { assertPosixPath(path); if (path.startsWith('./') || path.startsWith('../')) { return true; } else { /* Not true if `path` starts with a hidden directory (i.e. a directory with a name that starts with `.`) assert(!path.startsWith('.')) */ return false; } }