|
| 1 | +// misc utilities copied from vite |
| 2 | + |
| 3 | +import fs from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import type { DevEnvironment, ErrorPayload, Rollup } from 'vite' |
| 6 | +import { stripVTControlCharacters as strip } from 'node:util' |
| 7 | + |
| 8 | +export const VALID_ID_PREFIX = `/@id/` |
| 9 | + |
| 10 | +export const NULL_BYTE_PLACEHOLDER = `__x00__` |
| 11 | + |
| 12 | +export const FS_PREFIX = `/@fs/` |
| 13 | + |
| 14 | +export function wrapId(id: string): string { |
| 15 | + return id.startsWith(VALID_ID_PREFIX) |
| 16 | + ? id |
| 17 | + : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER) |
| 18 | +} |
| 19 | + |
| 20 | +export function unwrapId(id: string): string { |
| 21 | + return id.startsWith(VALID_ID_PREFIX) |
| 22 | + ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0') |
| 23 | + : id |
| 24 | +} |
| 25 | + |
| 26 | +export function withTrailingSlash(path: string): string { |
| 27 | + if (path[path.length - 1] !== '/') { |
| 28 | + return `${path}/` |
| 29 | + } |
| 30 | + return path |
| 31 | +} |
| 32 | + |
| 33 | +const postfixRE = /[?#].*$/ |
| 34 | +export function cleanUrl(url: string): string { |
| 35 | + return url.replace(postfixRE, '') |
| 36 | +} |
| 37 | + |
| 38 | +export function splitFileAndPostfix(path: string): { |
| 39 | + file: string |
| 40 | + postfix: string |
| 41 | +} { |
| 42 | + const file = cleanUrl(path) |
| 43 | + return { file, postfix: path.slice(file.length) } |
| 44 | +} |
| 45 | + |
| 46 | +const windowsSlashRE = /\\/g |
| 47 | +export function slash(p: string): string { |
| 48 | + return p.replace(windowsSlashRE, '/') |
| 49 | +} |
| 50 | + |
| 51 | +const isWindows = typeof process !== 'undefined' && process.platform === 'win32' |
| 52 | + |
| 53 | +export function injectQuery(url: string, queryToInject: string): string { |
| 54 | + const { file, postfix } = splitFileAndPostfix(url) |
| 55 | + const normalizedFile = isWindows ? slash(file) : file |
| 56 | + return `${normalizedFile}?${queryToInject}${ |
| 57 | + postfix[0] === '?' ? `&${postfix.slice(1)}` : /* hash only */ postfix |
| 58 | + }` |
| 59 | +} |
| 60 | + |
| 61 | +export function joinUrlSegments(a: string, b: string): string { |
| 62 | + if (!a || !b) { |
| 63 | + return a || b || '' |
| 64 | + } |
| 65 | + if (a.endsWith('/')) { |
| 66 | + a = a.substring(0, a.length - 1) |
| 67 | + } |
| 68 | + if (b[0] !== '/') { |
| 69 | + b = '/' + b |
| 70 | + } |
| 71 | + return a + b |
| 72 | +} |
| 73 | + |
| 74 | +export function normalizeResolvedIdToUrl( |
| 75 | + environment: DevEnvironment, |
| 76 | + url: string, |
| 77 | + resolved: Rollup.PartialResolvedId, |
| 78 | +): string { |
| 79 | + const root = environment.config.root |
| 80 | + const depsOptimizer = environment.depsOptimizer |
| 81 | + |
| 82 | + // normalize all imports into resolved URLs |
| 83 | + // e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js'` |
| 84 | + if (resolved.id.startsWith(withTrailingSlash(root))) { |
| 85 | + // in root: infer short absolute path from root |
| 86 | + url = resolved.id.slice(root.length) |
| 87 | + } else if ( |
| 88 | + depsOptimizer?.isOptimizedDepFile(resolved.id) || |
| 89 | + // vite-plugin-react isn't following the leading \0 virtual module convention. |
| 90 | + // This is a temporary hack to avoid expensive fs checks for React apps. |
| 91 | + // We'll remove this as soon we're able to fix the react plugins. |
| 92 | + (resolved.id !== '/@react-refresh' && |
| 93 | + path.isAbsolute(resolved.id) && |
| 94 | + fs.existsSync(cleanUrl(resolved.id))) |
| 95 | + ) { |
| 96 | + // an optimized deps may not yet exists in the filesystem, or |
| 97 | + // a regular file exists but is out of root: rewrite to absolute /@fs/ paths |
| 98 | + url = path.posix.join(FS_PREFIX, resolved.id) |
| 99 | + } else { |
| 100 | + url = resolved.id |
| 101 | + } |
| 102 | + |
| 103 | + // if the resolved id is not a valid browser import specifier, |
| 104 | + // prefix it to make it valid. We will strip this before feeding it |
| 105 | + // back into the transform pipeline |
| 106 | + if (url[0] !== '.' && url[0] !== '/') { |
| 107 | + url = wrapId(resolved.id) |
| 108 | + } |
| 109 | + |
| 110 | + return url |
| 111 | +} |
| 112 | + |
| 113 | +export function normalizeViteImportAnalysisUrl( |
| 114 | + environment: DevEnvironment, |
| 115 | + id: string, |
| 116 | +): string { |
| 117 | + let url = normalizeResolvedIdToUrl(environment, id, { id }) |
| 118 | + |
| 119 | + // https://github.com/vitejs/vite/blob/c18ce868c4d70873406e9f7d1b2d0a03264d2168/packages/vite/src/node/plugins/importAnalysis.ts#L416 |
| 120 | + if (environment.config.consumer === 'client') { |
| 121 | + const mod = environment.moduleGraph.getModuleById(id) |
| 122 | + if (mod && mod.lastHMRTimestamp > 0) { |
| 123 | + url = injectQuery(url, `t=${mod.lastHMRTimestamp}`) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return url |
| 128 | +} |
| 129 | + |
| 130 | +// error formatting |
| 131 | +// https://github.com/vitejs/vite/blob/8033e5bf8d3ff43995d0620490ed8739c59171dd/packages/vite/src/node/server/middlewares/error.ts#L11 |
| 132 | + |
| 133 | +type RollupError = Rollup.RollupError |
| 134 | + |
| 135 | +export function prepareError(err: Error | RollupError): ErrorPayload['err'] { |
| 136 | + // only copy the information we need and avoid serializing unnecessary |
| 137 | + // properties, since some errors may attach full objects (e.g. PostCSS) |
| 138 | + return { |
| 139 | + message: strip(err.message), |
| 140 | + stack: strip(cleanStack(err.stack || '')), |
| 141 | + id: (err as RollupError).id, |
| 142 | + frame: strip((err as RollupError).frame || ''), |
| 143 | + plugin: (err as RollupError).plugin, |
| 144 | + pluginCode: (err as RollupError).pluginCode?.toString(), |
| 145 | + loc: (err as RollupError).loc, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +function cleanStack(stack: string) { |
| 150 | + return stack |
| 151 | + .split(/\n/) |
| 152 | + .filter((l) => /^\s*at/.test(l)) |
| 153 | + .join('\n') |
| 154 | +} |
| 155 | + |
| 156 | +// https://github.com/vitejs/vite/blob/ea9aed7ebcb7f4be542bd2a384cbcb5a1e7b31bd/packages/vite/src/node/utils.ts#L1469-L1475 |
| 157 | +export function evalValue<T = any>(rawValue: string): T { |
| 158 | + const fn = new Function(` |
| 159 | + var console, exports, global, module, process, require |
| 160 | + return (\n${rawValue}\n) |
| 161 | + `) |
| 162 | + return fn() |
| 163 | +} |
| 164 | + |
| 165 | +// https://github.com/vitejs/vite/blob/84079a84ad94de4c1ef4f1bdb2ab448ff2c01196/packages/vite/src/node/utils.ts#L321 |
| 166 | +export const directRequestRE: RegExp = /(\?|&)direct=?(?:&|$)/ |
0 commit comments