vite 2.6.0-beta.0 → 2.6.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of vite might be problematic. Click here for more details.
- package/CHANGELOG.md +66 -0
- package/LICENSE.md +161 -3011
- package/bin/vite.js +2 -1
- package/dist/node/chunks/{dep-91aa74e8.js → dep-1e0a75a8.js} +58 -32
- package/dist/node/chunks/dep-1e0a75a8.js.map +1 -0
- package/dist/node/chunks/{dep-e36486f6.js → dep-ac1b4bf9.js} +1 -1
- package/dist/node/chunks/dep-ac1b4bf9.js.map +1 -0
- package/dist/node/chunks/{dep-c7e510f9.js → dep-ba6b30a0.js} +27538 -4953
- package/dist/node/chunks/dep-ba6b30a0.js.map +1 -0
- package/dist/node/chunks/{dep-11213a75.js → dep-c4cf6e92.js} +19 -6
- package/dist/node/chunks/dep-c4cf6e92.js.map +1 -0
- package/dist/node/chunks/{dep-eb6ef720.js → dep-d574094c.js} +18 -5
- package/dist/node/chunks/dep-d574094c.js.map +1 -0
- package/dist/node/chunks/{dep-0d2f9464.js → dep-e39b05d6.js} +18 -5
- package/dist/node/chunks/dep-e39b05d6.js.map +1 -0
- package/dist/node/cli.js +60 -23992
- package/dist/node/cli.js.map +1 -1
- package/dist/node/index.d.ts +26 -8
- package/dist/node/index.js +18 -15
- package/dist/node/index.js.map +1 -1
- package/dist/node/terser.js +102 -55
- package/package.json +28 -13
- package/types/package.json +3 -0
- package/api-extractor.json +0 -54
- package/dist/node/chunks/dep-0d2f9464.js.map +0 -1
- package/dist/node/chunks/dep-11213a75.js.map +0 -1
- package/dist/node/chunks/dep-91aa74e8.js.map +0 -1
- package/dist/node/chunks/dep-c7e510f9.js.map +0 -1
- package/dist/node/chunks/dep-e36486f6.js.map +0 -1
- package/dist/node/chunks/dep-eb6ef720.js.map +0 -1
- package/dist/node/terser.js.map +0 -1
- package/rollup.config.js +0 -389
- package/scripts/patchTypes.js +0 -70
- package/src/node/__tests__/asset.spec.ts +0 -156
- package/src/node/__tests__/build.spec.ts +0 -67
- package/src/node/__tests__/config.spec.ts +0 -166
- package/src/node/__tests__/packages/name/package.json +0 -3
- package/src/node/__tests__/packages/noname/package.json +0 -1
- package/src/node/__tests__/plugins/css.spec.ts +0 -116
- package/src/node/__tests__/scan.spec.ts +0 -118
- package/src/node/__tests__/utils.spec.ts +0 -40
- package/src/node/build.ts +0 -783
- package/src/node/cli.ts +0 -239
- package/src/node/config.ts +0 -1033
- package/src/node/constants.ts +0 -87
- package/src/node/importGlob.ts +0 -173
- package/src/node/index.ts +0 -88
- package/src/node/logger.ts +0 -167
- package/src/node/optimizer/esbuildDepPlugin.ts +0 -216
- package/src/node/optimizer/index.ts +0 -410
- package/src/node/optimizer/registerMissing.ts +0 -102
- package/src/node/optimizer/scan.ts +0 -457
- package/src/node/plugin.ts +0 -138
- package/src/node/plugins/asset.ts +0 -365
- package/src/node/plugins/assetImportMetaUrl.ts +0 -99
- package/src/node/plugins/clientInjections.ts +0 -72
- package/src/node/plugins/css.ts +0 -1279
- package/src/node/plugins/dataUri.ts +0 -64
- package/src/node/plugins/define.ts +0 -107
- package/src/node/plugins/esbuild.ts +0 -280
- package/src/node/plugins/html.ts +0 -673
- package/src/node/plugins/importAnalysis.ts +0 -614
- package/src/node/plugins/importAnalysisBuild.ts +0 -334
- package/src/node/plugins/index.ts +0 -69
- package/src/node/plugins/json.ts +0 -75
- package/src/node/plugins/loadFallback.ts +0 -19
- package/src/node/plugins/manifest.ts +0 -123
- package/src/node/plugins/modulePreloadPolyfill.ts +0 -100
- package/src/node/plugins/preAlias.ts +0 -22
- package/src/node/plugins/reporter.ts +0 -244
- package/src/node/plugins/resolve.ts +0 -925
- package/src/node/plugins/terser.ts +0 -40
- package/src/node/plugins/wasm.ts +0 -72
- package/src/node/plugins/worker.ts +0 -117
- package/src/node/preview.ts +0 -82
- package/src/node/server/__tests__/fixtures/none/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/pnpm-workspace.yaml +0 -0
- package/src/node/server/__tests__/fixtures/yarn/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/yarn/package.json +0 -6
- package/src/node/server/__tests__/search-root.spec.ts +0 -31
- package/src/node/server/hmr.ts +0 -489
- package/src/node/server/http.ts +0 -198
- package/src/node/server/index.ts +0 -705
- package/src/node/server/middlewares/base.ts +0 -52
- package/src/node/server/middlewares/error.ts +0 -98
- package/src/node/server/middlewares/indexHtml.ts +0 -170
- package/src/node/server/middlewares/proxy.ts +0 -124
- package/src/node/server/middlewares/spaFallback.ts +0 -32
- package/src/node/server/middlewares/static.ts +0 -153
- package/src/node/server/middlewares/time.ts +0 -18
- package/src/node/server/middlewares/transform.ts +0 -196
- package/src/node/server/moduleGraph.ts +0 -200
- package/src/node/server/openBrowser.ts +0 -101
- package/src/node/server/pluginContainer.ts +0 -546
- package/src/node/server/searchRoot.ts +0 -70
- package/src/node/server/send.ts +0 -54
- package/src/node/server/sourcemap.ts +0 -54
- package/src/node/server/transformRequest.ts +0 -168
- package/src/node/server/ws.ts +0 -131
- package/src/node/ssr/__tests__/ssrTransform.spec.ts +0 -309
- package/src/node/ssr/ssrExternal.ts +0 -161
- package/src/node/ssr/ssrManifestPlugin.ts +0 -53
- package/src/node/ssr/ssrModuleLoader.ts +0 -214
- package/src/node/ssr/ssrStacktrace.ts +0 -75
- package/src/node/ssr/ssrTransform.ts +0 -452
- package/src/node/tsconfig.json +0 -14
- package/src/node/utils.ts +0 -565
- package/tsconfig.base.json +0 -11
package/src/node/utils.ts
DELETED
|
@@ -1,565 +0,0 @@
|
|
|
1
|
-
import debug from 'debug'
|
|
2
|
-
import chalk from 'chalk'
|
|
3
|
-
import fs from 'fs'
|
|
4
|
-
import os from 'os'
|
|
5
|
-
import path from 'path'
|
|
6
|
-
import { pathToFileURL, URL } from 'url'
|
|
7
|
-
import {
|
|
8
|
-
FS_PREFIX,
|
|
9
|
-
DEFAULT_EXTENSIONS,
|
|
10
|
-
VALID_ID_PREFIX,
|
|
11
|
-
CLIENT_PUBLIC_PATH,
|
|
12
|
-
ENV_PUBLIC_PATH
|
|
13
|
-
} from './constants'
|
|
14
|
-
import resolve from 'resolve'
|
|
15
|
-
import builtins from 'builtin-modules'
|
|
16
|
-
import { FSWatcher } from 'chokidar'
|
|
17
|
-
import remapping from '@ampproject/remapping'
|
|
18
|
-
import {
|
|
19
|
-
DecodedSourceMap,
|
|
20
|
-
RawSourceMap
|
|
21
|
-
} from '@ampproject/remapping/dist/types/types'
|
|
22
|
-
|
|
23
|
-
export function slash(p: string): string {
|
|
24
|
-
return p.replace(/\\/g, '/')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Strip valid id prefix. This is prepended to resolved Ids that are
|
|
28
|
-
// not valid browser import specifiers by the importAnalysis plugin.
|
|
29
|
-
export function unwrapId(id: string): string {
|
|
30
|
-
return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length) : id
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const flattenId = (id: string): string =>
|
|
34
|
-
id.replace(/(\s*>\s*)/g, '__').replace(/[\/\.]/g, '_')
|
|
35
|
-
|
|
36
|
-
export const normalizeId = (id: string): string =>
|
|
37
|
-
id.replace(/(\s*>\s*)/g, ' > ')
|
|
38
|
-
|
|
39
|
-
export function isBuiltin(id: string): boolean {
|
|
40
|
-
return builtins.includes(id)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const bareImportRE = /^[\w@](?!.*:\/\/)/
|
|
44
|
-
export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//
|
|
45
|
-
|
|
46
|
-
export let isRunningWithYarnPnp: boolean
|
|
47
|
-
try {
|
|
48
|
-
isRunningWithYarnPnp = Boolean(require('pnpapi'))
|
|
49
|
-
} catch {}
|
|
50
|
-
|
|
51
|
-
const ssrExtensions = ['.js', '.cjs', '.json', '.node']
|
|
52
|
-
|
|
53
|
-
export function resolveFrom(
|
|
54
|
-
id: string,
|
|
55
|
-
basedir: string,
|
|
56
|
-
preserveSymlinks = false,
|
|
57
|
-
ssr = false
|
|
58
|
-
): string {
|
|
59
|
-
return resolve.sync(id, {
|
|
60
|
-
basedir,
|
|
61
|
-
extensions: ssr ? ssrExtensions : DEFAULT_EXTENSIONS,
|
|
62
|
-
// necessary to work with pnpm
|
|
63
|
-
preserveSymlinks: preserveSymlinks || isRunningWithYarnPnp || false
|
|
64
|
-
})
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* like `resolveFrom` but supports resolving `>` path in `id`,
|
|
69
|
-
* for example: `foo > bar > baz`
|
|
70
|
-
*/
|
|
71
|
-
export function nestedResolveFrom(
|
|
72
|
-
id: string,
|
|
73
|
-
basedir: string,
|
|
74
|
-
preserveSymlinks = false
|
|
75
|
-
): string {
|
|
76
|
-
const pkgs = id.split('>').map((pkg) => pkg.trim())
|
|
77
|
-
try {
|
|
78
|
-
for (const pkg of pkgs) {
|
|
79
|
-
basedir = resolveFrom(pkg, basedir, preserveSymlinks)
|
|
80
|
-
}
|
|
81
|
-
} catch {}
|
|
82
|
-
return basedir
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// set in bin/vite.js
|
|
86
|
-
const filter = process.env.VITE_DEBUG_FILTER
|
|
87
|
-
|
|
88
|
-
const DEBUG = process.env.DEBUG
|
|
89
|
-
|
|
90
|
-
interface DebuggerOptions {
|
|
91
|
-
onlyWhenFocused?: boolean | string
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export type ViteDebugScope = `vite:${string}`
|
|
95
|
-
|
|
96
|
-
export function createDebugger(
|
|
97
|
-
namespace: ViteDebugScope,
|
|
98
|
-
options: DebuggerOptions = {}
|
|
99
|
-
): debug.Debugger['log'] {
|
|
100
|
-
const log = debug(namespace)
|
|
101
|
-
const { onlyWhenFocused } = options
|
|
102
|
-
const focus =
|
|
103
|
-
typeof onlyWhenFocused === 'string' ? onlyWhenFocused : namespace
|
|
104
|
-
return (msg: string, ...args: any[]) => {
|
|
105
|
-
if (filter && !msg.includes(filter)) {
|
|
106
|
-
return
|
|
107
|
-
}
|
|
108
|
-
if (onlyWhenFocused && !DEBUG?.includes(focus)) {
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
log(msg, ...args)
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export const isWindows = os.platform() === 'win32'
|
|
116
|
-
const VOLUME_RE = /^[A-Z]:/i
|
|
117
|
-
|
|
118
|
-
export function normalizePath(id: string): string {
|
|
119
|
-
return path.posix.normalize(isWindows ? slash(id) : id)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function fsPathFromId(id: string): string {
|
|
123
|
-
const fsPath = normalizePath(id.slice(FS_PREFIX.length))
|
|
124
|
-
return fsPath.startsWith('/') || fsPath.match(VOLUME_RE)
|
|
125
|
-
? fsPath
|
|
126
|
-
: `/${fsPath}`
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function ensureVolumeInPath(file: string): string {
|
|
130
|
-
return isWindows ? path.resolve(file) : file
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export const queryRE = /\?.*$/s
|
|
134
|
-
export const hashRE = /#.*$/s
|
|
135
|
-
|
|
136
|
-
export const cleanUrl = (url: string): string =>
|
|
137
|
-
url.replace(hashRE, '').replace(queryRE, '')
|
|
138
|
-
|
|
139
|
-
export const externalRE = /^(https?:)?\/\//
|
|
140
|
-
export const isExternalUrl = (url: string): boolean => externalRE.test(url)
|
|
141
|
-
|
|
142
|
-
export const dataUrlRE = /^\s*data:/i
|
|
143
|
-
export const isDataUrl = (url: string): boolean => dataUrlRE.test(url)
|
|
144
|
-
|
|
145
|
-
const knownJsSrcRE = /\.((j|t)sx?|mjs|vue|marko|svelte)($|\?)/
|
|
146
|
-
export const isJSRequest = (url: string): boolean => {
|
|
147
|
-
url = cleanUrl(url)
|
|
148
|
-
if (knownJsSrcRE.test(url)) {
|
|
149
|
-
return true
|
|
150
|
-
}
|
|
151
|
-
if (!path.extname(url) && !url.endsWith('/')) {
|
|
152
|
-
return true
|
|
153
|
-
}
|
|
154
|
-
return false
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const importQueryRE = /(\?|&)import=?(?:&|$)/
|
|
158
|
-
const internalPrefixes = [
|
|
159
|
-
FS_PREFIX,
|
|
160
|
-
VALID_ID_PREFIX,
|
|
161
|
-
CLIENT_PUBLIC_PATH,
|
|
162
|
-
ENV_PUBLIC_PATH
|
|
163
|
-
]
|
|
164
|
-
const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join('|')})`)
|
|
165
|
-
const trailingSeparatorRE = /[\?&]$/
|
|
166
|
-
export const isImportRequest = (url: string): boolean => importQueryRE.test(url)
|
|
167
|
-
export const isInternalRequest = (url: string): boolean =>
|
|
168
|
-
InternalPrefixRE.test(url)
|
|
169
|
-
|
|
170
|
-
export function removeImportQuery(url: string): string {
|
|
171
|
-
return url.replace(importQueryRE, '$1').replace(trailingSeparatorRE, '')
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function injectQuery(url: string, queryToInject: string): string {
|
|
175
|
-
// encode percents for consistent behavior with pathToFileURL
|
|
176
|
-
// see #2614 for details
|
|
177
|
-
let resolvedUrl = new URL(url.replace(/%/g, '%25'), 'relative:///')
|
|
178
|
-
if (resolvedUrl.protocol !== 'relative:') {
|
|
179
|
-
resolvedUrl = pathToFileURL(url)
|
|
180
|
-
}
|
|
181
|
-
let { protocol, pathname, search, hash } = resolvedUrl
|
|
182
|
-
if (protocol === 'file:') {
|
|
183
|
-
pathname = pathname.slice(1)
|
|
184
|
-
}
|
|
185
|
-
pathname = decodeURIComponent(pathname)
|
|
186
|
-
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
|
|
187
|
-
hash || ''
|
|
188
|
-
}`
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const timestampRE = /\bt=\d{13}&?\b/
|
|
192
|
-
export function removeTimestampQuery(url: string): string {
|
|
193
|
-
return url.replace(timestampRE, '').replace(trailingSeparatorRE, '')
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
export async function asyncReplace(
|
|
197
|
-
input: string,
|
|
198
|
-
re: RegExp,
|
|
199
|
-
replacer: (match: RegExpExecArray) => string | Promise<string>
|
|
200
|
-
): Promise<string> {
|
|
201
|
-
let match: RegExpExecArray | null
|
|
202
|
-
let remaining = input
|
|
203
|
-
let rewritten = ''
|
|
204
|
-
while ((match = re.exec(remaining))) {
|
|
205
|
-
rewritten += remaining.slice(0, match.index)
|
|
206
|
-
rewritten += await replacer(match)
|
|
207
|
-
remaining = remaining.slice(match.index + match[0].length)
|
|
208
|
-
}
|
|
209
|
-
rewritten += remaining
|
|
210
|
-
return rewritten
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export function timeFrom(start: number, subtract = 0): string {
|
|
214
|
-
const time: number | string = Date.now() - start - subtract
|
|
215
|
-
const timeString = (time + `ms`).padEnd(5, ' ')
|
|
216
|
-
if (time < 10) {
|
|
217
|
-
return chalk.green(timeString)
|
|
218
|
-
} else if (time < 50) {
|
|
219
|
-
return chalk.yellow(timeString)
|
|
220
|
-
} else {
|
|
221
|
-
return chalk.red(timeString)
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* pretty url for logging.
|
|
227
|
-
*/
|
|
228
|
-
export function prettifyUrl(url: string, root: string): string {
|
|
229
|
-
url = removeTimestampQuery(url)
|
|
230
|
-
const isAbsoluteFile = url.startsWith(root)
|
|
231
|
-
if (isAbsoluteFile || url.startsWith(FS_PREFIX)) {
|
|
232
|
-
let file = path.relative(root, isAbsoluteFile ? url : fsPathFromId(url))
|
|
233
|
-
const seg = file.split('/')
|
|
234
|
-
const npmIndex = seg.indexOf(`node_modules`)
|
|
235
|
-
const isSourceMap = file.endsWith('.map')
|
|
236
|
-
if (npmIndex > 0) {
|
|
237
|
-
file = seg[npmIndex + 1]
|
|
238
|
-
if (file.startsWith('@')) {
|
|
239
|
-
file = `${file}/${seg[npmIndex + 2]}`
|
|
240
|
-
}
|
|
241
|
-
file = `npm: ${chalk.dim(file)}${isSourceMap ? ` (source map)` : ``}`
|
|
242
|
-
}
|
|
243
|
-
return chalk.dim(file)
|
|
244
|
-
} else {
|
|
245
|
-
return chalk.dim(url)
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
export function isObject(value: unknown): value is Record<string, any> {
|
|
250
|
-
return Object.prototype.toString.call(value) === '[object Object]'
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
export function isDefined<T>(value: T | undefined | null): value is T {
|
|
254
|
-
return value != null
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export function lookupFile(
|
|
258
|
-
dir: string,
|
|
259
|
-
formats: string[],
|
|
260
|
-
pathOnly = false
|
|
261
|
-
): string | undefined {
|
|
262
|
-
for (const format of formats) {
|
|
263
|
-
const fullPath = path.join(dir, format)
|
|
264
|
-
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
|
|
265
|
-
return pathOnly ? fullPath : fs.readFileSync(fullPath, 'utf-8')
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
const parentDir = path.dirname(dir)
|
|
269
|
-
if (parentDir !== dir) {
|
|
270
|
-
return lookupFile(parentDir, formats, pathOnly)
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const splitRE = /\r?\n/
|
|
275
|
-
|
|
276
|
-
const range: number = 2
|
|
277
|
-
|
|
278
|
-
export function pad(source: string, n = 2): string {
|
|
279
|
-
const lines = source.split(splitRE)
|
|
280
|
-
return lines.map((l) => ` `.repeat(n) + l).join(`\n`)
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export function posToNumber(
|
|
284
|
-
source: string,
|
|
285
|
-
pos: number | { line: number; column: number }
|
|
286
|
-
): number {
|
|
287
|
-
if (typeof pos === 'number') return pos
|
|
288
|
-
const lines = source.split(splitRE)
|
|
289
|
-
const { line, column } = pos
|
|
290
|
-
let start = 0
|
|
291
|
-
for (let i = 0; i < line - 1; i++) {
|
|
292
|
-
start += lines[i].length + 1
|
|
293
|
-
}
|
|
294
|
-
return start + column
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export function numberToPos(
|
|
298
|
-
source: string,
|
|
299
|
-
offset: number | { line: number; column: number }
|
|
300
|
-
): { line: number; column: number } {
|
|
301
|
-
if (typeof offset !== 'number') return offset
|
|
302
|
-
if (offset > source.length) {
|
|
303
|
-
throw new Error(
|
|
304
|
-
`offset is longer than source length! offset ${offset} > length ${source.length}`
|
|
305
|
-
)
|
|
306
|
-
}
|
|
307
|
-
const lines = source.split(splitRE)
|
|
308
|
-
let counted = 0
|
|
309
|
-
let line = 0
|
|
310
|
-
let column = 0
|
|
311
|
-
for (; line < lines.length; line++) {
|
|
312
|
-
const lineLength = lines[line].length + 1
|
|
313
|
-
if (counted + lineLength >= offset) {
|
|
314
|
-
column = offset - counted + 1
|
|
315
|
-
break
|
|
316
|
-
}
|
|
317
|
-
counted += lineLength
|
|
318
|
-
}
|
|
319
|
-
return { line: line + 1, column }
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
export function generateCodeFrame(
|
|
323
|
-
source: string,
|
|
324
|
-
start: number | { line: number; column: number } = 0,
|
|
325
|
-
end?: number
|
|
326
|
-
): string {
|
|
327
|
-
start = posToNumber(source, start)
|
|
328
|
-
end = end || start
|
|
329
|
-
const lines = source.split(splitRE)
|
|
330
|
-
let count = 0
|
|
331
|
-
const res: string[] = []
|
|
332
|
-
for (let i = 0; i < lines.length; i++) {
|
|
333
|
-
count += lines[i].length + 1
|
|
334
|
-
if (count >= start) {
|
|
335
|
-
for (let j = i - range; j <= i + range || end > count; j++) {
|
|
336
|
-
if (j < 0 || j >= lines.length) continue
|
|
337
|
-
const line = j + 1
|
|
338
|
-
res.push(
|
|
339
|
-
`${line}${' '.repeat(Math.max(3 - String(line).length, 0))}| ${
|
|
340
|
-
lines[j]
|
|
341
|
-
}`
|
|
342
|
-
)
|
|
343
|
-
const lineLength = lines[j].length
|
|
344
|
-
if (j === i) {
|
|
345
|
-
// push underline
|
|
346
|
-
const pad = start - (count - lineLength) + 1
|
|
347
|
-
const length = Math.max(
|
|
348
|
-
1,
|
|
349
|
-
end > count ? lineLength - pad : end - start
|
|
350
|
-
)
|
|
351
|
-
res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length))
|
|
352
|
-
} else if (j > i) {
|
|
353
|
-
if (end > count) {
|
|
354
|
-
const length = Math.max(Math.min(end - count, lineLength), 1)
|
|
355
|
-
res.push(` | ` + '^'.repeat(length))
|
|
356
|
-
}
|
|
357
|
-
count += lineLength + 1
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
break
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return res.join('\n')
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
export function writeFile(
|
|
367
|
-
filename: string,
|
|
368
|
-
content: string | Uint8Array
|
|
369
|
-
): void {
|
|
370
|
-
const dir = path.dirname(filename)
|
|
371
|
-
if (!fs.existsSync(dir)) {
|
|
372
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
373
|
-
}
|
|
374
|
-
fs.writeFileSync(filename, content)
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Delete every file and subdirectory. **The given directory must exist.**
|
|
379
|
-
* Pass an optional `skip` array to preserve files in the root directory.
|
|
380
|
-
*/
|
|
381
|
-
export function emptyDir(dir: string, skip?: string[]): void {
|
|
382
|
-
for (const file of fs.readdirSync(dir)) {
|
|
383
|
-
if (skip?.includes(file)) {
|
|
384
|
-
continue
|
|
385
|
-
}
|
|
386
|
-
const abs = path.resolve(dir, file)
|
|
387
|
-
// baseline is Node 12 so can't use rmSync :(
|
|
388
|
-
if (fs.lstatSync(abs).isDirectory()) {
|
|
389
|
-
emptyDir(abs)
|
|
390
|
-
fs.rmdirSync(abs)
|
|
391
|
-
} else {
|
|
392
|
-
fs.unlinkSync(abs)
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
export function copyDir(srcDir: string, destDir: string): void {
|
|
398
|
-
fs.mkdirSync(destDir, { recursive: true })
|
|
399
|
-
for (const file of fs.readdirSync(srcDir)) {
|
|
400
|
-
const srcFile = path.resolve(srcDir, file)
|
|
401
|
-
if (srcFile === destDir) {
|
|
402
|
-
continue
|
|
403
|
-
}
|
|
404
|
-
const destFile = path.resolve(destDir, file)
|
|
405
|
-
const stat = fs.statSync(srcFile)
|
|
406
|
-
if (stat.isDirectory()) {
|
|
407
|
-
copyDir(srcFile, destFile)
|
|
408
|
-
} else {
|
|
409
|
-
fs.copyFileSync(srcFile, destFile)
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
export function ensureLeadingSlash(path: string): string {
|
|
415
|
-
return !path.startsWith('/') ? '/' + path : path
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
export function ensureWatchedFile(
|
|
419
|
-
watcher: FSWatcher,
|
|
420
|
-
file: string | null,
|
|
421
|
-
root: string
|
|
422
|
-
): void {
|
|
423
|
-
if (
|
|
424
|
-
file &&
|
|
425
|
-
// only need to watch if out of root
|
|
426
|
-
!file.startsWith(root + '/') &&
|
|
427
|
-
// some rollup plugins use null bytes for private resolved Ids
|
|
428
|
-
!file.includes('\0') &&
|
|
429
|
-
fs.existsSync(file)
|
|
430
|
-
) {
|
|
431
|
-
// resolve file to normalized system path
|
|
432
|
-
watcher.add(path.resolve(file))
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
interface ImageCandidate {
|
|
437
|
-
url: string
|
|
438
|
-
descriptor: string
|
|
439
|
-
}
|
|
440
|
-
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g
|
|
441
|
-
export async function processSrcSet(
|
|
442
|
-
srcs: string,
|
|
443
|
-
replacer: (arg: ImageCandidate) => Promise<string>
|
|
444
|
-
): Promise<string> {
|
|
445
|
-
const imageCandidates: ImageCandidate[] = srcs
|
|
446
|
-
.split(',')
|
|
447
|
-
.map((s) => {
|
|
448
|
-
const [url, descriptor] = s
|
|
449
|
-
.replace(escapedSpaceCharacters, ' ')
|
|
450
|
-
.trim()
|
|
451
|
-
.split(' ', 2)
|
|
452
|
-
return { url, descriptor }
|
|
453
|
-
})
|
|
454
|
-
.filter(({ url }) => !!url)
|
|
455
|
-
|
|
456
|
-
const ret = await Promise.all(
|
|
457
|
-
imageCandidates.map(async ({ url, descriptor }) => {
|
|
458
|
-
return {
|
|
459
|
-
url: await replacer({ url, descriptor }),
|
|
460
|
-
descriptor
|
|
461
|
-
}
|
|
462
|
-
})
|
|
463
|
-
)
|
|
464
|
-
|
|
465
|
-
const url = ret.reduce((prev, { url, descriptor }, index) => {
|
|
466
|
-
descriptor = descriptor || ''
|
|
467
|
-
return (prev +=
|
|
468
|
-
url + ` ${descriptor}${index === ret.length - 1 ? '' : ', '}`)
|
|
469
|
-
}, '')
|
|
470
|
-
|
|
471
|
-
return url
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
// based on https://github.com/sveltejs/svelte/blob/abf11bb02b2afbd3e4cac509a0f70e318c306364/src/compiler/utils/mapped_code.ts#L221
|
|
475
|
-
const nullSourceMap: RawSourceMap = {
|
|
476
|
-
names: [],
|
|
477
|
-
sources: [],
|
|
478
|
-
mappings: '',
|
|
479
|
-
version: 3
|
|
480
|
-
}
|
|
481
|
-
export function combineSourcemaps(
|
|
482
|
-
filename: string,
|
|
483
|
-
sourcemapList: Array<DecodedSourceMap | RawSourceMap>
|
|
484
|
-
): RawSourceMap {
|
|
485
|
-
if (
|
|
486
|
-
sourcemapList.length === 0 ||
|
|
487
|
-
sourcemapList.every((m) => m.sources.length === 0)
|
|
488
|
-
) {
|
|
489
|
-
return { ...nullSourceMap }
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
// We don't declare type here so we can convert/fake/map as RawSourceMap
|
|
493
|
-
let map //: SourceMap
|
|
494
|
-
let mapIndex = 1
|
|
495
|
-
const useArrayInterface =
|
|
496
|
-
sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === undefined
|
|
497
|
-
if (useArrayInterface) {
|
|
498
|
-
map = remapping(sourcemapList, () => null, true)
|
|
499
|
-
} else {
|
|
500
|
-
map = remapping(
|
|
501
|
-
sourcemapList[0],
|
|
502
|
-
function loader(sourcefile) {
|
|
503
|
-
if (sourcefile === filename && sourcemapList[mapIndex]) {
|
|
504
|
-
return sourcemapList[mapIndex++]
|
|
505
|
-
} else {
|
|
506
|
-
return { ...nullSourceMap }
|
|
507
|
-
}
|
|
508
|
-
},
|
|
509
|
-
true
|
|
510
|
-
)
|
|
511
|
-
}
|
|
512
|
-
if (!map.file) {
|
|
513
|
-
delete map.file
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
return map as RawSourceMap
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
export function unique<T>(arr: T[]): T[] {
|
|
520
|
-
return Array.from(new Set(arr))
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
export interface Hostname {
|
|
524
|
-
// undefined sets the default behaviour of server.listen
|
|
525
|
-
host: string | undefined
|
|
526
|
-
// resolve to localhost when possible
|
|
527
|
-
name: string
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
export function resolveHostname(
|
|
531
|
-
optionsHost: string | boolean | undefined
|
|
532
|
-
): Hostname {
|
|
533
|
-
let host: string | undefined
|
|
534
|
-
if (
|
|
535
|
-
optionsHost === undefined ||
|
|
536
|
-
optionsHost === false ||
|
|
537
|
-
optionsHost === 'localhost'
|
|
538
|
-
) {
|
|
539
|
-
// Use a secure default
|
|
540
|
-
host = '127.0.0.1'
|
|
541
|
-
} else if (optionsHost === true) {
|
|
542
|
-
// If passed --host in the CLI without arguments
|
|
543
|
-
host = undefined // undefined typically means 0.0.0.0 or :: (listen on all IPs)
|
|
544
|
-
} else {
|
|
545
|
-
host = optionsHost
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
// Set host name to localhost when possible, unless the user explicitly asked for '127.0.0.1'
|
|
549
|
-
const name =
|
|
550
|
-
(optionsHost !== '127.0.0.1' && host === '127.0.0.1') ||
|
|
551
|
-
host === '0.0.0.0' ||
|
|
552
|
-
host === '::' ||
|
|
553
|
-
host === undefined
|
|
554
|
-
? 'localhost'
|
|
555
|
-
: host
|
|
556
|
-
|
|
557
|
-
return { host, name }
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
export function arraify<T>(target: T | T[]): T[] {
|
|
561
|
-
return Array.isArray(target) ? target : [target]
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
|
|
565
|
-
export const singlelineCommentsRE = /\/\/.*/g
|