public_host 5 years ago
parent
commit
28b90ced28
  1. 38
      govue/bindata.go
  2. 37
      govue/govue-runtime/header.js
  3. 25
      govue/govue-runtime/runtime.js
  4. 8
      govue/govue.go
  5. 13
      static/index.html

38
govue/bindata.go
File diff suppressed because it is too large
View File

37
govue/govue-runtime/header.js

@ -4,7 +4,7 @@ var Promise;
var GoUseCallback; var GoUseCallback;
var GoUse = function (cb) { var GoUse = function (cb) {
GoUseCallback = "with(this){(" + cb.toString() + ")()}"; GoUseCallback = "with(this){(" + cb.toString() + ")()}";
}
};
var net = { var net = {
request: function (c) { request: function (c) {
var url = c["url"]; var url = c["url"];
@ -129,22 +129,49 @@ var axiosUse = function (ctx) {
var GetGoVueTemplate = function (raw) { var GetGoVueTemplate = function (raw) {
var tagStart = "<!--gv-start-->"; var tagStart = "<!--gv-start-->";
var tagEnd = "<!--gv-end-->"; var tagEnd = "<!--gv-end-->";
var start = raw.indexOf(tagStart)
var start = raw.indexOf(tagStart);
if (start < 0) { if (start < 0) {
return return
} }
var end = raw.indexOf(tagEnd)
var end = raw.indexOf(tagEnd);
if (end < 0) { if (end < 0) {
throw "注释未闭合"
throw "注释未闭合";
return return
} }
var content = raw.substring(start + tagStart.length, end); var content = raw.substring(start + tagStart.length, end);
var template = raw.substring(0, start) + "<!--vue-ssr-outlet-->\n{{{GoVueTemplate}}}\n{{{GoVueData}}}" + raw.substr(end + tagEnd.length); var template = raw.substring(0, start) + "<!--vue-ssr-outlet-->\n{{{GoVueTemplate}}}\n{{{GoVueData}}}" + raw.substr(end + tagEnd.length);
return { return {
template: template, template: template,
content: content, content: content,
} }
}
};
var GetGoVueTemplateInlineJs = function (raw) {
var tagStart = "<!--gv-js-start-->";
var tagEnd = "<!--gv-js-end-->";
var start = raw.indexOf(tagStart);
if (start < 0) {
return
}
var end = raw.indexOf(tagEnd);
if (end < 0) {
throw "js注释未闭合";
return
}
var content = raw.substring(start + tagStart.length, end);
var template = raw.substring(0, start) + "<!--gv-js-outlet-->" + raw.substr(end + tagEnd.length);
return {
template: template,
content: content,
}
};
var GetGoVueJsCode = function (htmlparser2, raw) { var GetGoVueJsCode = function (htmlparser2, raw) {

25
govue/govue-runtime/runtime.js

@ -1,20 +1,24 @@
GV.init(function (_require) { GV.init(function (_require) {
new (function () { new (function () {
this.require = _require; this.require = _require;
this.Vue = _require('Vue');
var Vue = this.Vue = _require('Vue');
this.VueRouter = _require('vue-router'); this.VueRouter = _require('vue-router');
this.Vue.use(this.VueRouter)
this.Vue.use(this.VueRouter);
this.axios = _require('axios'); this.axios = _require('axios');
var htmlparser2 = _require('htmlparser2'); var htmlparser2 = _require('htmlparser2');
axiosUse(this); axiosUse(this);
this.qs = _require('qs'); this.qs = _require('qs');
this.GoVueMount = function (app, context) { this.GoVueMount = function (app, context) {
var ctx = GetGoVueTemplate(GoHtmlSrc)
var ctx = GetGoVueTemplate(GoHtmlSrc);
var cleanInlineJsCtx = GetGoVueTemplateInlineJs(ctx.template);
app.$options.template = ctx.content; app.$options.template = ctx.content;
// console.log(ctx.template) // console.log(ctx.template)
var renderer = _require('vue-server-renderer').createRenderer({ var renderer = _require('vue-server-renderer').createRenderer({
template: ctx.template
})
template: cleanInlineJsCtx.template
});
if (!context) { if (!context) {
context = {} context = {}
@ -23,8 +27,10 @@ GV.init(function (_require) {
context.GoVueData = "<go-vue-data style='display: none'>" + Base64Encode(encodeURIComponent(JSON.stringify(app.$data))) + "</go-vue-data>" context.GoVueData = "<go-vue-data style='display: none'>" + Base64Encode(encodeURIComponent(JSON.stringify(app.$data))) + "</go-vue-data>"
renderer.renderToString(app, context, function (err, html) { renderer.renderToString(app, context, function (err, html) {
if (err) { if (err) {
Vue.options = {};
throw err throw err
} else { } else {
html = html.replace("<!--gv-js-outlet-->",cleanInlineJsCtx.content);
GoReturn(html); GoReturn(html);
} }
}) })
@ -37,7 +43,8 @@ GV.init(function (_require) {
GoSetError(JSON.stringify({ GoSetError(JSON.stringify({
code: GoUseCallback, code: GoUseCallback,
error: e.toString(), error: e.toString(),
}))
}));
Vue.options = {};
throw e throw e
} }
@ -52,9 +59,13 @@ GV.init(function (_require) {
GoSetError(JSON.stringify({ GoSetError(JSON.stringify({
code: "with(this){\n" + codes[i] + "\n}", code: "with(this){\n" + codes[i] + "\n}",
error: e.toString(), error: e.toString(),
}))
}));
Vue.options = {};
throw e throw e
} }
} }
Vue.options = {};
})() })()
}) })

8
govue/govue.go

@ -71,6 +71,10 @@ func (gv *GoVue) initRender(debug bool) (err error) {
if err != nil { if err != nil {
return return
} }
//headerScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "header.js"))
//if err != nil {
// return
//}
polyfill, err := gv.Resources.Asset(filepath.Join("govue-runtime", "polyfill.js")) polyfill, err := gv.Resources.Asset(filepath.Join("govue-runtime", "polyfill.js"))
if err != nil { if err != nil {
@ -81,6 +85,10 @@ func (gv *GoVue) initRender(debug bool) (err error) {
if err != nil { if err != nil {
return return
} }
//mainScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "runtime.js"))
//if err != nil {
// return
//}
govueScriptFile := "govue.js" govueScriptFile := "govue.js"
govueScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", govueScriptFile)) govueScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", govueScriptFile))

13
static/index.html

@ -63,18 +63,21 @@
</div> </div>
</div> </div>
<!--gv-end--> <!--gv-end-->
<script gv-src>
<!--gv-js-start-->
<script gv-src>
// 代码中使用component时必须被 gv-js-star|gv-js-end 注释包裹 , 如果使用<script gv-src src="...index.js">
Vue.component('govue-title', { Vue.component('govue-title', {
props:["govue_title"],
props: ["govue_title"],
template: '<h1 id="title">{{govue_title}}</h1>' template: '<h1 id="title">{{govue_title}}</h1>'
}); });
if (isBrowser) { if (isBrowser) {
GoHtmlSrc = "" GoHtmlSrc = ""
} }
var app = new Vue({ var app = new Vue({
data: { data: {
title: "govue", title: "govue",
@ -99,8 +102,10 @@
GoVueMount(app, { GoVueMount(app, {
title: "govue-基础golang开发的一套vue服务端渲染方案", title: "govue-基础golang开发的一套vue服务端渲染方案",
govue_title: "",//使用的组件,data返回的数据需要全部在context中指定,原因未知
}, "#app") }, "#app")
</script> </script>
<!--gv-js-end-->
</body> </body>
</html> </html>
Loading…
Cancel
Save