UNPKG

@joker.front/cli

Version:

The Next-Generation Front-End Toolchain: Swift, Efficient, and Adaptive.

367 lines (366 loc) 11.5 kB
import fs from "node:fs"; import type { RollupError, TransformResult, SourceMap as RollupSourceMap } from "rollup"; import type { DecodedSourceMap, RawSourceMap } from "@ampproject/remapping"; import { ResolvedConfig } from "../config"; import MagicString from "magic-string"; export declare const isWindows: boolean; /** * 转换host * @param host * @returns host,name */ export declare function transformHostName(host?: string | boolean): Promise<{ host: string; name: string; }>; /** * 通配符主机 */ export declare const WILDCARD_HOSTS: string[]; /** * 环回主机 */ export declare const LOOPBACK_HOSTS: string[]; export declare const NODE_MODULES_RE: RegExp; export declare const PNPM_RE: RegExp; export declare const SPECIAL_QUERT_RE: RegExp; export declare const RAW_RE: RegExp; export declare const URL_RE: RegExp; export declare const SCRIPT_TYPES_RE: RegExp; export declare const OPTIMIZABLE_ENTRY_RE: RegExp; export declare const HTML_TYPES_RE: RegExp; export declare const DEP_VERSION_RE: RegExp; export declare function getFileExtRegex(filter: string[]): RegExp; export declare function getDepVersion(url: string): string | undefined; export declare function extractPnpmPackagePath(str: string): string; /** * 转换可识别规范的地址 * @param strPath * @returns */ export declare function normalizePath(strPath: string): string; export declare function slash(p: string): string; export declare function stripBase(url: string, base: string): string; export declare function ensureVolumeInPath(file: string): string; /** * CLI当前类库的路径 */ export declare const CLI_PACKAGE_DIR: string; export declare const NODE_MODULE_DIR: string; /** * 返回指定内容相同长度的空格替换符 * @param str * @returns */ export declare function blankReplacer(str: string): string; type SrcSetType = { url: string; descriptor: string; }; /** * 转换 srcset中的url * @param srcSet * @param transformFn * @returns */ export declare function transformSrcSetUrlAsync(srcSet: string, transformFn: (srcs: SrcSetType) => string): string; export declare function transformSrcSetUrl(srcSet: string, transformFn: (srcs: SrcSetType) => Promise<string>): Promise<string>; export declare function urlToFileURL(url: string): URL; /** * 向URL中添加query参数 * @param url * @param query * @returns */ export declare function addUrlQuery(url: string, query: string): string; /** * 向Url中添加热更新时间戳 * @param url * @param timer * @returns */ export declare function addUrlTimerQuery(url: string, timer: number): string; /** * 移除url中热更新时间戳 * @param url */ export declare function removeTimestampQuery(url: string): string; /** * 移除url中的import 以及query * @param url * @returns */ export declare function removeImportQuery(url: string): string; /** * 是否是import请求 * @param url * @returns */ export declare function isImportRequest(url: string): boolean; /** * 是否是内部请求 * @param url */ export declare function isInternalRequest(url: string): boolean; /** * 是否是ts请求(不考虑 cmts|tsx) * @param url */ export declare function isTSRequest(url: string): boolean; /** * ts可能输出的文件类型(不考虑cmjs|jsx) * @param url * @returns */ export declare function isPossibleTsOutput(url: string): boolean; /** * 根据输出,推导出可能的src源文件地址 * @param filePath * @returns */ export declare function getPossibleTsSrcPath(filePath: string): string[]; /** * 是否是脚本文件请求,目前只支持js、ts、joker文件 * @param url * @returns */ export declare function isJSRequest(url: string): boolean; export declare function isInNodeModules(id: string): boolean; export declare function isJSONRequest(url: string): boolean; export declare function isHTMLRequest(url: string): boolean; export declare const JS_EXTENSION_RE: RegExp; export declare const JS_MAP_EXTENSION_RE: RegExp; export declare const CSS_LANG_ARRAY: string[]; export declare const CSS_LANG: string; export declare const CSS_LANG_RE: RegExp; export declare const SHIMS_CSS_LANG: string; export declare const SHIMS_CSS_LANG_RE: RegExp; /** * 是否是样式文件请求 * @param url */ export declare function isCssRequest(url: string): boolean; export declare function isDirectCssRequest(request: string): boolean; /** * 是否是dep请求,dep请求具有v=hash的特性 * @param url * @returns */ export declare function isDepRequest(url: string): boolean; /** * 转换‘@id’协议-逆向 * @param url * @returns */ export declare function unwarpId(id: string): string; /** * 转换‘@id’协议 * @param id * @returns */ export declare function warpId(id: string): string; /** * 是否是带有direct特性的url * @param url * @returns */ export declare function isDirectRequest(url: string): boolean; /** * @example @joker.front/cli 或者 @joker.front/cli/xxxx/index.js */ export declare const BARE_IMPORT_RE: RegExp; export declare function isBareImportRequest(id: string): boolean; export declare function resolveFrom(id: string, baseDir: string, preserveSymlinks?: boolean): string; export declare function nestedResolveFrom(id: string, baseDir: string, preserveSymlinks?: boolean): string; export declare function isNonDriveRelativeAbsolutePath(p: string): boolean; export declare function toUpperCaseDriveLetter(pathName: string): string; export declare function prettifyUrl(url: string, root: string): string; export declare function isParentDirectory(dir: string, file: string): boolean; export declare function fsPathFromId(id: string): string; export declare function fsPathFromUrl(url: string): string; export declare const REQUEST_QUERY_SPLIT_RE: RegExp; export declare function parseRequest(id: string): Record<string, string> | undefined; /** * 打开浏览器 * @param url * @returns */ export declare function openBrowser(url: string): void; /** * 读取JSON文件 * @param filePath * @returns */ export declare function readJSON(filePath: string): any; /** * 返回公共文件路径 * 所有public文件都会在根目录中提供索引,如果找到,则按照公共资源处理,否则返回空 * 交由下面程序处理 * @param publicDir public目录 * @param url 文件URL * @returns */ export declare function getPublicFilePath(publicDir: string | false, url: string): string | undefined; /** * 清空文件夹 * @param dir 文件夹地址 * @param skip 需要跳过/排除的文件 */ export declare function emptyDir(dir: string, skip?: string[]): void; /** * 复制文件夹 * @param dir 原文件夹 * @param aim 目标文件夹 */ export declare function copyDir(dir: string, aim: string): void; /** * 创建/写入一个文件 * @param filePath 文件地址 * @param data 文件内容 */ export declare function writeFile(filePath: string, data: string): void; /** * 删除文件夹 * * @returns Promise<void>|void */ export declare const removeDir: (dir: string) => void; /** * 重命名文件夹 * @returns Promise<void>|void */ export declare const renameDir: ((arg1: string, arg2: string) => Promise<void>) | typeof fs.renameSync; export declare function getFileStat(fileName: string): fs.Stats | undefined; export declare const LINE_RE: RegExp; /** * offset -> pos * @param source * @param offset * @returns */ export declare function offsetToPosition(source: string, offset: number | { line: number; column: number; }): { line: number; column: number; }; /** * pos -> offset * @param source * @param pos * @returns */ export declare function positionToOffset(source: string, pos: number | { line: number; column: number; }): number; /** * 输出代码,并对其进行标注,适用于错误、警告位置标注 * @param source * @param start * @param end * @returns */ export declare function generateCodeFrame(source: string, start: number | { line: number; column: number; }, end?: number): string; /** * 通过rolluperror创建带颜色标注的错误提示 * @param err * @param args * @param stack */ export declare function createErrorMsgFromRollupError(err: RollupError): string; export declare function tabLineContent(content: string, tab?: number): string; export declare function clearnStack(stack: string): string; export declare const NULL_SOURCE_MAP: RawSourceMap; export declare function escapeToLinuxLikePath(path: string): string; export declare function unescapeToLinuxLikePath(path: string): string; export declare function combineSourceMaps(fileName: string, sourceMapList: Array<DecodedSourceMap | RawSourceMap>, excludeContent?: boolean): any; /** * 查找文件 * @param dir 目录 * @param fileName 文件名称 * @param rtPath 是否返回地址,false则返回文件内容'utf-8' * @returns 针对fileName传递数组时,只要找到一个即返回内容 */ export declare function lookupFile(dir: string, fileName: string | string[], rtPath?: boolean): string | undefined; /** * 获取buffer/string 的hash值 * @param text 值 * @param max hash最大长度,默认8 * @returns */ export declare function getHash(text: Buffer | string, max?: number): string; /** * 创建一个可操作的Promise */ export declare function createOperablePromise(): { promise: Promise<void>; resolve: () => void; }; /** * 将id/path进行打平 * @param id */ export declare function flattenId(id: string): string; /** * 摘取部分Object内容 * @param obj * @param key * @returns */ export declare function getPartObject<T>(obj: Record<string, T>, key: keyof T): Record<string, any>; export interface ControlPromise { promise: Promise<void>; resolve: () => void; } /** * 返回一个可控制的promise,用于主动控制异步状态 * @returns */ export declare function createControlPromise(): ControlPromise; /** * 判断当前id是否包含在list中 * @param list * @param id * @returns */ export declare function moduleListContains(list: string[] | undefined, id: string): boolean | undefined; /** * 去除内容中 UTF-8 BOM * @param content * @returns */ export declare function stripBomTag(content: string): string; export declare function transformStableResult(s: MagicString, id: string, config: ResolvedConfig): TransformResult; export declare function strip(str: string): string; /** * 异步替换 * @param input * @param re * @param replacer * @returns */ export declare function asyncReplace(input: string, re: RegExp, replacer: (match: RegExpExecArray) => string | Promise<string>): Promise<string>; /** * 在内容中追加SourceMap * @param type * @param code * @param map * @returns */ export declare function getCodeWithSourcemap(type: "js" | "css", code: string, map: RollupSourceMap | undefined): string; export declare function getSourceMapUrl(map?: RollupSourceMap | string): string; export declare const COMMENT_RE: RegExp; export declare const MULTI_LINE_COMMENT_RE: RegExp; export declare const SINGLE_LINE_COMMENT_RE: RegExp; export declare function clearCssComments(raw: string): string; export type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null; export declare const createFilter: (include?: FilterPattern, exclude?: FilterPattern, options?: { resolve?: string | false | null; }) => (id: string | unknown) => boolean; export declare function evalValue<T = any>(rawValue: string): T; export {};