-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
提高gitment的浏览器兼容性 #52
base: master
Are you sure you want to change the base?
提高gitment的浏览器兼容性 #52
Conversation
相关的issue : #50 可以使用IE10以及10以上打开页面 JavaScript模块化的一二事 进行查看兼容性如何。 如果想在合并之前在自己的博客中使用,可以使用以下文件:(后者已压缩)
|
src/theme/default.js
Outdated
container.appendChild(renderHeader(state, instance)) | ||
container.appendChild(renderComments(state, instance)) | ||
container.appendChild(renderEditor(state, instance)) | ||
container.appendChild(renderFooter(state, instance)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
默认渲染函数之所以用 instance 调用渲染函数,是为了方便自定义主题。
比如一个人想自定义一下 footer,那他只要写一个 { renderFooter: () => { /* ... */} }
设为主题就好了,而不用将其他所有部分的逻辑都写一遍。自定义的 renderFooter
会覆盖 instance 上默认的 renderFooter
进行渲染。
package.json
Outdated
"dev": "webpack-dev-server --config webpack.dev.config.js --host 0.0.0.0 --progress --profile --colors" | ||
"build": "babel src --out-dir dist --ignore test.js --source-maps & cross-env NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors", | ||
"dev": "webpack-dev-server --config webpack.dev.config.js --host 0.0.0.0 --progress --profile --colors", | ||
"postbuild": "babel src --out-dir dist --ignore test.js --source-maps & webpack --config webpack.config.js -p --env.production" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有理解为什么需要 postbuild。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样的话可以使用npm run build
完成后自动生成压缩版本的gitment,当然可以直接生成压缩版本,但是为了不改动太大,所以就保留了你原有的脚本,在原有基础上添加
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个和 postbuild 的语义不大相符。而且 postbuild 是接着 build 执行的, 前半段命令在 build 时已经执行过了,没有必要再执行一遍。另外我也不建议通过 env.production
做标识来判断是否需要压缩,因为已经有 NODE_ENV
了,都是 production 语义上却有分歧。如果加压缩应该在 build 脚本里同时 build 出两个文件。
至于展开来说,我当时没加压缩主要是因为现在的 server 都开了 gzip,压不压缩意义不大。不过加了我也没有意见。
webpack.config.js
Outdated
devtool: 'source-map', | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: (env&&env.production)?'gitment.browser.min.js':'gitment.browser.js', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还没加 lint。所以请手动在 operator 间补下空格。
(env && env.production) ? 'gitment.browser.min.js' : 'gitment.browser.js'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
额,第一次用review
功能,不大会使用,望见谅。还有一个被隐藏了,this.theme = Object.assign({},defaultTheme,options.theme);
的改动是将所有的render method (包括default以及options)都统一挂载到this.theme
下
src/gitment.js
Outdated
oauth: {}, | ||
perPage: 20, | ||
maxCommentHeight: 250, | ||
}, options) | ||
|
||
this.theme = Object.assign({},defaultTheme,options.theme); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
将所有的render method (包括default以及options)都统一挂载到this.theme
下
src/gitment.js
Outdated
renderers.forEach(renderer => extendRenderer(this, renderer)) | ||
const renderers = Object.keys(theme) | ||
renderers.forEach(renderer => this[renderer]=this.theme[renderer]) | ||
extendRenderer(this,'render') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
貌似你原本的逻辑是将所有的render method都挂载到instance
下,然后供render
函数调用。但是问题在于extendRenderer()
函数对各个render method
进行包装,改变了render method的参数,由(state,instance)
变为container
。而正确的做法应该是只对render
包装即可,其余的render method简单地挂到instance
下,供render
函数调用
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extendRenderer
是将 theme 里的 renderer 暴露为 API,所以需要接收一个 container 用于渲染。而 theme 里的 renderer 不需要关心这些,只要接收 state 返回一个 DOM element 就可以了。
这两个的区别在于,instance 上的 renderer 是用户传参调用的,theme 里的 renderer 是被内部调用的,所以接受的参数不一样。比如用户只想渲染一个评论列表时就可以 gitment.renderComments(document.body)
。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来如此~引入babel-plugin-transform-runtime
后语法上的兼容性基本没有问题,但是在IE10下Mobx会报错。
[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[Autorun@2] TypeError: 调用的对象无效
没有接触过Mobx,但初步断定是在渲染的时候出错,所以目前需要解决的也是这个问题。
src/gitment.js
Outdated
renderers.forEach(renderer => extendRenderer(this, renderer)) | ||
const renderers = Object.keys(theme) | ||
renderers.forEach(renderer => this[renderer]=this.theme[renderer]) | ||
extendRenderer(this,'render') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extendRenderer
是将 theme 里的 renderer 暴露为 API,所以需要接收一个 container 用于渲染。而 theme 里的 renderer 不需要关心这些,只要接收 state 返回一个 DOM element 就可以了。
这两个的区别在于,instance 上的 renderer 是用户传参调用的,theme 里的 renderer 是被内部调用的,所以接受的参数不一样。比如用户只想渲染一个评论列表时就可以 gitment.renderComments(document.body)
。
package.json
Outdated
"dev": "webpack-dev-server --config webpack.dev.config.js --host 0.0.0.0 --progress --profile --colors" | ||
"build": "babel src --out-dir dist --ignore test.js --source-maps & cross-env NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors", | ||
"dev": "webpack-dev-server --config webpack.dev.config.js --host 0.0.0.0 --progress --profile --colors", | ||
"postbuild": "babel src --out-dir dist --ignore test.js --source-maps & webpack --config webpack.config.js -p --env.production" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个和 postbuild 的语义不大相符。而且 postbuild 是接着 build 执行的, 前半段命令在 build 时已经执行过了,没有必要再执行一遍。另外我也不建议通过 env.production
做标识来判断是否需要压缩,因为已经有 NODE_ENV
了,都是 production 语义上却有分歧。如果加压缩应该在 build 脚本里同时 build 出两个文件。
至于展开来说,我当时没加压缩主要是因为现在的 server 都开了 gzip,压不压缩意义不大。不过加了我也没有意见。
@@ -13,7 +13,7 @@ module.exports = { | |||
loaders: [ | |||
{ | |||
test: /\.js$/, | |||
exclude: /^node_mocules/, | |||
exclude: /^node_modules/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我竟然还写过这种代码。震惊。
src/theme/default.js
Outdated
container.appendChild(instance.theme.renderEditor(state, instance)) | ||
container.appendChild(instance.theme.renderFooter(state, instance)) | ||
container.appendChild(instance.renderHeader(state, instance)) | ||
container.appendChild(instance.renderComments(state, instance)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
找到IE的问题所在了。。确实是API的问题。我们简单地走一下流程。当调用gitment.render(container)
的时候,实际上是调用instance.render(container)
,而在instance.render(container)
中获取了instance.theme[renderer]
也就是this.theme.render(state, instance)
,是渲染的总入口(囊括了Header
等),但是在this.theme.render(state, instance)
中调用,比如默认的this.theme.render(state, instance)
是调用以下:
container.appendChild(instance.renderHeader(state, instance))
container.appendChild(instance.renderComments(state, instance))
container.appendChild(instance.renderEditor(state, instance))
container.appendChild(instance.renderFooter(state, instance))
问题来了,instance.renderHeader(state, instance)
的使用是错误的,签名对应不上,正确的参数列表应该是(container)
,因为instance.renderHeader
接收的是一个element
或字符串。而实际上传进去的state
和instance
,那container
是一个state对象,正确应该是使用instance.theme.renderxxxx(state, instance)
(只渲染不操作,因为操作已经在container.appendChild有了呀),不过并无大影响,因为你的isString()
已经帮你过滤了不正确的参数,粗略看了一下,不过是外加了一个div
。所以本来改了API后又改了回来
但是对于IE不通过的原因是,toString
不兼容。。所以加了一个空对象。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
完善的如何了,最新的引用地址是哪个?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
非官方的引用地址可以看上面的记录,等作者有空了并且review通过了应该就可以merge了。
http://blog.geekaholic.cn/js/thirdParty/gitment.browser.js
http://blog.geekaholic.cn/js/thirdParty/gitment.browser.min.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,先用着你的...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @GeekaholicLin . 大佬,刚看到你博客了,有兴趣互相添加友链否?宇宙湾(https://yuzhouwan.com)
所做的修改
配置文件以及源文件:增加babel对
Promise
以及Object.assign
的转换,增强浏览器的兼容,可以兼容到IE10(但是样式可能会错乱)npm script
以及跨平台脚本(使用了cross-env):方便npm run build
后直接生成gitment.browser.js
以及gitment.browser.min.js