Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 9f5dd9f

Browse files
committed
wip compile templates
1 parent 7b45ae4 commit 9f5dd9f

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"rollup": "latest",
4949
"rollup-plugin-buble": "^0.13.0",
5050
"rollup-plugin-replace": "latest",
51-
"vue-hot-reload-api": "^1.2.2"
51+
"vue-hot-reload-api": "^1.2.2",
52+
"vue-template-compiler": "^2.0.0-rc.4"
5253
}
5354
}

src/vueTransform.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import deIndent from 'de-indent';
22
import htmlMinifier from 'html-minifier';
3+
import { compile as compileTemplate } from 'vue-template-compiler';
34
import parse5 from 'parse5';
45
import validateTemplate from 'vue-template-validator';
56
import { relative } from 'path';
@@ -38,6 +39,25 @@ function padContent(content) {
3839
.join('\n');
3940
}
4041

42+
/**
43+
* Only support for es5 modules
44+
*
45+
* @param script
46+
* @param render
47+
* @returns {string}
48+
*/
49+
function injectRender(script, render) {
50+
const matches = /(export default[^{]*\{)/g.exec(script);
51+
if (matches) {
52+
function toFunction (code) {
53+
return `function(){${code}})`;
54+
}
55+
return script.split(matches[1])
56+
.join(`${matches[1]} render: ${toFunction(render.render)}, staticRenderFns: [${render.staticRenderFns.map(toFunction).join(',')}]`);
57+
}
58+
throw new Error('[rollup-plugin-vue] could not find place to inject template in script.');
59+
}
60+
4161
/**
4262
* Only support for es5 modules
4363
*
@@ -78,15 +98,21 @@ function processTemplate(node, filePath, content) {
7898
* @param {string} content
7999
* @param {string} template
80100
*/
81-
function processScript(node, filePath, content, template) {
101+
function processScript(node, filePath, content, {template, render}) {
82102
const lang = checkLang(node) || 'js';
83103
let script = parse5.serialize(node);
84104
// pad the script to ensure correct line number for syntax errors
85105
const location = content.indexOf(script);
86106
const before = padContent(content.slice(0, location));
87107
script = before + script;
88-
script = injectTemplate(script, template, lang);
108+
if (template) {
109+
script = injectTemplate(script, template, lang);
110+
} else if (render) {
111+
script = injectRender(script, render, lang);
112+
}
89113
script = deIndent(script);
114+
115+
console.log(script)
90116
return script;
91117
}
92118

@@ -108,10 +134,11 @@ export default function vueTransform(code, filePath) {
108134

109135
// 4. Process template
110136
const template = processTemplate(nodes.template, filePath, code);
137+
const render = compileTemplate(template);
111138

112139
// 5. Process script & style
113140
return {
114-
js: processScript(nodes.script, filePath, code, template),
141+
js: processScript(nodes.script, filePath, code, { render }),
115142
css: nodes.style && {
116143
content: parse5.serialize(nodes.style),
117144
lang: checkLang(nodes.style),

0 commit comments

Comments
 (0)