/
esbuild.ts
165 lines (149 loc) · 4.04 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
import path from 'path'
import chalk from 'chalk'
import { Plugin } from '../plugin'
import {
Service,
Message,
Loader,
TransformOptions,
TransformResult
} from 'esbuild'
import { createDebugger, generateCodeFrame } from '../utils'
import merge from 'merge-source-map'
import { SourceMap } from 'rollup'
import { ResolvedConfig } from '..'
import { createFilter } from '@rollup/pluginutils'
const debug = createDebugger('vite:esbuild')
// lazy start the service
let _servicePromise: Promise<Service> | undefined
export interface ESBuildOptions extends TransformOptions {
include?: string | RegExp | string[] | RegExp[]
exclude?: string | RegExp | string[] | RegExp[]
jsxInject?: string
}
export async function ensureService() {
if (!_servicePromise) {
_servicePromise = require('esbuild').startService()
}
return _servicePromise!
}
export async function stopService() {
if (_servicePromise) {
const service = await _servicePromise
service.stop()
_servicePromise = undefined
}
}
export type EsbuildTransformResult = Omit<TransformResult, 'map'> & {
map: SourceMap
}
export async function transformWithEsbuild(
code: string,
filename: string,
options?: TransformOptions,
inMap?: object
): Promise<EsbuildTransformResult> {
const service = await ensureService()
const resolvedOptions = {
loader: path.extname(filename).slice(1) as Loader,
sourcemap: true,
// ensure source file name contains full query
sourcefile: filename,
...options
} as ESBuildOptions
delete resolvedOptions.include
delete resolvedOptions.exclude
delete resolvedOptions.jsxInject
try {
const result = await service.transform(code, resolvedOptions)
if (inMap) {
const nextMap = JSON.parse(result.map)
// merge-source-map will overwrite original sources if newMap also has
// sourcesContent
nextMap.sourcesContent = []
return {
...result,
map: merge(inMap, nextMap) as SourceMap
}
} else {
return {
...result,
map: JSON.parse(result.map)
}
}
} catch (e) {
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
)
return {
name: 'vite:esbuild',
async transform(code, id) {
if (filter(id)) {
const result = await transformWithEsbuild(code, id, options)
if (result.warnings.length) {
result.warnings.forEach((m) => {
this.warn(prettifyMessage(m, code))
})
}
if (options.jsxInject) {
result.code = options.jsxInject + ';' + result.code
}
return {
code: result.code,
map: result.map
}
}
},
async closeBundle() {
await stopService()
}
}
}
export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
return {
name: 'vite:esbuild-transpile',
async renderChunk(code, chunk) {
const target = config.build.target
const minify = config.build.minify === 'esbuild'
if (!target && !minify) {
return null
}
return transformWithEsbuild(code, chunk.fileName, {
target: target || undefined,
minify
})
},
async closeBundle() {
await stopService()
}
}
}
function prettifyMessage(m: Message, code: string): string {
let res = chalk.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`
}