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
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// This is based on @rollup/plugin-data-uri
|
|
2
|
-
// MIT Licensed https://github.com/rollup/plugins/blob/master/LICENSE
|
|
3
|
-
// ref https://github.com/vitejs/vite/issues/1428#issuecomment-757033808
|
|
4
|
-
import { Plugin } from '../plugin'
|
|
5
|
-
import { URL } from 'url'
|
|
6
|
-
|
|
7
|
-
const dataUriRE = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/
|
|
8
|
-
|
|
9
|
-
const dataUriPrefix = `/@data-uri/`
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Build only, since importing from a data URI works natively.
|
|
13
|
-
*/
|
|
14
|
-
export function dataURIPlugin(): Plugin {
|
|
15
|
-
let resolved: {
|
|
16
|
-
[key: string]: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
name: 'vite:data-uri',
|
|
21
|
-
|
|
22
|
-
buildStart() {
|
|
23
|
-
resolved = {}
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
resolveId(id) {
|
|
27
|
-
if (!dataUriRE.test(id)) {
|
|
28
|
-
return null
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const uri = new URL(id)
|
|
32
|
-
if (uri.protocol !== 'data:') {
|
|
33
|
-
return null
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const match = uri.pathname.match(dataUriRE)
|
|
37
|
-
if (!match) {
|
|
38
|
-
return null
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const [, mime, format, data] = match
|
|
42
|
-
if (mime !== 'text/javascript') {
|
|
43
|
-
throw new Error(
|
|
44
|
-
`data URI with non-JavaScript mime type is not supported.`
|
|
45
|
-
)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// decode data
|
|
49
|
-
const base64 = format && /base64/i.test(format.substring(1))
|
|
50
|
-
const content = base64
|
|
51
|
-
? Buffer.from(data, 'base64').toString('utf-8')
|
|
52
|
-
: data
|
|
53
|
-
resolved[id] = content
|
|
54
|
-
return dataUriPrefix + id
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
load(id) {
|
|
58
|
-
if (id.startsWith(dataUriPrefix)) {
|
|
59
|
-
id = id.slice(dataUriPrefix.length)
|
|
60
|
-
return resolved[id] || null
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import MagicString from 'magic-string'
|
|
2
|
-
import { TransformResult } from 'rollup'
|
|
3
|
-
import { ResolvedConfig } from '../config'
|
|
4
|
-
import { Plugin } from '../plugin'
|
|
5
|
-
import { isCSSRequest } from './css'
|
|
6
|
-
|
|
7
|
-
export function definePlugin(config: ResolvedConfig): Plugin {
|
|
8
|
-
const isBuild = config.command === 'build'
|
|
9
|
-
|
|
10
|
-
const userDefine: Record<string, string> = {}
|
|
11
|
-
for (const key in config.define) {
|
|
12
|
-
const val = config.define[key]
|
|
13
|
-
userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// during dev, import.meta properties are handled by importAnalysis plugin
|
|
17
|
-
const importMetaKeys: Record<string, string> = {}
|
|
18
|
-
if (isBuild) {
|
|
19
|
-
const env: Record<string, any> = {
|
|
20
|
-
...config.env,
|
|
21
|
-
SSR: !!config.build.ssr
|
|
22
|
-
}
|
|
23
|
-
for (const key in env) {
|
|
24
|
-
importMetaKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
|
|
25
|
-
}
|
|
26
|
-
Object.assign(importMetaKeys, {
|
|
27
|
-
'import.meta.env.': `({}).`,
|
|
28
|
-
'import.meta.env': JSON.stringify(config.env),
|
|
29
|
-
'import.meta.hot': `false`
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const replacements: Record<string, string | undefined> = {
|
|
34
|
-
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode),
|
|
35
|
-
'global.process.env.NODE_ENV': JSON.stringify(
|
|
36
|
-
process.env.NODE_ENV || config.mode
|
|
37
|
-
),
|
|
38
|
-
'globalThis.process.env.NODE_ENV': JSON.stringify(
|
|
39
|
-
process.env.NODE_ENV || config.mode
|
|
40
|
-
),
|
|
41
|
-
...userDefine,
|
|
42
|
-
...importMetaKeys,
|
|
43
|
-
'process.env.': `({}).`,
|
|
44
|
-
'global.process.env.': `({}).`,
|
|
45
|
-
'globalThis.process.env.': `({}).`
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const pattern = new RegExp(
|
|
49
|
-
// Do not allow preceding '.', but do allow preceding '...' for spread operations
|
|
50
|
-
'(?<!(?<!\\.\\.)\\.)\\b(' +
|
|
51
|
-
Object.keys(replacements)
|
|
52
|
-
.map((str) => {
|
|
53
|
-
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
|
|
54
|
-
})
|
|
55
|
-
.join('|') +
|
|
56
|
-
')\\b',
|
|
57
|
-
'g'
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
name: 'vite:define',
|
|
62
|
-
transform(code, id, ssr) {
|
|
63
|
-
if (!ssr && !isBuild) {
|
|
64
|
-
// for dev we inject actual global defines in the vite client to
|
|
65
|
-
// avoid the transform cost.
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (
|
|
70
|
-
// exclude css and static assets for performance
|
|
71
|
-
isCSSRequest(id) ||
|
|
72
|
-
config.assetsInclude(id)
|
|
73
|
-
) {
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (ssr && !isBuild) {
|
|
78
|
-
// ssr + dev, simple replace
|
|
79
|
-
return code.replace(pattern, (_, match) => {
|
|
80
|
-
return '' + replacements[match]
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const s = new MagicString(code)
|
|
85
|
-
let hasReplaced = false
|
|
86
|
-
let match: RegExpExecArray | null
|
|
87
|
-
|
|
88
|
-
while ((match = pattern.exec(code))) {
|
|
89
|
-
hasReplaced = true
|
|
90
|
-
const start = match.index
|
|
91
|
-
const end = start + match[0].length
|
|
92
|
-
const replacement = '' + replacements[match[1]]
|
|
93
|
-
s.overwrite(start, end, replacement)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!hasReplaced) {
|
|
97
|
-
return null
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const result: TransformResult = { code: s.toString() }
|
|
101
|
-
if (config.build.sourcemap) {
|
|
102
|
-
result.map = s.generateMap({ hires: true })
|
|
103
|
-
}
|
|
104
|
-
return result
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import chalk from 'chalk'
|
|
3
|
-
import { Plugin } from '../plugin'
|
|
4
|
-
import {
|
|
5
|
-
transform,
|
|
6
|
-
Message,
|
|
7
|
-
Loader,
|
|
8
|
-
TransformOptions,
|
|
9
|
-
TransformResult
|
|
10
|
-
} from 'esbuild'
|
|
11
|
-
import {
|
|
12
|
-
cleanUrl,
|
|
13
|
-
createDebugger,
|
|
14
|
-
ensureWatchedFile,
|
|
15
|
-
generateCodeFrame
|
|
16
|
-
} from '../utils'
|
|
17
|
-
import { RawSourceMap } from '@ampproject/remapping/dist/types/types'
|
|
18
|
-
import { SourceMap } from 'rollup'
|
|
19
|
-
import { ResolvedConfig, ViteDevServer } from '..'
|
|
20
|
-
import { createFilter } from '@rollup/pluginutils'
|
|
21
|
-
import { combineSourcemaps } from '../utils'
|
|
22
|
-
import { parse, TSConfckParseError, TSConfckParseResult } from 'tsconfck'
|
|
23
|
-
|
|
24
|
-
const debug = createDebugger('vite:esbuild')
|
|
25
|
-
|
|
26
|
-
let server: ViteDevServer
|
|
27
|
-
|
|
28
|
-
export interface ESBuildOptions extends TransformOptions {
|
|
29
|
-
include?: string | RegExp | string[] | RegExp[]
|
|
30
|
-
exclude?: string | RegExp | string[] | RegExp[]
|
|
31
|
-
jsxInject?: string
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type ESBuildTransformResult = Omit<TransformResult, 'map'> & {
|
|
35
|
-
map: SourceMap
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type TSConfigJSON = {
|
|
39
|
-
extends?: string
|
|
40
|
-
compilerOptions?: {
|
|
41
|
-
target?: string
|
|
42
|
-
jsxFactory?: string
|
|
43
|
-
jsxFragmentFactory?: string
|
|
44
|
-
useDefineForClassFields?: boolean
|
|
45
|
-
importsNotUsedAsValues?: 'remove' | 'preserve' | 'error'
|
|
46
|
-
}
|
|
47
|
-
[key: string]: any
|
|
48
|
-
}
|
|
49
|
-
type TSCompilerOptions = NonNullable<TSConfigJSON['compilerOptions']>
|
|
50
|
-
|
|
51
|
-
export async function transformWithEsbuild(
|
|
52
|
-
code: string,
|
|
53
|
-
filename: string,
|
|
54
|
-
options?: TransformOptions,
|
|
55
|
-
inMap?: object
|
|
56
|
-
): Promise<ESBuildTransformResult> {
|
|
57
|
-
let loader = options?.loader
|
|
58
|
-
|
|
59
|
-
if (!loader) {
|
|
60
|
-
// if the id ends with a valid ext, use it (e.g. vue blocks)
|
|
61
|
-
// otherwise, cleanup the query before checking the ext
|
|
62
|
-
const ext = path
|
|
63
|
-
.extname(/\.\w+$/.test(filename) ? filename : cleanUrl(filename))
|
|
64
|
-
.slice(1)
|
|
65
|
-
|
|
66
|
-
if (ext === 'cjs' || ext === 'mjs') {
|
|
67
|
-
loader = 'js'
|
|
68
|
-
} else {
|
|
69
|
-
loader = ext as Loader
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
let tsconfigRaw = options?.tsconfigRaw
|
|
74
|
-
|
|
75
|
-
// if options provide tsconfigraw in string, it takes highest precedence
|
|
76
|
-
if (typeof tsconfigRaw !== 'string') {
|
|
77
|
-
// these fields would affect the compilation result
|
|
78
|
-
// https://esbuild.github.io/content-types/#tsconfig-json
|
|
79
|
-
const meaningfulFields: Array<keyof TSCompilerOptions> = [
|
|
80
|
-
'jsxFactory',
|
|
81
|
-
'jsxFragmentFactory',
|
|
82
|
-
'useDefineForClassFields',
|
|
83
|
-
'importsNotUsedAsValues'
|
|
84
|
-
]
|
|
85
|
-
const compilerOptionsForFile: TSCompilerOptions = {}
|
|
86
|
-
if (loader === 'ts' || loader === 'tsx') {
|
|
87
|
-
const loadedTsconfig = await loadTsconfigJsonForFile(filename)
|
|
88
|
-
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
|
|
89
|
-
|
|
90
|
-
for (const field of meaningfulFields) {
|
|
91
|
-
if (field in loadedCompilerOptions) {
|
|
92
|
-
// @ts-ignore TypeScript can't tell they are of the same type
|
|
93
|
-
compilerOptionsForFile[field] = loadedCompilerOptions[field]
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// align with TypeScript 4.3
|
|
98
|
-
// https://github.com/microsoft/TypeScript/pull/42663
|
|
99
|
-
if (loadedCompilerOptions.target?.toLowerCase() === 'esnext') {
|
|
100
|
-
compilerOptionsForFile.useDefineForClassFields =
|
|
101
|
-
loadedCompilerOptions.useDefineForClassFields ?? true
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
tsconfigRaw = {
|
|
106
|
-
...tsconfigRaw,
|
|
107
|
-
compilerOptions: {
|
|
108
|
-
...compilerOptionsForFile,
|
|
109
|
-
...tsconfigRaw?.compilerOptions
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const resolvedOptions = {
|
|
115
|
-
sourcemap: true,
|
|
116
|
-
// ensure source file name contains full query
|
|
117
|
-
sourcefile: filename,
|
|
118
|
-
...options,
|
|
119
|
-
loader,
|
|
120
|
-
tsconfigRaw
|
|
121
|
-
} as ESBuildOptions
|
|
122
|
-
|
|
123
|
-
delete resolvedOptions.include
|
|
124
|
-
delete resolvedOptions.exclude
|
|
125
|
-
delete resolvedOptions.jsxInject
|
|
126
|
-
|
|
127
|
-
try {
|
|
128
|
-
const result = await transform(code, resolvedOptions)
|
|
129
|
-
if (inMap && resolvedOptions.sourcemap) {
|
|
130
|
-
const nextMap = JSON.parse(result.map)
|
|
131
|
-
nextMap.sourcesContent = []
|
|
132
|
-
return {
|
|
133
|
-
...result,
|
|
134
|
-
map: combineSourcemaps(filename, [
|
|
135
|
-
nextMap as RawSourceMap,
|
|
136
|
-
inMap as RawSourceMap
|
|
137
|
-
]) as SourceMap
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
return {
|
|
141
|
-
...result,
|
|
142
|
-
map: resolvedOptions.sourcemap
|
|
143
|
-
? JSON.parse(result.map)
|
|
144
|
-
: { mappings: '' }
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
} catch (e) {
|
|
148
|
-
debug(`esbuild error with options used: `, resolvedOptions)
|
|
149
|
-
// patch error information
|
|
150
|
-
if (e.errors) {
|
|
151
|
-
e.frame = ''
|
|
152
|
-
e.errors.forEach((m: Message) => {
|
|
153
|
-
e.frame += `\n` + prettifyMessage(m, code)
|
|
154
|
-
})
|
|
155
|
-
e.loc = e.errors[0].location
|
|
156
|
-
}
|
|
157
|
-
throw e
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
|
|
162
|
-
const filter = createFilter(
|
|
163
|
-
options.include || /\.(tsx?|jsx)$/,
|
|
164
|
-
options.exclude || /\.js$/
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
return {
|
|
168
|
-
name: 'vite:esbuild',
|
|
169
|
-
configureServer(_server) {
|
|
170
|
-
server = _server
|
|
171
|
-
server.watcher
|
|
172
|
-
.on('add', reloadOnTsconfigChange)
|
|
173
|
-
.on('change', reloadOnTsconfigChange)
|
|
174
|
-
.on('unlink', reloadOnTsconfigChange)
|
|
175
|
-
},
|
|
176
|
-
async transform(code, id) {
|
|
177
|
-
if (filter(id) || filter(cleanUrl(id))) {
|
|
178
|
-
const result = await transformWithEsbuild(code, id, options)
|
|
179
|
-
if (result.warnings.length) {
|
|
180
|
-
result.warnings.forEach((m) => {
|
|
181
|
-
this.warn(prettifyMessage(m, code))
|
|
182
|
-
})
|
|
183
|
-
}
|
|
184
|
-
if (options.jsxInject && /\.(?:j|t)sx\b/.test(id)) {
|
|
185
|
-
result.code = options.jsxInject + ';' + result.code
|
|
186
|
-
}
|
|
187
|
-
return {
|
|
188
|
-
code: result.code,
|
|
189
|
-
map: result.map
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
|
|
197
|
-
return {
|
|
198
|
-
name: 'vite:esbuild-transpile',
|
|
199
|
-
async renderChunk(code, chunk, opts) {
|
|
200
|
-
// @ts-ignore injected by @vitejs/plugin-legacy
|
|
201
|
-
if (opts.__vite_skip_esbuild__) {
|
|
202
|
-
return null
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const target = config.build.target
|
|
206
|
-
const minify = config.build.minify === 'esbuild'
|
|
207
|
-
if ((!target || target === 'esnext') && !minify) {
|
|
208
|
-
return null
|
|
209
|
-
}
|
|
210
|
-
return transformWithEsbuild(code, chunk.fileName, {
|
|
211
|
-
target: target || undefined,
|
|
212
|
-
minify
|
|
213
|
-
})
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function prettifyMessage(m: Message, code: string): string {
|
|
219
|
-
let res = chalk.yellow(m.text)
|
|
220
|
-
if (m.location) {
|
|
221
|
-
const lines = code.split(/\r?\n/g)
|
|
222
|
-
const line = Number(m.location.line)
|
|
223
|
-
const column = Number(m.location.column)
|
|
224
|
-
const offset =
|
|
225
|
-
lines
|
|
226
|
-
.slice(0, line - 1)
|
|
227
|
-
.map((l) => l.length)
|
|
228
|
-
.reduce((total, l) => total + l + 1, 0) + column
|
|
229
|
-
res += `\n` + generateCodeFrame(code, offset, offset + 1)
|
|
230
|
-
}
|
|
231
|
-
return res + `\n`
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const tsconfigCache = new Map<string, TSConfckParseResult>()
|
|
235
|
-
async function loadTsconfigJsonForFile(
|
|
236
|
-
filename: string
|
|
237
|
-
): Promise<TSConfigJSON> {
|
|
238
|
-
try {
|
|
239
|
-
const result = await parse(filename, {
|
|
240
|
-
cache: tsconfigCache,
|
|
241
|
-
resolveWithEmptyIfConfigNotFound: true
|
|
242
|
-
})
|
|
243
|
-
// tsconfig could be out of root, make sure it is watched on dev
|
|
244
|
-
if (server && result.tsconfigFile !== 'no_tsconfig_file_found') {
|
|
245
|
-
ensureWatchedFile(server.watcher, result.tsconfigFile, server.config.root)
|
|
246
|
-
}
|
|
247
|
-
return result.tsconfig
|
|
248
|
-
} catch (e) {
|
|
249
|
-
if (e instanceof TSConfckParseError) {
|
|
250
|
-
// tsconfig could be out of root, make sure it is watched on dev
|
|
251
|
-
if (server && e.tsconfigFile) {
|
|
252
|
-
ensureWatchedFile(server.watcher, e.tsconfigFile, server.config.root)
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
throw e
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function reloadOnTsconfigChange(changedFile: string) {
|
|
260
|
-
// any tsconfig.json that's added in the workspace could be closer to a code file than a previously cached one
|
|
261
|
-
// any json file in the tsconfig cache could have been used to compile ts
|
|
262
|
-
if (
|
|
263
|
-
path.basename(changedFile) === 'tsconfig.json' ||
|
|
264
|
-
(changedFile.endsWith('.json') && tsconfigCache.has(changedFile))
|
|
265
|
-
) {
|
|
266
|
-
server.config.logger.info(
|
|
267
|
-
`changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure typescript is compiled with updated config values.`,
|
|
268
|
-
{ clear: server.config.clearScreen, timestamp: true }
|
|
269
|
-
)
|
|
270
|
-
// clear tsconfig cache so that recompile works with up2date configs
|
|
271
|
-
tsconfigCache.clear()
|
|
272
|
-
// clear module graph to remove code compiled with outdated config
|
|
273
|
-
server.moduleGraph.invalidateAll()
|
|
274
|
-
// force full reload
|
|
275
|
-
server.ws.send({
|
|
276
|
-
type: 'full-reload',
|
|
277
|
-
path: '*'
|
|
278
|
-
})
|
|
279
|
-
}
|
|
280
|
-
}
|