forked from vuejs/vue-jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
176 lines (155 loc) · 4.14 KB
/
utils.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
const constants = require('./constants')
const loadPartialConfig = require('@babel/core').loadPartialConfig
const { resolveSync: resolveTsConfigSync } = require('tsconfig')
const chalk = require('chalk')
const path = require('path')
const fs = require('fs')
const fetchTransformer = function fetchTransformer(key, obj) {
for (const exp in obj) {
const matchKey = new RegExp(exp)
if (matchKey.test(key)) {
return obj[exp]
}
}
return null
}
const resolvePath = function resolvePath(pathToResolve) {
return /^(\.\.\/|\.\/|\/)/.test(pathToResolve)
? path.resolve(process.cwd(), pathToResolve)
: pathToResolve
}
const info = function info(msg) {
console.info(chalk.blue('\n[vue-jest]: ' + msg + '\n'))
}
const warn = function warn(msg) {
console.warn(chalk.red('\n[vue-jest]: ' + msg + '\n'))
}
const transformContent = function transformContent(
content,
filePath,
config,
transformer,
attrs
) {
if (!transformer) {
return content
}
try {
return transformer(content, filePath, config, attrs)
} catch (err) {
warn(`There was an error while compiling ${filePath} ${err}`)
}
return content
}
const getVueJestConfig = function getVueJestConfig(jestConfig) {
return (
(jestConfig &&
jestConfig.config &&
jestConfig.config.globals &&
jestConfig.config.globals['vue-jest']) ||
{}
)
}
const getBabelOptions = function loadBabelOptions(filename, options = {}) {
const opts = Object.assign(options, {
caller: {
name: 'vue-jest',
supportsStaticESM: false
},
filename,
sourceMaps: 'both'
})
return loadPartialConfig(opts).options
}
const getTsJestConfig = function getTsJestConfig(config) {
const tsConfigPath = path.resolve(
process.cwd(),
getVueJestConfig(config).tsConfig || ''
)
const isUsingTs = resolveTsConfigSync(tsConfigPath)
if (!isUsingTs) {
return null
}
const { ConfigSet } = require('ts-jest/dist/config/config-set')
const configSet = new ConfigSet(config.config)
const tsConfig = configSet.typescript || configSet.parsedTsConfig
// Force es5 to prevent const vue_1 = require('vue') from conflicting
return {
compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' }
}
}
function isValidTransformer(transformer) {
return (
isFunction(transformer.process) ||
isFunction(transformer.postprocess) ||
isFunction(transformer.preprocess)
)
}
const isFunction = fn => typeof fn === 'function'
const getCustomTransformer = function getCustomTransformer(
transform = {},
lang
) {
transform = { ...constants.defaultVueJestConfig.transform, ...transform }
const transformerPath = fetchTransformer(lang, transform)
if (!transformerPath) {
return null
}
let transformer
if (
typeof transformerPath === 'string' &&
require(resolvePath(transformerPath))
) {
transformer = require(resolvePath(transformerPath))
transformer = transformer.default || transformer
} else if (typeof transformerPath === 'object') {
transformer = transformerPath
}
if (!isValidTransformer(transformer)) {
throwError(
`transformer must contain at least one process, preprocess, or ` +
`postprocess method`
)
}
return transformer
}
const throwError = function error(msg) {
throw new Error('\n[vue-jest] Error: ' + msg + '\n')
}
const stripInlineSourceMap = function(str) {
return str.slice(0, str.indexOf('//# sourceMappingURL'))
}
const logResultErrors = result => {
if (result.errors.length) {
result.errors.forEach(function(msg) {
console.error('\n' + chalk.red(msg) + '\n')
})
throwError('Vue template compilation failed')
}
}
const loadSrc = (src, filePath) => {
var dir = path.dirname(filePath)
var srcPath = path.resolve(dir, src)
try {
return fs.readFileSync(srcPath, 'utf-8')
} catch (e) {
throwError(
'Failed to load src: "' + src + '" from file: "' + filePath + '"'
)
}
}
module.exports = {
stripInlineSourceMap,
throwError,
logResultErrors,
getCustomTransformer,
getTsJestConfig,
getBabelOptions,
getVueJestConfig,
transformContent,
info,
warn,
resolvePath,
fetchTransformer,
loadSrc
}