forked from vitejs/vite
/
esbuild.ts
367 lines (335 loc) · 10.8 KB
/
esbuild.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import path from 'path'
import colors from 'picocolors'
import type {
Loader,
Message,
TransformOptions,
TransformResult
} from 'esbuild'
import { transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import type { SourceMap } from 'rollup'
import { createFilter } from '@rollup/pluginutils'
import type { TSConfckParseOptions, TSConfckParseResult } from 'tsconfck'
import { TSConfckParseError, findAll, parse } from 'tsconfck'
import {
cleanUrl,
combineSourcemaps,
createDebugger,
ensureWatchedFile,
generateCodeFrame,
toUpperCaseDriveLetter
} from '../utils'
import type { ResolvedConfig, ViteDevServer } from '..'
import type { Plugin } from '../plugin'
import { searchForWorkspaceRoot } from '..'
const debug = createDebugger('vite:esbuild')
const INJECT_HELPERS_IIFE_RE =
/(.*)((?:const|var) [^\s]+=function\([^)]*?\){"use strict";)(.*)/s
const INJECT_HELPERS_UMD_RE =
/(.*)(\(function\([^)]*?\){.+amd.+function\([^)]*?\){"use strict";)(.*)/s
let server: ViteDevServer
export interface ESBuildOptions extends TransformOptions {
include?: string | RegExp | string[] | RegExp[]
exclude?: string | RegExp | string[] | RegExp[]
jsxInject?: string
}
export type ESBuildTransformResult = Omit<TransformResult, 'map'> & {
map: SourceMap
}
type TSConfigJSON = {
extends?: string
compilerOptions?: {
target?: string
jsxFactory?: string
jsxFragmentFactory?: string
useDefineForClassFields?: boolean
importsNotUsedAsValues?: 'remove' | 'preserve' | 'error'
preserveValueImports?: boolean
}
[key: string]: any
}
type TSCompilerOptions = NonNullable<TSConfigJSON['compilerOptions']>
export async function transformWithEsbuild(
code: string,
filename: string,
options?: TransformOptions,
inMap?: object
): Promise<ESBuildTransformResult> {
let loader = options?.loader
if (!loader) {
// if the id ends with a valid ext, use it (e.g. vue blocks)
// otherwise, cleanup the query before checking the ext
const ext = path
.extname(/\.\w+$/.test(filename) ? filename : cleanUrl(filename))
.slice(1)
if (ext === 'cjs' || ext === 'mjs') {
loader = 'js'
} else {
loader = ext as Loader
}
}
let tsconfigRaw = options?.tsconfigRaw
// if options provide tsconfigraw in string, it takes highest precedence
if (typeof tsconfigRaw !== 'string') {
// these fields would affect the compilation result
// https://esbuild.github.io/content-types/#tsconfig-json
const meaningfulFields: Array<keyof TSCompilerOptions> = [
'target',
'jsxFactory',
'jsxFragmentFactory',
'useDefineForClassFields',
'importsNotUsedAsValues',
'preserveValueImports'
]
const compilerOptionsForFile: TSCompilerOptions = {}
if (loader === 'ts' || loader === 'tsx') {
const loadedTsconfig = await loadTsconfigJsonForFile(filename)
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
for (const field of meaningfulFields) {
if (field in loadedCompilerOptions) {
// @ts-ignore TypeScript can't tell they are of the same type
compilerOptionsForFile[field] = loadedCompilerOptions[field]
}
}
}
tsconfigRaw = {
...tsconfigRaw,
compilerOptions: {
...compilerOptionsForFile,
...tsconfigRaw?.compilerOptions
}
}
}
const resolvedOptions = {
sourcemap: true,
// ensure source file name contains full query
sourcefile: filename,
...options,
loader,
tsconfigRaw
} as ESBuildOptions
delete resolvedOptions.include
delete resolvedOptions.exclude
delete resolvedOptions.jsxInject
try {
const result = await transform(code, resolvedOptions)
let map: SourceMap
if (inMap && resolvedOptions.sourcemap) {
const nextMap = JSON.parse(result.map)
nextMap.sourcesContent = []
map = combineSourcemaps(filename, [
nextMap as RawSourceMap,
inMap as RawSourceMap
]) as SourceMap
} else {
map = resolvedOptions.sourcemap
? JSON.parse(result.map)
: { mappings: '' }
}
if (Array.isArray(map.sources)) {
map.sources = map.sources.map((it) => toUpperCaseDriveLetter(it))
}
return {
...result,
map
}
} catch (e: any) {
debug(`esbuild error with options used: `, resolvedOptions)
// patch error information
if (e.errors) {
e.frame = ''
e.errors.forEach((m: Message) => {
e.frame += `\n` + prettifyMessage(m, code)
})
e.loc = e.errors[0].location
}
throw e
}
}
export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
const filter = createFilter(
options.include || /\.(tsx?|jsx)$/,
options.exclude || /\.js$/
)
return {
name: 'vite:esbuild',
configureServer(_server) {
server = _server
server.watcher
.on('add', reloadOnTsconfigChange)
.on('change', reloadOnTsconfigChange)
.on('unlink', reloadOnTsconfigChange)
},
async configResolved(config) {
await initTSConfck(config)
},
buildEnd() {
// recycle serve to avoid preventing Node self-exit (#6815)
server = null as any
},
async transform(code, id) {
if (filter(id) || filter(cleanUrl(id))) {
const result = await transformWithEsbuild(code, id, options)
if (result.warnings.length) {
result.warnings.forEach((m) => {
this.warn(prettifyMessage(m, code))
})
}
if (options.jsxInject && /\.(?:j|t)sx\b/.test(id)) {
result.code = options.jsxInject + ';' + result.code
}
return {
code: result.code,
map: result.map
}
}
}
}
}
const rollupToEsbuildFormatMap: Record<
string,
TransformOptions['format'] | undefined
> = {
es: 'esm',
cjs: 'cjs',
// passing `var Lib = (() => {})()` to esbuild with format = "iife"
// will turn it to `(() => { var Lib = (() => {})() })()`,
// so we remove the format config to tell esbuild not doing this
//
// although esbuild doesn't change format, there is still possibility
// that `{ treeShaking: true }` removes a top-level no-side-effect variable
// like: `var Lib = 1`, which becomes `` after esbuild transforming,
// but thankfully rollup does not do this optimization now
iife: undefined
}
export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
return {
name: 'vite:esbuild-transpile',
async configResolved(config) {
await initTSConfck(config)
},
async renderChunk(code, chunk, opts) {
// @ts-ignore injected by @vitejs/plugin-legacy
if (opts.__vite_skip_esbuild__) {
return null
}
const target = config.build.target
const minify =
config.build.minify === 'esbuild' &&
// Do not minify ES lib output since that would remove pure annotations
// and break tree-shaking
// https://github.com/vuejs/core/issues/2860#issuecomment-926882793
!(config.build.lib && opts.format === 'es')
if ((!target || target === 'esnext') && !minify) {
return null
}
const res = await transformWithEsbuild(code, chunk.fileName, {
...config.esbuild,
target: target || undefined,
...(minify
? {
minify,
treeShaking: true,
format: rollupToEsbuildFormatMap[opts.format]
}
: undefined)
})
if (config.build.lib) {
// #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
// names are minified potentially causing collision with other globals.
// We use a regex to inject the helpers inside the wrappers.
// We don't need to create a MagicString here because both the helpers and
// the headers don't modify the sourcemap
const injectHelpers =
opts.format === 'umd'
? INJECT_HELPERS_UMD_RE
: opts.format === 'iife'
? INJECT_HELPERS_IIFE_RE
: undefined
if (injectHelpers) {
res.code = res.code.replace(
injectHelpers,
(_, helpers, header, rest) => header + helpers + rest
)
}
}
return res
}
}
}
function prettifyMessage(m: Message, code: string): string {
let res = colors.yellow(m.text)
if (m.location) {
const lines = code.split(/\r?\n/g)
const line = Number(m.location.line)
const column = Number(m.location.column)
const offset =
lines
.slice(0, line - 1)
.map((l) => l.length)
.reduce((total, l) => total + l + 1, 0) + column
res += `\n` + generateCodeFrame(code, offset, offset + 1)
}
return res + `\n`
}
const tsconfckParseOptions: TSConfckParseOptions = {
cache: new Map<string, TSConfckParseResult>(),
tsConfigPaths: undefined,
root: undefined,
resolveWithEmptyIfConfigNotFound: true
}
async function initTSConfck(config: ResolvedConfig) {
tsconfckParseOptions.cache!.clear()
const workspaceRoot = searchForWorkspaceRoot(config.root)
tsconfckParseOptions.root = workspaceRoot
tsconfckParseOptions.tsConfigPaths = new Set([
...(await findAll(workspaceRoot, {
skip: (dir) => dir === 'node_modules' || dir === '.git'
}))
])
}
async function loadTsconfigJsonForFile(
filename: string
): Promise<TSConfigJSON> {
try {
const result = await parse(filename, tsconfckParseOptions)
// tsconfig could be out of root, make sure it is watched on dev
if (server && result.tsconfigFile !== 'no_tsconfig_file_found') {
ensureWatchedFile(server.watcher, result.tsconfigFile, server.config.root)
}
return result.tsconfig
} catch (e) {
if (e instanceof TSConfckParseError) {
// tsconfig could be out of root, make sure it is watched on dev
if (server && e.tsconfigFile) {
ensureWatchedFile(server.watcher, e.tsconfigFile, server.config.root)
}
}
throw e
}
}
function reloadOnTsconfigChange(changedFile: string) {
// any tsconfig.json that's added in the workspace could be closer to a code file than a previously cached one
// any json file in the tsconfig cache could have been used to compile ts
if (
path.basename(changedFile) === 'tsconfig.json' ||
(changedFile.endsWith('.json') &&
tsconfckParseOptions?.cache?.has(changedFile))
) {
server.config.logger.info(
`changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure typescript is compiled with updated config values.`,
{ clear: server.config.clearScreen, timestamp: true }
)
// clear module graph to remove code compiled with outdated config
server.moduleGraph.invalidateAll()
// reset tsconfck so that recompile works with up2date configs
initTSConfck(server.config).finally(() => {
// force full reload
server.ws.send({
type: 'full-reload',
path: '*'
})
})
}
}