-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
68 lines (56 loc) · 2.15 KB
/
index.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
/* eslint-env node */
const fs = require('fs');
const path = require('path');
const vueCompiler = require('vue-template-compiler');
const vueNextCompiler = require('vue-template-es2015-compiler');
const transforms = require('./transforms');
const extractHTML = (template, templatePath) => {
let resultHTML = '';
if (template.content === '' && template.src !== '') {
template.content = fs.readFileSync(
path.resolve(path.dirname(templatePath), template.src),
'utf8'
);
}
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 extractScriptContent = (script, scriptPath) => {
if (!script) {
throw 'No script available to transform';
}
if (script.content === '' && script.src !== '') {
script.content = fs.readFileSync(path.resolve(path.dirname(scriptPath), script.src), 'utf8');
}
return script.content;
};
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: false });
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);
}
let scriptContent = { code: '' };
scriptContent = extractScriptContent(script, filePath);
const transformKey = script.lang || 'babel';
return transforms[transformKey](scriptContent, filePath, render, staticRenderFns);
},
};