-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcompiler.js
229 lines (198 loc) · 6.84 KB
/
compiler.js
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
import { parse, compileScript, compileTemplate, compileStyleAsync } from '@vue/compiler-sfc'
import hash from 'hash-sum'
import { genHotReloadCode } from './hmr'
import path from 'path'
import { appendSourceMaps, combineSourceMaps } from './source-map'
import { formatError } from './error'
export class VueCompiler extends MultiFileCachingCompiler {
constructor () {
super({
compilerName: 'vue3-components',
defaultCacheSize: 1024 * 1024 * 10,
})
}
getCacheKey (inputFile) {
return [
inputFile.getSourceHash(),
inputFile.getPathInPackage(),
]
}
normalizeTemplate(template) {
// In order to prevent Prettier from reporting error,
// one more temporary variable had to be used to reconstruct follow code:
// const indent = template.match(/^\n?(\s+)/)?.[1]
const temp = template.match(/^\n?(\s+)/)
const indent = temp && temp[1]
if (indent) {
return template
.split('\n')
.map(str => str.replace(indent, ''))
.join('\n')
}
return template
}
async compileOneFile (inputFile) {
const contents = inputFile.getContentsAsString()
const filename = inputFile.getPathInPackage()
const { errors, descriptor } = parse(contents, {
filename,
})
if (errors.length) {
for (const error of errors) {
console.error(formatError(error, contents, filename))
}
throw new Error(`Parsing failed for ${inputFile.getDisplayPath()} (${errors.length} error(s))`)
}
const compileResult = {
source: '',
sourceMap: null,
styles: [],
}
const referencedImportPaths = []
const isProd = process.env.NODE_ENV === 'production'
const hasScoped = descriptor.styles.some((s) => s.scoped)
const scopeId = hash(filename)
if (descriptor.script || descriptor.scriptSetup) {
const scriptResult = compileScript(descriptor, {
id: scopeId,
isProd,
})
compileResult.source += scriptResult.content.replace(/export default/, 'const __script__ = ')
compileResult.sourceMap = scriptResult.map
}
if (descriptor.template) {
let template
const lang = descriptor.template.attrs.lang
// if a template language is set (for example pug)
// check if there's a compiler and compile the template
if(lang) {
const templateCompiler = global.vue.lang[lang]
if (templateCompiler) {
const result = templateCompiler({
source: this.normalizeTemplate(descriptor.template.content),
inputFile: this.inputFile,
basePath: descriptor.template.map.file,
})
template = result.template
} else {
throw new Error(`Compiler missing for ${lang}`)
}
} else {
template = descriptor.template.content
}
const templateResult = compileTemplate({
id: scopeId,
filename,
source: template,
scoped: hasScoped,
isProd,
inMap: descriptor.template.map,
compilerOptions: {
scopeId: hasScoped ? `data-v-${scopeId}` : undefined,
},
})
if (templateResult.errors && templateResult.errors.length) {
for (const error of templateResult.errors) {
console.error(formatError(error, contents, filename))
}
throw new Error(`Compiling template failed for ${inputFile.getDisplayPath()} (${templateResult.errors.length} error(s))`)
}
if (!compileResult.source) {
compileResult.source = 'const __script__ = {};'
} else {
compileResult.source += '\n'
}
const lines = compileResult.source.split('\n').length
compileResult.source += templateResult.code
.replace(/export function render/, '__script__.render = function')
.replace(/export const render/, '__script__.render')
if (templateResult.map) {
if (compileResult.sourceMap) {
compileResult.sourceMap = await appendSourceMaps(templateResult.map, compileResult.sourceMap, lines - 1)
} else {
compileResult.sourceMap = templateResult.map
}
}
}
// Scope id
if (hasScoped) {
compileResult.source += `\n__script__.__scopeId = 'data-v-${scopeId}'`
}
// HMR
if (process.env.NODE_ENV !== 'production' && inputFile.hmrAvailable()) {
compileResult.source += genHotReloadCode(scopeId)
}
// File (devtools)
if (process.env.NODE_ENV !== 'production') {
compileResult.source += `\n__script__.__file = ${JSON.stringify(path.resolve(process.cwd(), filename))}`
}
// Default export
compileResult.source += '\nexport default __script__'
const babelOptions = Babel.getDefaultOptions()
babelOptions.babelrc = true
babelOptions.sourceMaps = true
babelOptions.filename = babelOptions.sourceFileName = filename
const transpiled = Babel.compile(compileResult.source, babelOptions, {
cacheDirectory: this._diskCache,
})
compileResult.source = transpiled.code
if (transpiled.map && compileResult.sourceMap) {
compileResult.sourceMap = await combineSourceMaps(transpiled.map, compileResult.sourceMap)
}
for (const style of descriptor.styles) {
// compile sass, scss, etc first to css
if (style.lang) {
const styleCompiler = global.vue.lang[style.lang]
if (styleCompiler) {
// expect this compiler to return css
style.content = styleCompiler({
data: style.content,
filename,
})
} else {
throw new Error(`Compiler missing for ${style.lang}`)
}
}
// compile css styles so scope etc is applied
const styleResult = await compileStyleAsync({
id: scopeId,
filename,
source: style.content,
scoped: style.scoped,
isProd,
inMap: style.map,
})
compileResult.styles.push({
source: styleResult.code,
sourceMap: styleResult.map,
})
}
return {
compileResult,
referencedImportPaths,
}
}
addCompileResult (inputFile, result) {
if (result.source) {
inputFile.addJavaScript({
path: inputFile.getPathInPackage() + '.js',
sourcePath: inputFile.getPathInPackage(),
data: result.source,
sourceMap: result.sourceMap,
})
}
for (const style of result.styles) {
inputFile.addStylesheet({
path: inputFile.getPathInPackage(),
sourcePath: inputFile.getPathInPackage(),
data: style.source,
sourceMap: style.sourceMap,
lazy: false,
})
}
}
compileResultSize (result) {
const styleSize = result.styles.reduce((total, style) => total + style.source.length + (style.sourceMap ? style.sourceMap.length : 0), 0)
return result.source.length + (result.sourceMap ? result.sourceMap.length : 0) + styleSize
}
}