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/constants.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
|
|
3
|
-
export const DEFAULT_MAIN_FIELDS = [
|
|
4
|
-
'module',
|
|
5
|
-
'jsnext:main', // moment still uses this...
|
|
6
|
-
'jsnext'
|
|
7
|
-
]
|
|
8
|
-
|
|
9
|
-
export const DEFAULT_EXTENSIONS = [
|
|
10
|
-
'.mjs',
|
|
11
|
-
'.js',
|
|
12
|
-
'.ts',
|
|
13
|
-
'.jsx',
|
|
14
|
-
'.tsx',
|
|
15
|
-
'.json'
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
export const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/
|
|
19
|
-
|
|
20
|
-
export const OPTIMIZABLE_ENTRY_RE = /\.(?:m?js|ts)$/
|
|
21
|
-
|
|
22
|
-
export const SPECIAL_QUERY_RE = /[\?&](?:worker|sharedworker|raw|url)\b/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Prefix for resolved fs paths, since windows paths may not be valid as URLs.
|
|
26
|
-
*/
|
|
27
|
-
export const FS_PREFIX = `/@fs/`
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Prefix for resolved Ids that are not valid browser import specifiers
|
|
31
|
-
*/
|
|
32
|
-
export const VALID_ID_PREFIX = `/@id/`
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Some Rollup plugins use ids that starts with the null byte \0 to avoid
|
|
36
|
-
* collisions, but it is not permitted in import URLs so we have to replace
|
|
37
|
-
* them.
|
|
38
|
-
*/
|
|
39
|
-
export const NULL_BYTE_PLACEHOLDER = `__x00__`
|
|
40
|
-
|
|
41
|
-
export const CLIENT_PUBLIC_PATH = `/@vite/client`
|
|
42
|
-
export const ENV_PUBLIC_PATH = `/@vite/env`
|
|
43
|
-
// eslint-disable-next-line node/no-missing-require
|
|
44
|
-
export const CLIENT_ENTRY = require.resolve('vite/dist/client/client.mjs')
|
|
45
|
-
// eslint-disable-next-line node/no-missing-require
|
|
46
|
-
export const ENV_ENTRY = require.resolve('vite/dist/client/env.mjs')
|
|
47
|
-
export const CLIENT_DIR = path.dirname(CLIENT_ENTRY)
|
|
48
|
-
|
|
49
|
-
// ** READ THIS ** before editing `KNOWN_ASSET_TYPES`.
|
|
50
|
-
// If you add an asset to `KNOWN_ASSET_TYPES`, make sure to also add it
|
|
51
|
-
// to the TypeScript declaration file `packages/vite/client.d.ts`.
|
|
52
|
-
export const KNOWN_ASSET_TYPES = [
|
|
53
|
-
// images
|
|
54
|
-
'png',
|
|
55
|
-
'jpe?g',
|
|
56
|
-
'gif',
|
|
57
|
-
'svg',
|
|
58
|
-
'ico',
|
|
59
|
-
'webp',
|
|
60
|
-
'avif',
|
|
61
|
-
|
|
62
|
-
// media
|
|
63
|
-
'mp4',
|
|
64
|
-
'webm',
|
|
65
|
-
'ogg',
|
|
66
|
-
'mp3',
|
|
67
|
-
'wav',
|
|
68
|
-
'flac',
|
|
69
|
-
'aac',
|
|
70
|
-
|
|
71
|
-
// fonts
|
|
72
|
-
'woff2?',
|
|
73
|
-
'eot',
|
|
74
|
-
'ttf',
|
|
75
|
-
'otf',
|
|
76
|
-
|
|
77
|
-
// other
|
|
78
|
-
'wasm',
|
|
79
|
-
'webmanifest',
|
|
80
|
-
'pdf'
|
|
81
|
-
]
|
|
82
|
-
|
|
83
|
-
export const DEFAULT_ASSETS_RE = new RegExp(
|
|
84
|
-
`\\.(` + KNOWN_ASSET_TYPES.join('|') + `)(\\?.*)?$`
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
export const DEP_VERSION_RE = /[\?&](v=[\w\.-]+)\b/
|
package/src/node/importGlob.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import glob from 'fast-glob'
|
|
3
|
-
import {
|
|
4
|
-
isModernFlag,
|
|
5
|
-
preloadMethod,
|
|
6
|
-
preloadMarker
|
|
7
|
-
} from './plugins/importAnalysisBuild'
|
|
8
|
-
import { cleanUrl } from './utils'
|
|
9
|
-
import { RollupError } from 'rollup'
|
|
10
|
-
|
|
11
|
-
export async function transformImportGlob(
|
|
12
|
-
source: string,
|
|
13
|
-
pos: number,
|
|
14
|
-
importer: string,
|
|
15
|
-
importIndex: number,
|
|
16
|
-
root: string,
|
|
17
|
-
normalizeUrl?: (url: string, pos: number) => Promise<[string, string]>,
|
|
18
|
-
preload = true
|
|
19
|
-
): Promise<{
|
|
20
|
-
importsString: string
|
|
21
|
-
imports: string[]
|
|
22
|
-
exp: string
|
|
23
|
-
endIndex: number
|
|
24
|
-
isEager: boolean
|
|
25
|
-
pattern: string
|
|
26
|
-
base: string
|
|
27
|
-
}> {
|
|
28
|
-
const isEager = source.slice(pos, pos + 21) === 'import.meta.globEager'
|
|
29
|
-
const isEagerDefault =
|
|
30
|
-
isEager && source.slice(pos + 21, pos + 28) === 'Default'
|
|
31
|
-
|
|
32
|
-
const err = (msg: string) => {
|
|
33
|
-
const e = new Error(`Invalid glob import syntax: ${msg}`)
|
|
34
|
-
;(e as any).pos = pos
|
|
35
|
-
return e
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
importer = cleanUrl(importer)
|
|
39
|
-
const importerBasename = path.basename(importer)
|
|
40
|
-
|
|
41
|
-
let [pattern, endIndex] = lexGlobPattern(source, pos)
|
|
42
|
-
if (!pattern.startsWith('.') && !pattern.startsWith('/')) {
|
|
43
|
-
throw err(`pattern must start with "." or "/" (relative to project root)`)
|
|
44
|
-
}
|
|
45
|
-
let base: string
|
|
46
|
-
let parentDepth = 0
|
|
47
|
-
const isAbsolute = pattern.startsWith('/')
|
|
48
|
-
if (isAbsolute) {
|
|
49
|
-
base = path.resolve(root)
|
|
50
|
-
pattern = pattern.slice(1)
|
|
51
|
-
} else {
|
|
52
|
-
base = path.dirname(importer)
|
|
53
|
-
while (pattern.startsWith('../')) {
|
|
54
|
-
pattern = pattern.slice(3)
|
|
55
|
-
base = path.resolve(base, '../')
|
|
56
|
-
parentDepth++
|
|
57
|
-
}
|
|
58
|
-
if (pattern.startsWith('./')) {
|
|
59
|
-
pattern = pattern.slice(2)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
const files = glob.sync(pattern, {
|
|
63
|
-
cwd: base,
|
|
64
|
-
ignore: ['**/node_modules/**']
|
|
65
|
-
})
|
|
66
|
-
const imports: string[] = []
|
|
67
|
-
let importsString = ``
|
|
68
|
-
let entries = ``
|
|
69
|
-
for (let i = 0; i < files.length; i++) {
|
|
70
|
-
// skip importer itself
|
|
71
|
-
if (files[i] === importerBasename) continue
|
|
72
|
-
const file = isAbsolute
|
|
73
|
-
? `/${files[i]}`
|
|
74
|
-
: parentDepth
|
|
75
|
-
? `${'../'.repeat(parentDepth)}${files[i]}`
|
|
76
|
-
: `./${files[i]}`
|
|
77
|
-
let importee = file
|
|
78
|
-
if (normalizeUrl) {
|
|
79
|
-
;[importee] = await normalizeUrl(file, pos)
|
|
80
|
-
}
|
|
81
|
-
imports.push(importee)
|
|
82
|
-
const identifier = `__glob_${importIndex}_${i}`
|
|
83
|
-
if (isEager) {
|
|
84
|
-
importsString += `import ${
|
|
85
|
-
isEagerDefault ? `` : `* as `
|
|
86
|
-
}${identifier} from ${JSON.stringify(importee)};`
|
|
87
|
-
entries += ` ${JSON.stringify(file)}: ${identifier},`
|
|
88
|
-
} else {
|
|
89
|
-
let imp = `import(${JSON.stringify(importee)})`
|
|
90
|
-
if (!normalizeUrl && preload) {
|
|
91
|
-
imp =
|
|
92
|
-
`(${isModernFlag}` +
|
|
93
|
-
`? ${preloadMethod}(()=>${imp},"${preloadMarker}")` +
|
|
94
|
-
`: ${imp})`
|
|
95
|
-
}
|
|
96
|
-
entries += ` ${JSON.stringify(file)}: () => ${imp},`
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
imports,
|
|
102
|
-
importsString,
|
|
103
|
-
exp: `{${entries}}`,
|
|
104
|
-
endIndex,
|
|
105
|
-
isEager,
|
|
106
|
-
pattern,
|
|
107
|
-
base
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const enum LexerState {
|
|
112
|
-
inCall,
|
|
113
|
-
inSingleQuoteString,
|
|
114
|
-
inDoubleQuoteString,
|
|
115
|
-
inTemplateString
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function lexGlobPattern(code: string, pos: number): [string, number] {
|
|
119
|
-
let state = LexerState.inCall
|
|
120
|
-
let pattern = ''
|
|
121
|
-
|
|
122
|
-
let i = code.indexOf(`(`, pos) + 1
|
|
123
|
-
outer: for (; i < code.length; i++) {
|
|
124
|
-
const char = code.charAt(i)
|
|
125
|
-
switch (state) {
|
|
126
|
-
case LexerState.inCall:
|
|
127
|
-
if (char === `'`) {
|
|
128
|
-
state = LexerState.inSingleQuoteString
|
|
129
|
-
} else if (char === `"`) {
|
|
130
|
-
state = LexerState.inDoubleQuoteString
|
|
131
|
-
} else if (char === '`') {
|
|
132
|
-
state = LexerState.inTemplateString
|
|
133
|
-
} else if (/\s/.test(char)) {
|
|
134
|
-
continue
|
|
135
|
-
} else {
|
|
136
|
-
error(i)
|
|
137
|
-
}
|
|
138
|
-
break
|
|
139
|
-
case LexerState.inSingleQuoteString:
|
|
140
|
-
if (char === `'`) {
|
|
141
|
-
break outer
|
|
142
|
-
} else {
|
|
143
|
-
pattern += char
|
|
144
|
-
}
|
|
145
|
-
break
|
|
146
|
-
case LexerState.inDoubleQuoteString:
|
|
147
|
-
if (char === `"`) {
|
|
148
|
-
break outer
|
|
149
|
-
} else {
|
|
150
|
-
pattern += char
|
|
151
|
-
}
|
|
152
|
-
break
|
|
153
|
-
case LexerState.inTemplateString:
|
|
154
|
-
if (char === '`') {
|
|
155
|
-
break outer
|
|
156
|
-
} else {
|
|
157
|
-
pattern += char
|
|
158
|
-
}
|
|
159
|
-
break
|
|
160
|
-
default:
|
|
161
|
-
throw new Error('unknown import.meta.glob lexer state')
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return [pattern, code.indexOf(`)`, i) + 1]
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function error(pos: number) {
|
|
168
|
-
const err = new Error(
|
|
169
|
-
`import.meta.glob() can only accept string literals.`
|
|
170
|
-
) as RollupError
|
|
171
|
-
err.pos = pos
|
|
172
|
-
throw err
|
|
173
|
-
}
|
package/src/node/index.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
export * from './config'
|
|
2
|
-
export { createServer } from './server'
|
|
3
|
-
export { build } from './build'
|
|
4
|
-
export { optimizeDeps } from './optimizer'
|
|
5
|
-
export { send } from './server/send'
|
|
6
|
-
export { createLogger } from './logger'
|
|
7
|
-
export { transformWithEsbuild } from './plugins/esbuild'
|
|
8
|
-
export { resolvePackageData, resolvePackageEntry } from './plugins/resolve'
|
|
9
|
-
export { normalizePath } from './utils'
|
|
10
|
-
|
|
11
|
-
// additional types
|
|
12
|
-
export type {
|
|
13
|
-
ViteDevServer,
|
|
14
|
-
ServerOptions,
|
|
15
|
-
CorsOptions,
|
|
16
|
-
FileSystemServeOptions,
|
|
17
|
-
CorsOrigin,
|
|
18
|
-
ServerHook,
|
|
19
|
-
ResolvedServerOptions
|
|
20
|
-
} from './server'
|
|
21
|
-
export type {
|
|
22
|
-
BuildOptions,
|
|
23
|
-
LibraryOptions,
|
|
24
|
-
LibraryFormats,
|
|
25
|
-
ResolvedBuildOptions
|
|
26
|
-
} from './build'
|
|
27
|
-
export type {
|
|
28
|
-
DepOptimizationMetadata,
|
|
29
|
-
DepOptimizationOptions
|
|
30
|
-
} from './optimizer'
|
|
31
|
-
export type { Plugin } from './plugin'
|
|
32
|
-
export type {
|
|
33
|
-
Logger,
|
|
34
|
-
LogOptions,
|
|
35
|
-
LogErrorOptions,
|
|
36
|
-
LogLevel,
|
|
37
|
-
LogType,
|
|
38
|
-
LoggerOptions
|
|
39
|
-
} from './logger'
|
|
40
|
-
export type {
|
|
41
|
-
AliasOptions,
|
|
42
|
-
ResolverFunction,
|
|
43
|
-
ResolverObject,
|
|
44
|
-
Alias
|
|
45
|
-
} from 'types/alias'
|
|
46
|
-
export type {
|
|
47
|
-
IndexHtmlTransform,
|
|
48
|
-
IndexHtmlTransformHook,
|
|
49
|
-
IndexHtmlTransformContext,
|
|
50
|
-
IndexHtmlTransformResult,
|
|
51
|
-
HtmlTagDescriptor
|
|
52
|
-
} from './plugins/html'
|
|
53
|
-
export type { CSSOptions, CSSModulesOptions } from './plugins/css'
|
|
54
|
-
export type { JsonOptions } from './plugins/json'
|
|
55
|
-
export type { TransformOptions as EsbuildTransformOptions } from 'esbuild'
|
|
56
|
-
export type { ESBuildOptions, ESBuildTransformResult } from './plugins/esbuild'
|
|
57
|
-
export type { Manifest, ManifestChunk } from './plugins/manifest'
|
|
58
|
-
export type {
|
|
59
|
-
PackageData,
|
|
60
|
-
ResolveOptions,
|
|
61
|
-
InternalResolveOptions
|
|
62
|
-
} from './plugins/resolve'
|
|
63
|
-
export type { WebSocketServer } from './server/ws'
|
|
64
|
-
export type { PluginContainer } from './server/pluginContainer'
|
|
65
|
-
export type { ModuleGraph, ModuleNode } from './server/moduleGraph'
|
|
66
|
-
export type { ProxyOptions } from './server/middlewares/proxy'
|
|
67
|
-
export type {
|
|
68
|
-
TransformOptions,
|
|
69
|
-
TransformResult
|
|
70
|
-
} from './server/transformRequest'
|
|
71
|
-
export type { HmrOptions, HmrContext } from './server/hmr'
|
|
72
|
-
export type {
|
|
73
|
-
HMRPayload,
|
|
74
|
-
ConnectedPayload,
|
|
75
|
-
UpdatePayload,
|
|
76
|
-
Update,
|
|
77
|
-
FullReloadPayload,
|
|
78
|
-
CustomPayload,
|
|
79
|
-
PrunePayload,
|
|
80
|
-
ErrorPayload
|
|
81
|
-
} from 'types/hmrPayload'
|
|
82
|
-
export type { Connect } from 'types/connect'
|
|
83
|
-
export type { HttpProxy } from 'types/http-proxy'
|
|
84
|
-
export type { FSWatcher, WatchOptions } from 'types/chokidar'
|
|
85
|
-
export type { Terser } from 'types/terser'
|
|
86
|
-
export type { RollupCommonJSOptions } from 'types/commonjs'
|
|
87
|
-
export type { RollupDynamicImportVarsOptions } from 'types/dynamicImportVars'
|
|
88
|
-
export type { Matcher, AnymatchPattern, AnymatchFn } from 'types/anymatch'
|
package/src/node/logger.ts
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
/* eslint no-console: 0 */
|
|
2
|
-
|
|
3
|
-
import chalk from 'chalk'
|
|
4
|
-
import readline from 'readline'
|
|
5
|
-
import os from 'os'
|
|
6
|
-
import { RollupError } from 'rollup'
|
|
7
|
-
import { Hostname } from './utils'
|
|
8
|
-
|
|
9
|
-
export type LogType = 'error' | 'warn' | 'info'
|
|
10
|
-
export type LogLevel = LogType | 'silent'
|
|
11
|
-
export interface Logger {
|
|
12
|
-
info(msg: string, options?: LogOptions): void
|
|
13
|
-
warn(msg: string, options?: LogOptions): void
|
|
14
|
-
warnOnce(msg: string, options?: LogOptions): void
|
|
15
|
-
error(msg: string, options?: LogErrorOptions): void
|
|
16
|
-
clearScreen(type: LogType): void
|
|
17
|
-
hasErrorLogged(error: Error | RollupError): boolean
|
|
18
|
-
hasWarned: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface LogOptions {
|
|
22
|
-
clear?: boolean
|
|
23
|
-
timestamp?: boolean
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface LogErrorOptions extends LogOptions {
|
|
27
|
-
error?: Error | RollupError | null
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const LogLevels: Record<LogLevel, number> = {
|
|
31
|
-
silent: 0,
|
|
32
|
-
error: 1,
|
|
33
|
-
warn: 2,
|
|
34
|
-
info: 3
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let lastType: LogType | undefined
|
|
38
|
-
let lastMsg: string | undefined
|
|
39
|
-
let sameCount = 0
|
|
40
|
-
|
|
41
|
-
function clearScreen() {
|
|
42
|
-
const repeatCount = process.stdout.rows - 2
|
|
43
|
-
const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : ''
|
|
44
|
-
console.log(blank)
|
|
45
|
-
readline.cursorTo(process.stdout, 0, 0)
|
|
46
|
-
readline.clearScreenDown(process.stdout)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface LoggerOptions {
|
|
50
|
-
prefix?: string
|
|
51
|
-
allowClearScreen?: boolean
|
|
52
|
-
customLogger?: Logger
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function createLogger(
|
|
56
|
-
level: LogLevel = 'info',
|
|
57
|
-
options: LoggerOptions = {}
|
|
58
|
-
): Logger {
|
|
59
|
-
if (options.customLogger) {
|
|
60
|
-
return options.customLogger
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const loggedErrors = new WeakSet<Error | RollupError>()
|
|
64
|
-
const { prefix = '[vite]', allowClearScreen = true } = options
|
|
65
|
-
const thresh = LogLevels[level]
|
|
66
|
-
const clear =
|
|
67
|
-
allowClearScreen && process.stdout.isTTY && !process.env.CI
|
|
68
|
-
? clearScreen
|
|
69
|
-
: () => {}
|
|
70
|
-
|
|
71
|
-
function output(type: LogType, msg: string, options: LogErrorOptions = {}) {
|
|
72
|
-
if (thresh >= LogLevels[type]) {
|
|
73
|
-
const method = type === 'info' ? 'log' : type
|
|
74
|
-
const format = () => {
|
|
75
|
-
if (options.timestamp) {
|
|
76
|
-
const tag =
|
|
77
|
-
type === 'info'
|
|
78
|
-
? chalk.cyan.bold(prefix)
|
|
79
|
-
: type === 'warn'
|
|
80
|
-
? chalk.yellow.bold(prefix)
|
|
81
|
-
: chalk.red.bold(prefix)
|
|
82
|
-
return `${chalk.dim(new Date().toLocaleTimeString())} ${tag} ${msg}`
|
|
83
|
-
} else {
|
|
84
|
-
return msg
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (options.error) {
|
|
88
|
-
loggedErrors.add(options.error)
|
|
89
|
-
}
|
|
90
|
-
if (type === lastType && msg === lastMsg) {
|
|
91
|
-
sameCount++
|
|
92
|
-
clear()
|
|
93
|
-
console[method](format(), chalk.yellow(`(x${sameCount + 1})`))
|
|
94
|
-
} else {
|
|
95
|
-
sameCount = 0
|
|
96
|
-
lastMsg = msg
|
|
97
|
-
lastType = type
|
|
98
|
-
if (options.clear) {
|
|
99
|
-
clear()
|
|
100
|
-
}
|
|
101
|
-
console[method](format())
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const warnedMessages = new Set<string>()
|
|
107
|
-
|
|
108
|
-
const logger: Logger = {
|
|
109
|
-
hasWarned: false,
|
|
110
|
-
info(msg, opts) {
|
|
111
|
-
output('info', msg, opts)
|
|
112
|
-
},
|
|
113
|
-
warn(msg, opts) {
|
|
114
|
-
logger.hasWarned = true
|
|
115
|
-
output('warn', msg, opts)
|
|
116
|
-
},
|
|
117
|
-
warnOnce(msg, opts) {
|
|
118
|
-
if (warnedMessages.has(msg)) return
|
|
119
|
-
logger.hasWarned = true
|
|
120
|
-
output('warn', msg, opts)
|
|
121
|
-
warnedMessages.add(msg)
|
|
122
|
-
},
|
|
123
|
-
error(msg, opts) {
|
|
124
|
-
logger.hasWarned = true
|
|
125
|
-
output('error', msg, opts)
|
|
126
|
-
},
|
|
127
|
-
clearScreen(type) {
|
|
128
|
-
if (thresh >= LogLevels[type]) {
|
|
129
|
-
clear()
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
hasErrorLogged(error) {
|
|
133
|
-
return loggedErrors.has(error)
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return logger
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export function printServerUrls(
|
|
141
|
-
hostname: Hostname,
|
|
142
|
-
protocol: string,
|
|
143
|
-
port: number,
|
|
144
|
-
base: string,
|
|
145
|
-
info: Logger['info']
|
|
146
|
-
): void {
|
|
147
|
-
if (hostname.host === '127.0.0.1') {
|
|
148
|
-
const url = `${protocol}://${hostname.name}:${chalk.bold(port)}${base}`
|
|
149
|
-
info(` > Local: ${chalk.cyan(url)}`)
|
|
150
|
-
if (hostname.name !== '127.0.0.1') {
|
|
151
|
-
info(` > Network: ${chalk.dim('use `--host` to expose')}`)
|
|
152
|
-
}
|
|
153
|
-
} else {
|
|
154
|
-
Object.values(os.networkInterfaces())
|
|
155
|
-
.flatMap((nInterface) => nInterface ?? [])
|
|
156
|
-
.filter((detail) => detail.family === 'IPv4')
|
|
157
|
-
.map((detail) => {
|
|
158
|
-
const type = detail.address.includes('127.0.0.1')
|
|
159
|
-
? 'Local: '
|
|
160
|
-
: 'Network: '
|
|
161
|
-
const host = detail.address.replace('127.0.0.1', hostname.name)
|
|
162
|
-
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
|
|
163
|
-
return ` > ${type} ${chalk.cyan(url)}`
|
|
164
|
-
})
|
|
165
|
-
.forEach((msg) => info(msg))
|
|
166
|
-
}
|
|
167
|
-
}
|