Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

GeekaholicLin
Copy link

所做的修改

  • 配置文件以及源文件:增加babel对Promise以及Object.assign的转换,增强浏览器的兼容,可以兼容到IE10(但是样式可能会错乱)

  • npm script以及跨平台脚本(使用了cross-env):方便npm run build后直接生成gitment.browser.js以及gitment.browser.min.js

@GeekaholicLin
Copy link
Author

相关的issue : #50

可以使用IE10以及10以上打开页面 JavaScript模块化的一二事 进行查看兼容性如何。
因为一些原因,还有一些浏览器尚未测试(FF番茄不了,移动设备有限)。

如果想在合并之前在自己的博客中使用,可以使用以下文件:(后者已压缩)

http://blog.geekaholic.cn/js/thirdParty/gitment.browser.js
http://blog.geekaholic.cn/js/thirdParty/gitment.browser.min.js

container.appendChild(renderHeader(state, instance))
container.appendChild(renderComments(state, instance))
container.appendChild(renderEditor(state, instance))
container.appendChild(renderFooter(state, instance))
Copy link
Owner

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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有理解为什么需要 postbuild。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样的话可以使用npm run build完成后自动生成压缩版本的gitment,当然可以直接生成压缩版本,但是为了不改动太大,所以就保留了你原有的脚本,在原有基础上添加

Copy link
Owner

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,压不压缩意义不大。不过加了我也没有意见。

devtool: 'source-map',
output: {
path: path.join(__dirname, 'dist'),
filename: (env&&env.production)?'gitment.browser.min.js':'gitment.browser.js',
Copy link
Owner

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'

Copy link
Author

@GeekaholicLin GeekaholicLin left a 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);
Copy link
Author

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')
Copy link
Author

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函数调用

Copy link
Owner

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)

Copy link
Author

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')
Copy link
Owner

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"
Copy link
Owner

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/,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我竟然还写过这种代码。震惊。

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))
Copy link
Author

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或字符串。而实际上传进去的stateinstance,那container是一个state对象,正确应该是使用instance.theme.renderxxxx(state, instance)(只渲染不操作,因为操作已经在container.appendChild有了呀),不过并无大影响,因为你的isString()已经帮你过滤了不正确的参数,粗略看了一下,不过是外加了一个div。所以本来改了API后又改了回来

但是对于IE不通过的原因是,toString不兼容。。所以加了一个空对象。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

完善的如何了,最新的引用地址是哪个?

Copy link
Author

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,先用着你的...

Copy link

@asdf2014 asdf2014 Jun 21, 2018

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

catusax added a commit to catusax/hexo-theme-apollo that referenced this pull request Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants