-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.js
More file actions
137 lines (121 loc) · 4.5 KB
/
transform.js
File metadata and controls
137 lines (121 loc) · 4.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const babel = require('@babel/core');
const compiler = require('@vue/compiler-sfc');
function removeSourceMap(ast) {
babel.traverse(ast, {
enter(path) {
if (path.node.loc) delete path.node.loc;
delete path.node.start;
delete path.node.end;
}
});
}
const {
Identifier,
VariableDeclaration,
VariableDeclarator,
ExpressionStatement,
AssignmentExpression,
MemberExpression,
ExportDefaultDeclaration,
LogicalExpression,
FunctionDeclaration
} = babel.types;
function getRenderFunctionDeclaration(ast) {
const expr = ast.program.body[0].declarations[0].init;
return FunctionDeclaration(Identifier('render'), expr.params, expr.body);
}
function getStaticRenderFunctionExpressions(ast) {
return ast.program.body[1].declarations[0].init;
}
const getPlugin = (renderFunctionDeclr, staticRenderArrayExpr, isFunctional) => {
return function AddFunctionPlugin() {
return {
visitor: {
ExportDefaultDeclaration(path) {
const body = path.parent.body;
const expression = path.node.declaration;
const statements = [
VariableDeclaration('const',
[VariableDeclarator(Identifier('__export__'), expression)]
),
ExportDefaultDeclaration(Identifier('__export__'))
];
if (renderFunctionDeclr) {
statements.splice(1, 0, ExpressionStatement(
AssignmentExpression('=',
MemberExpression(Identifier('__export__'), Identifier('render')),
LogicalExpression('||',
MemberExpression(Identifier('__export__'), Identifier('render')),
Identifier('render')
)
)
));
statements.splice(1, 0, renderFunctionDeclr);
}
if (staticRenderArrayExpr) {
statements.splice(1, 0, ExpressionStatement(
AssignmentExpression('=',
MemberExpression(Identifier('__export__'), Identifier('staticRenderFns')),
staticRenderArrayExpr
)
));
}
if (isFunctional) {
statements.splice(1, 0, ExpressionStatement(
AssignmentExpression('=',
MemberExpression(Identifier('__export__'), Identifier('functional')),
Identifier('true')
)
));
}
const args = [body.indexOf(path.node), 1].concat(statements);
body.splice.apply(body, args);
}
}
};
}
};
module.exports = async function (vueSource, vueFilename, extraPlugins) {
vueFilename = vueFilename.replace(/\\/g, "/");
const plugins = extraPlugins || [];
const sfc = compiler.parse({source: vueSource, filename: vueFilename});
const script = sfc.script || sfc.scriptSetup ? compiler.compileScript(sfc) : null;
const lang = sfc.script?.lang || sfc.scriptSetup?.lang;
const isTS = !!(lang && /tsx?$/.test(lang));
const whitespace = sfc.template ? (sfc.template.attrs && sfc.template.attrs.condense ? 'condense' : 'preserve') : null;
const NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = "production";
const template = sfc.template ? compiler.compileTemplate({
bindings: script && script.bindings,
source: sfc.template.content,
isFunctional: sfc.template ? sfc.template.attrs.functional : false,
isProduction: true, // just disables prettifying render functions as of 2.2.0
compilerOptions: {outputSourceRange: true, whitespace},
transpileOptions: {transforms: {spreadRest: false}},
isTS
}) : null;
process.env.NODE_ENV = NODE_ENV;
let renderFunctionDeclr, staticRenderArrayExpr;
if (template) {
const plugins = isTS ? ['@babel/plugin-transform-typescript'] : [];
const ast = babel.transformSync(template.code, {plugins, ast: true}).ast;
removeSourceMap(ast);
renderFunctionDeclr = getRenderFunctionDeclaration(ast);
staticRenderArrayExpr = getStaticRenderFunctionExpressions(ast);
}
plugins.unshift(getPlugin(renderFunctionDeclr, staticRenderArrayExpr, sfc.template && sfc.template.attrs.functional));
if (isTS) plugins.unshift(require('@babel/plugin-transform-typescript').default);
const generated = babel.transformSync(script ? script.content : 'export default {};', {
plugins,
inputSourceMap: script.map,
sourceFileName: vueFilename,
sourceMaps: true
});
return {
code: generated.code,
map: generated.map,
tips: template && template.tips || [],
errors: template && template.errors || [],
template
};
};