-
Notifications
You must be signed in to change notification settings - Fork 156
/
process.js
190 lines (164 loc) · 5.63 KB
/
process.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
const { parse, compileTemplate, compileScript } = require('@vue/compiler-sfc')
const { transform } = require('@babel/core')
const babelTransformer = require('babel-jest').default
const typescriptTransformer = require('./transformers/typescript')
const coffeescriptTransformer = require('./transformers/coffee')
const _processStyle = require('./process-style')
const processCustomBlocks = require('./process-custom-blocks')
const getTypeScriptConfig = require('./utils').getTypeScriptConfig
const getVueJestConfig = require('./utils').getVueJestConfig
const logResultErrors = require('./utils').logResultErrors
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
const getCustomTransformer = require('./utils').getCustomTransformer
const loadSrc = require('./utils').loadSrc
const generateCode = require('./generate-code')
const mapLines = require('./map-lines')
const vueComponentNamespace = require('./constants').vueComponentNamespace
function resolveTransformer(lang = 'js', vueJestConfig) {
const transformer = getCustomTransformer(vueJestConfig['transform'], lang)
if (/^typescript$|tsx?$/.test(lang)) {
return transformer || typescriptTransformer
} else if (/^coffee$|coffeescript$/.test(lang)) {
return transformer || coffeescriptTransformer
} else {
return transformer || babelTransformer.createTransformer()
}
}
function processScript(scriptPart, filePath, config) {
if (!scriptPart) {
return null
}
let content = scriptPart.content
let filename = filePath
if (scriptPart.src) {
content = loadSrc(scriptPart.src, filePath)
filename = scriptPart.src
}
const vueJestConfig = getVueJestConfig(config)
const transformer = resolveTransformer(scriptPart.lang, vueJestConfig)
const result = transformer.process(content, filename, config)
result.code = stripInlineSourceMap(result.code)
result.map = mapLines(scriptPart.map, result.map)
return result
}
function processScriptSetup(descriptor, filePath, config) {
if (!descriptor.scriptSetup) {
return null
}
const vueJestConfig = getVueJestConfig(config)
const content = compileScript(descriptor, {
id: filePath,
refTransform: true,
...vueJestConfig.compilerOptions
})
const contentMap = mapLines(descriptor.scriptSetup.map, content.map)
const transformer = resolveTransformer(
descriptor.scriptSetup.lang,
vueJestConfig
)
const result = transformer.process(content.content, filePath, config)
result.map = mapLines(contentMap, result.map)
return result
}
/**
* Process SFC <template> section.
* @param {import('@vue/compiler-sfc').SFCDescriptor} descriptor
* @param {string} filename
* @param {import('@jest/transform').TransformOptions} config
*/
function processTemplate(descriptor, filename, config) {
const { template, scriptSetup } = descriptor
if (!template) {
return null
}
const vueJestConfig = getVueJestConfig(config)
if (template.src) {
template.content = loadSrc(template.src, filename)
}
let bindings
if (scriptSetup) {
const scriptSetupResult = compileScript(descriptor, {
id: filename,
refTransform: true,
...vueJestConfig.compilerOptions
})
bindings = scriptSetupResult.bindings
}
// Since Vue 3.2.13, it's possible to use TypeScript in templates,
// but this needs the `isTS` option of the compiler.
// We could let users set it themselves, but vue-loader and vite automatically add it
// if the script is in TypeScript, so let's do the same for a seamless experience.
const lang =
(descriptor.scriptSetup && descriptor.scriptSetup.lang) ||
(descriptor.script && descriptor.script.lang)
const isTS = /^typescript$|tsx?$/.test(lang)
const result = compileTemplate({
id: filename,
source: template.content,
filename,
preprocessLang: template.lang,
preprocessOptions: vueJestConfig[template.lang],
compilerOptions: {
bindingMetadata: bindings,
mode: 'module',
isTS,
...vueJestConfig.compilerOptions
}
})
logResultErrors(result)
// TypeScript
if (isTS) {
const tsconfig = getTypeScriptConfig(vueJestConfig.tsConfig)
if (tsconfig) {
const { transpileModule } = require('typescript')
const { outputText } = transpileModule(result.code, { tsconfig })
return { code: outputText }
}
}
// babel
const babelify = transform(result.code, {
filename: 'file.js',
presets: ['@babel/preset-env']
})
return { code: babelify.code }
}
function processStyle(styles, filename, config) {
if (!styles) {
return null
}
const filteredStyles = styles
.filter(style => style.module)
.map(style => ({
code: _processStyle(style, filename, config),
moduleName: style.module === true ? '$style' : style.module
}))
return filteredStyles.length ? filteredStyles : null
}
module.exports = function(src, filename, config) {
const { descriptor } = parse(src, { filename })
const componentNamespace =
getVueJestConfig(config)['componentNamespace'] || vueComponentNamespace
const templateResult = processTemplate(descriptor, filename, config)
const scriptResult = processScript(descriptor.script, filename, config)
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
const stylesResult = processStyle(descriptor.styles, filename, config)
const customBlocksResult = processCustomBlocks(
descriptor.customBlocks,
filename,
componentNamespace,
config
)
const output = generateCode({
scriptResult,
scriptSetupResult,
templateResult,
customBlocksResult,
componentNamespace,
filename,
stylesResult
})
return {
code: output.code,
map: output.map.toString()
}
}