-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-code.js
90 lines (79 loc) · 2.75 KB
/
generate-code.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
const namespace = require('./constants').vueOptionsNamespace;
const splitRE = /\r?\n/g;
module.exports = function generateCode(
scriptResult,
templateResult,
stylesResult,
customBlocksResult,
isFunctional,
) {
let output = '';
let renderFnStartLine;
let renderFnEndLine;
if (scriptResult) {
output += `${scriptResult.code};\n`;
} else {
output +=
`Object.defineProperty(exports, "__esModule", {\n` +
` value: true\n` +
`});\n` +
'module.exports.default = {};\n';
}
output +=
`var ${namespace} = typeof exports.default === 'function' ` +
`? exports.default.options ` +
`: exports.default\n`;
if (templateResult) {
renderFnStartLine = output.split(splitRE).length;
templateResult.code = templateResult.code.replace(
'var _c = _vm._self._c || _h',
'/* istanbul ignore next */\nvar _c = _vm._self._c || _h',
);
output += `${templateResult.code}\n`;
renderFnEndLine = output.split(splitRE).length;
output +=
`__options__.render = render\n` +
`${namespace}.staticRenderFns = staticRenderFns\n`;
if (isFunctional) {
output += `${namespace}.functional = true\n`;
output += `${namespace}._compiled = true\n`;
}
}
if (stylesResult) {
const styleStr = stylesResult
.map(
({ code, moduleName }) => `if(!this['${moduleName}']) {\n` +
` this['${moduleName}'] = {};\n` +
`}\n` +
`this['${moduleName}'] = Object.assign(\n` +
`this['${moduleName}'], ${code});\n`,
)
.join('');
if (isFunctional) {
output +=
`;(function() {\n` +
` var originalRender = ${namespace}.render\n` +
` var styleFn = function () { ${styleStr} }\n` +
` ${namespace}.render = function renderWithStyleInjection (h, context) {\n` +
` styleFn.call(context)\n` +
` return originalRender(h, context)\n` +
` }\n` +
`})()\n`;
} else {
output +=
`;(function() {\n` +
` var beforeCreate = ${namespace}.beforeCreate\n` +
` var styleFn = function () { ${styleStr} }\n` +
` ${namespace}.beforeCreate = beforeCreate ? [].concat(beforeCreate, styleFn) : [styleFn]\n` +
`})()\n`;
}
}
if (customBlocksResult) {
output += `;\n ${customBlocksResult}`;
}
return {
code: output,
renderFnStartLine,
renderFnEndLine,
};
};