-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
155 lines (135 loc) · 3.85 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
const constants = require('./constants')
const loadPartialConfig = require('@babel/core').loadPartialConfig
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 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 createTransformer = require('ts-jest').createTransformer
const tr = createTransformer()
const configSet = tr.configsFor(config)
var tsConfig = configSet.typescript || configSet.parsedTsConfig
return { compilerOptions: tsConfig.options }
}
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))
} 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) {
const sourceMapLine = str.indexOf('//# sourceMappingURL');
if (sourceMapLine < 0) return str;
return str.slice(0, sourceMapLine);
}
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,
transformContent,
info,
warn,
resolvePath,
fetchTransformer,
loadSrc
}