forked from vire/jest-vue-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (100 loc) · 3.69 KB
/
index.js
File metadata and controls
115 lines (100 loc) · 3.69 KB
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
/* eslint-env node */
const path = require('path');
const vueCompiler = require('vue-template-compiler');
const vueNextCompiler = require('vue-template-es2015-compiler');
const babelCore = require('babel-core');
const findBabelConfig = require('find-babel-config');
const tsc = require('typescript');
const transformBabel = (src, filePath) => {
const {config} = findBabelConfig.sync(process.cwd());
const transformOptions = {
presets: ['es2015'],
plugins: ['transform-runtime']
};
const combinedTransformOptions = config || transformOptions;
combinedTransformOptions.sourceMaps = true;
combinedTransformOptions.filename = filePath;
let result;
try {
result = babelCore.transform(src, combinedTransformOptions);
} catch (error) {
// eslint-disable-next-line
console.error('Failed to compile scr with `babel` at `vue-preprocessor`');
}
return result;
};
const getTsConfig = () => {
try {
return require(path.resolve(process.cwd(), 'tsconfig.json'));
} catch (error) {
return {};
}
};
const transformTs = (src, path) => {
const {compilerOptions} = getTsConfig();
let result;
try {
result = tsc.transpile(src, compilerOptions, path, []);
} catch (error) {
// eslint-disable-next-line
console.error('Failed to compile src with `tsc` at `vue-preprocessor`');
}
return result;
};
const transforms = {
ts: transformTs,
typescript: transformTs,
babel: transformBabel
};
const extractHTML = (template, templatePath) => {
let resultHTML = '';
if (!template.lang || template.lang === 'resultHTML') {
resultHTML = template.content;
} else if (template.lang === 'pug') {
resultHTML = require('pug').compile(template.content)();
} else {
throw templatePath + ': unknown <template lang="' + template.lang + '">';
}
return resultHTML;
};
const generateOutput = (script, renderFn, staticRenderFns, map) => {
let output = '';
output +=
'/* istanbul ignore next */;(function(){\n' + script + '\n})()\n' +
'/* istanbul ignore next */if (module.exports.__esModule) module.exports = module.exports.default\n';
output += '/* istanbul ignore next */var __vue__options__ = (typeof module.exports === "function"' +
'? module.exports.options: module.exports)\n';
if (renderFn && staticRenderFns) {
output +=
'/* istanbul ignore next */__vue__options__.render = ' + renderFn + '\n' +
'/* istanbul ignore next */__vue__options__.staticRenderFns = ' + staticRenderFns + '\n';
}
return { code: output, map: map };
};
const stringifyRender = render => vueNextCompiler('function render () {' + render + '}');
const stringifyStaticRender = staticRenderFns => `[${staticRenderFns.map(stringifyRender).join(',')}]`;
module.exports = {
process(src, filePath) {
// code copied from https://github.com/locoslab/vue-typescript-jest/blob/master/preprocessor.js
// LICENSE MIT
// @author https://github.com/locobert
// heavily based on vueify (Copyright (c) 2014-2016 Evan You)
const { script, template } = vueCompiler.parseComponent(src, { pad: true});
const transformKey = script.lang || 'babel';
let transformedScript = script ? transforms[transformKey](script.content, filePath) : '';
let transformedMap;
if (transformKey === 'babel') {
transformedMap = transformedScript.map;
transformedScript = transformedScript.code;
}
let render;
let staticRenderFns;
if (template) {
const HTML = extractHTML(template, filePath);
const res = HTML && vueCompiler.compile(HTML);
render = stringifyRender(res.render);
staticRenderFns = stringifyStaticRender(res.staticRenderFns);
}
return generateOutput(transformedScript, render, staticRenderFns, transformedMap);
}
};