Browse Source

fix

tags/v1.0.1
3136352472 5 years ago
parent
commit
3386e0cb49
  1. 90
      govue/bindata.go
  2. 2
      govue/cmd/main.go
  3. 11
      govue/govue-js-src/build.js
  4. 1
      govue/govue-js-src/domino
  5. 8
      govue/govue-js-src/package.json
  6. 3
      govue/govue-js-src/predo.js
  7. 178
      govue/govue-js-src/src/index.js
  8. 47
      govue/govue-js-src/test.js
  9. 91027
      govue/govue-runtime/govue-dev.js
  10. 91019
      govue/govue-runtime/govue-release.js
  11. 38
      govue/govue-runtime/header.js
  12. 7
      govue/govue-runtime/runtime.js
  13. 8959
      govue/govue-runtime/vuessr.js
  14. 39
      govue/govue.go
  15. 28
      jsruntime/runtime.go
  16. 1
      pool/pool.go
  17. 1
      readme.md
  18. 21
      static/404.html
  19. 20
      static/css/main.css
  20. 36
      static/index.html
  21. 6
      static/index.js
  22. 11965
      static/js/vue.js
  23. 13
      static/ssr.vue
  24. 29
      static/use.js

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

2
govue/cmd/main.go

@ -69,7 +69,7 @@ func main() {
govue.SetPoolConfig(config.Pool)
gv, err := govue.NewGoVue(config.UseJsFile, config.StaticDir, mode == "debug")
gv, err := govue.NewGoVue(config.UseJsFile, config.StaticDir, config.Mode == "debug")
if errorPage404 != "" {
config.ErrorPage404 = errorPage404

11
govue/govue-js-src/build.js

@ -1,6 +1,11 @@
var fs = require("fs");
var envify = require('envify/custom')
var browserify = require("browserify");
browserify("./src/index.js").bundle().pipe(fs.createWriteStream("./dist/govue-dev.js"));
browserify("./src/index.js")
// .transform("babelify", {
// plugins: [
@ -11,13 +16,13 @@ browserify("./src/index.js")
// "transform-remove-strict-mode"
// ]
// })
.transform(
.transform(
// 用来处理 `node_modules` 文件
{ global: true },
envify({ NODE_ENV: 'production' })
)
.bundle()
.pipe(fs.createWriteStream("./dist/index.js"));
.pipe(fs.createWriteStream("./dist/govue-release.js"));

1
govue/govue-js-src/domino

@ -0,0 +1 @@
Subproject commit e543301316335417ae43e5e645361fe2669fd05b

8
govue/govue-js-src/package.json

@ -15,13 +15,11 @@
"qs": "^6.9.4",
"through": "^2.3.8",
"vue": "^2.6.11",
"vue-router": "^3.2.0"
"vue-router": "^3.2.0",
"vue-server-renderer": "^2.6.11"
},
"scripts": {
"build": "node build.js && node predo.js && cp ./dist/index.js ../govue-runtime/govue.js"
},
"browser": {
"vue": "vue/dist/vue.common.js"
"build": "node build.js && node predo.js && cp ./dist/govue-release.js ../govue-runtime/govue-release.js && cp ./dist/govue-dev.js ../govue-runtime/govue-dev.js"
},
"devDependencies": {
"@babel/core": "^7.9.6",

3
govue/govue-js-src/predo.js

@ -1,8 +1,9 @@
var fs = require("fs");
var content = fs.readFileSync("./dist/index.js")
var content = fs.readFileSync("./dist/govue-release.js")
// content = content.toString().replace(`_m("`, `this._m("`).replace(`var isHTMLTag = `, `var isHTMLTag = makeMap('html,body,base,head,link,meta,style,title');var _isHTMLTag = `)
// content = content.toString().replace(`'production'`).replace(`"development"`)
// fs.writeFileSync("./dist/index.js", content)

178
govue/govue-js-src/src/index.js

@ -1,3 +1,6 @@
process = require('process');
domino = require('../domino/lib/index');
window = domino.createWindow("", "http://127.0.0.1/");
document = window.document;
@ -5,18 +8,49 @@ location = window.location;
Intl = require('./Intl');
Vue = require('vue');
Promise = require('es6-promise').Promise;
if (process.env.NODE_ENV == "production") {
Vue = require('../node_modules/vue/dist/vue.min');
} else {
Vue = require('../node_modules/vue/dist/vue');
}
Vue.config.ignoredElements = [/^b-/, /^gv-/];
vuessr = require('vue-server-renderer')
VueRouter = require('vue-router');
Vue.GoVueExtend = Vue.extend;
Vue.UseRaw = function () {
Vue.use_raw = true
return Vue
};
function HTMLEncode(html) {
var temp = document.createElement("div");
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
var output = temp.innerHTML;
temp = null;
return output;
}
Vue.extend = function (a, b) {
if (Vue.use_raw) {
var template = HTMLEncode(a.template)
a.template = "<gv-template-html>" + template + "<gv-template-html>"
Vue.use_raw = false;
}
return Vue.GoVueExtend(a, b)
}
vuessr = require('vue-server-renderer')
VueRouter = require('vue-router');
Vue.use(VueRouter)
$ = jQuery = require('jquery');
require('jquery-bbq');
qs = require('qs');
Promise = require('es6-promise').Promise;
axios = require('axios');
@ -27,93 +61,93 @@ var settle = require('axios/lib/core/settle');
var createError = require('axios/lib/core/createError');
function transformResponse(mpResponse, config, mpRequestOption) {
var headers = mpResponse.header || mpResponse.headers;
var status = mpResponse.statusCode || mpResponse.status;
var statusText = '';
if (status === 200) {
statusText = 'OK';
}
else if (status === 400) {
statusText = 'Bad Request';
}
var response = {
data: mpResponse.data,
status: status,
statusText: statusText,
headers: headers,
config: config,
request: mpRequestOption
};
return response;
var headers = mpResponse.header || mpResponse.headers;
var status = mpResponse.statusCode || mpResponse.status;
var statusText = '';
if (status === 200) {
statusText = 'OK';
}
else if (status === 400) {
statusText = 'Bad Request';
}
var response = {
data: mpResponse.data,
status: status,
statusText: statusText,
headers: headers,
config: config,
request: mpRequestOption
};
return response;
}
axios.defaults.adapter = function (config) {
return new Promise(function (resolve, reject) {
// console.log("resolve", JSON.stringify(resolve));
// console.log("reject", JSON.stringify(reject));
// console.log("config", JSON.stringify(config));
// console.log("axios.defaults.headers", JSON.stringify(axios.defaults.headers));
var mpRequestOption = {
url: buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
method: config["method"].toUpperCase(),
data: config["data"],
header: config["headers"],
timeout: config["timeout"],
success: function (mpResponse) {
// console.log("success");
// console.log(JSON.stringify(mpResponse));
var response = transformResponse(mpResponse, config, mpRequestOption);
settle(resolve, reject, response);
},
fail: function (error) {
reject(createError(error.data));
},
};
net.request(mpRequestOption)
})
return new Promise(function (resolve, reject) {
// console.log("resolve", JSON.stringify(resolve));
// console.log("reject", JSON.stringify(reject));
// console.log("config", JSON.stringify(config));
// console.log("axios.defaults.headers", JSON.stringify(axios.defaults.headers));
var mpRequestOption = {
url: buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
method: config["method"].toUpperCase(),
data: config["data"],
header: config["headers"],
timeout: config["timeout"],
success: function (mpResponse) {
// console.log("success");
// console.log(JSON.stringify(mpResponse));
var response = transformResponse(mpResponse, config, mpRequestOption);
settle(resolve, reject, response);
},
fail: function (error) {
reject(createError(error.data));
},
};
net.request(mpRequestOption)
})
};
useRoute = {};
GoUseRegistered = function (id, cb) {
useRoute[id] = cb
useRoute[id] = cb
};
goUseCallCache
GoUseCall = function (e) {
// console.log("GoUseCall", 1)
if (goUseCallCache) {
goUseCallCache(e)
}
// console.log("GoUseCall", 2)
var id = e["id"];
var path = e["path"];
var query = e["query"];
if (useRoute[id]) {
// console.log(useRoute[id])
useRoute[id](query);
return
}
// console.log("GoUseCall", 3)
if (useRoute[path]) {
useRoute[path](query);
return
}
// console.log("路由未找到:", id, "-", path)
// console.log("GoUseCall", 1)
if (goUseCallCache) {
goUseCallCache(e)
}
// console.log("GoUseCall", 2)
var id = e["id"];
var path = e["path"];
var query = e["query"];
if (useRoute[id]) {
// console.log(useRoute[id])
useRoute[id](query);
return
}
// console.log("GoUseCall", 3)
if (useRoute[path]) {
useRoute[path](query);
return
}
// console.log("路由未找到:", id, "-", path)
}
GoUse = function (cb) {
goUseCallCache = cb
goUseCallCache = cb
}
for (var i in global) {
window[i] = global[i]
window[i] = global[i]
}

47
govue/govue-js-src/test.js

@ -1,47 +0,0 @@
global.domino = null
global.window = null
global.document = null
global.location = null
global.Vue = null
global.VueRouter = null
global.axios = null
navigator = null
require("./dist/index")
var html = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <title>Title</title>\n</head>\n<body>\n<div id=\"classifyer\">\n code : {{code}}\n <li v-for=\"todo in data\">\n classifyer_id :{{ todo.classifyer_id }}\n name :{{ todo.name }}\n </li>\n</div>\n</body>\n</html>"
window = domino.createWindow(html, "http://127.0.0.1/#/detail/?id=123");
document = window.document;
location = window.location;
var vue = new Vue({
el: "#classifyer",
data: function(){
return {
code: 212123213123,
data: [
{ name: "1asdasdasd23" }
]
}
}
});
console.log(document.innerHTML);
Vue.axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});

91027
govue/govue-runtime/govue-dev.js
File diff suppressed because it is too large
View File

91019
govue/govue-runtime/govue-release.js
File diff suppressed because it is too large
View File

38
govue/govue-runtime/header.js

@ -6,6 +6,7 @@ global.Vue = Vue;
global.VueRouter = VueRouter;
global.Intl = Intl;
var VueDisableTag = [];
navigator = {
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 a/537.36 "
@ -96,6 +97,10 @@ var net = {
function GoVueRender(template, app, context) {
var raw = GoReadFile(template);
console.log(vuessr);
var renderer = vuessr.createRenderer({
template: raw
});
@ -126,7 +131,12 @@ function LoadPage(src, href) {
* @return {string}
*/
function GetGoVueId() {
return document.querySelector("[gv-id]").getAttribute("gv-id");
var h = document.querySelector("[gv-id]")
var id = "";
if (h){
id = h.getAttribute("gv-id");
}
return id;
}
/**
@ -137,7 +147,7 @@ function RunInlineCode() {
var jsInlineFiles = [];
for (var i = jsInline.length - 1; i >= 0; i--) {
var isGoVueSrc = jsInline[i].hasAttribute("gv-src");
var isGoVueCommon = jsInline[i].hasAttribute("gv-common");
var isGoVuePrivate = jsInline[i].hasAttribute("gv-private");
if (isGoVueSrc) {
var res = {};
var file = jsInline[i].getAttribute("src");
@ -151,11 +161,10 @@ function RunInlineCode() {
jsInlineFiles.unshift(res);
}
if (isGoVueCommon) {
jsInline[i].removeAttribute("gv-src");
jsInline[i].removeAttribute("gv-common");
} else {
if (isGoVuePrivate) {
jsInline[i].remove();
} else {
jsInline[i].removeAttribute("gv-src");
}
}
}
@ -277,4 +286,19 @@ var GoVueIgnoreOnEvent = function () {
}
})
};
};
function HTMLDecode(text) {
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
}
function GoVueRestoreRawTemplate(){
var template = document.getElementsByTagName("gv-template-html");
for(var i in template){
template[i].outerHTML = HTMLDecode(template[i].innerHTML);
}
}

7
govue/govue-runtime/runtime.js

@ -49,6 +49,13 @@ if (!IS_VUE_SSR) {
}
try {
GoVueRestoreRawTemplate();
} catch (e) {
console.log("GoVueRestoreRawTemplate:" + e);
}
try {
GoReturn(document.innerHTML);
} catch (e) {
console.log("GoReturn:" + e)

8959
govue/govue-runtime/vuessr.js
File diff suppressed because it is too large
View File

39
govue/govue.go

@ -21,13 +21,15 @@ type GoVue struct {
UseJsPath string
ErrorPage404 string
Resources *assetfs.AssetFS
IsDebugMode bool
jsRuntimePool *pool.JsRuntimePool
}
func NewGoVueDefaultConfig(debug bool) (gv *GoVue, err error) {
gv = &GoVue{
Resources: assetFS(),
Resources: assetFS(),
IsDebugMode: debug,
}
err = gv.initRender(debug)
return
@ -35,9 +37,10 @@ func NewGoVueDefaultConfig(debug bool) (gv *GoVue, err error) {
func NewGoVue(useJsPath string, staticPath string, debug bool) (gv *GoVue, err error) {
gv = &GoVue{
StaticPath: staticPath,
UseJsPath: useJsPath,
Resources: assetFS(),
StaticPath: staticPath,
UseJsPath: useJsPath,
Resources: assetFS(),
IsDebugMode: debug,
}
err = gv.initRender(debug)
return
@ -59,51 +62,47 @@ func (gv *GoVue) initRender(debug bool) (err error) {
if gv.UseJsPath == "" {
gv.UseJsPath = filepath.Join(gv.StaticPath, "use.js")
}
govueScriptFile := "govue-release.js"
if gv.IsDebugMode {
govueScriptFile = "govue-dev.js"
}
//vuessr, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "vuessr.js"))
//if err != nil {
// return
//}
//polyfill, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "polyfill.js"))
//if err != nil {
// return
//}
//
//mainScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "runtime.js"))
//headerScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "header.js"))
//if err != nil {
// return
//}
//
//headerScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "header.js"))
//govueScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime",govueScriptFile))
//if err != nil {
// return
//}
//
//govueScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-js-src", "dist", "index.js"))
//mainScript, err := ioutil.ReadFile(filepath.Join("govue", "govue-runtime", "runtime.js"))
//if err != nil {
// return
//}
vuessr, err := gv.Resources.Asset(filepath.Join("govue-runtime", "vuessr.js"))
if err != nil {
return
}
polyfill, err := gv.Resources.Asset(filepath.Join("govue-runtime", "polyfill.js"))
if err != nil {
return
}
mainScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "runtime.js"))
headerScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "header.js"))
if err != nil {
return
}
govueScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "govue.js"))
govueScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", govueScriptFile))
if err != nil {
return
}
headerScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "header.js"))
mainScript, err := gv.Resources.Asset(filepath.Join("govue-runtime", "runtime.js"))
if err != nil {
return
}
@ -116,9 +115,6 @@ func (gv *GoVue) initRender(debug bool) (err error) {
Src: string(headerScript),
},
jsruntime.Rely{
Src: string(vuessr),
},
jsruntime.Rely{
Src: string(govueScript),
},
}, jsruntime.ModeSync, debug)
@ -204,6 +200,7 @@ func (gv *GoVue) LoadStaticResources(request *http.Request, goExtDataS ...interf
} else {
jr.SetVariable("IS_VUE_SSR", false)
}
err = jr.Render(filePath, fmt.Sprintf("http://%s%s", request.Host, request.RequestURI), string(result), goExtData, func(data string) {
result = []byte(data)
})

28
jsruntime/runtime.go

@ -93,8 +93,10 @@ func NewJsRuntime(mainScript string, extFileDir string, rels Relys, mode RunMode
}
func (jr *JsRuntime) PrintError(msg string, code string) {
log.Println(msg)
if !jr.IsDebugModel {
log.Println(msg)
return
}
var errLine string
@ -127,16 +129,24 @@ func (jr *JsRuntime) PrintError(msg string, code string) {
if len(lines)-1 < linesEnd {
linesEnd = len(lines)
}
log.Println("error line linesEnd", linesEnd)
//linesEnd = linesStart + linesEnd
for e := range lines[linesStart:linesEnd] {
if e+linesStart+1 == line {
lines[linesStart:linesEnd][e] = fmt.Sprintf(`%2d: >>>>>> %s`, e+linesStart, lines[linesStart:linesEnd][e])
} else {
lines[linesStart:linesEnd][e] = fmt.Sprintf(`%2d: %s`, e+linesStart, lines[linesStart:linesEnd][e])
if linesStart > linesEnd {
errLine = "无法定位的报错位置"
linesStart = 0
} else {
//
//log.Println("error line linesStart", linesStart)
//log.Println("error line linesEnd", linesEnd)
//linesEnd = linesStart + linesEnd
for e := range lines[linesStart:linesEnd] {
if e+linesStart+1 == line {
lines[linesStart:linesEnd][e] = fmt.Sprintf(`%2d: >>>>>> %s`, e+linesStart, lines[linesStart:linesEnd][e])
} else {
lines[linesStart:linesEnd][e] = fmt.Sprintf(`%2d: %s`, e+linesStart, lines[linesStart:linesEnd][e])
}
}
errLine = strings.Join(lines[linesStart:linesEnd], "\n")
}
errLine = strings.Join(lines[linesStart:linesEnd], "\n")
}
if jr.RenderCallBackFun != nil {

1
pool/pool.go

@ -101,6 +101,7 @@ func (jrp *JsRuntimePool) JsRuntimeCall(call func(jr *jsruntime.JsRuntime)) (err
o := obj.(*jsruntime.JsRuntime)
call(o)
//err = jrp.jsRuntimePool.InvalidateObject(jrp.ctx, obj)
err = jrp.jsRuntimePool.ReturnObject(jrp.ctx, obj)
return
}

1
readme.md

@ -27,6 +27,7 @@
+ location[不完备的dom环境]
## 扩展方法
+ Vue.UseRaw().component(..)[vue原始模板替换,不进行渲染]
+ GoUse(call function(ctx))[所有请求都经过该方法,优先于内联模式]
+ GoUseRegistered(id string,call function(query))[注册id标签对应的方法,优先于内联模式]
+ GetGoVueId()[获取当前页面Id]

21
static/404.html

@ -1,21 +0,0 @@
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="keywords" content="">
<link rel="stylesheet" href="css/main.css">
<script src="js/vue.min.js"></script>
</head>
<body>
<div id="app" class="content">
<my-title></my-title>
<a>404 找不到页面</a>
</div>
</body>
<script gv-src>
new Vue({
el: "#app",
});
</script>
</html>

20
static/css/main.css

@ -1,20 +0,0 @@
html, body {
margin: 0;
padding: 0;
color: #888888;
}
.content {
text-align: center;
}
.content h1 {
font-size: 68px;
}
.content p {
font-size: 28px;
}
pre {
width: 300px;
}

36
static/index.html

@ -1,36 +0,0 @@
<!doctype html>
<html lang="zh" gv-id="index">
<head>
<meta charset="utf-8">
<meta name="keywords" content="">
<link rel="stylesheet" href="css/main.css">
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<div class="content">
<my-title></my-title>
<a v-on:click="click" gv-ignore>{{desc}}</a>
<br>
<br>
<br>
<a href="#" v-on:click="ssr" gv-ignore>{{a}}</a>
</div>
</div>
</body>
<script>
new Vue({
el: "#app",
methods: {
click: function () {
alert("点击触发");
},
ssr: function () {
location.href = "ssr.html?test"
},
}
});
</script>
</html>

6
static/index.js

@ -1,6 +0,0 @@
new Vue({
el: "#pre",
data: {
pre: "扩展参数\nhtml上的gv属性,值为页面id\nserc上的gv属性,表示脚本需要服务端渲染"
}
});

11965
static/js/vue.js
File diff suppressed because it is too large
View File

13
static/ssr.vue

@ -1,13 +0,0 @@
<div id="app">访问的 URL {{ url }}</div>
<script gv-src>
var context = {
title: 'hello'
};
var app = new Vue({
el:"#app",
data: {
url: location.href
}
});
GoVueRender("template/ssr.html", app, context)
</script>

29
static/use.js

@ -1,29 +0,0 @@
//公用
GoUse(function () {
});
//分页面
GoUseRegistered("index", function (query) {
// 定义一个名为 button-counter 的新组件
document.title = "govue";
document
.getElementsByName("keywords")[0]
.setAttribute("content", "govue,服务端渲染,ssr");
Vue.component('my-title', {
template: '<h1 id="title">govue</h1>'
});
new Vue({
el: "#app",
data: {
"desc": "基础golang开发的一套vue服务端渲染方案",
"a": "官方ssr模式",
},
});
});
Loading…
Cancel
Save