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
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015"]
"presets": ["es2015"],
"plugins": ["transform-runtime"]
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
},
"homepage": "https://github.com/imsun/gitment",
"scripts": {
"build": "babel src --out-dir dist --ignore test.js --source-maps & NODE_ENV=production webpack --config webpack.config.js --progress --profile --colors",
"build": "babel src --out-dir dist --ignore test.js --source-maps & webpack --progress --profile --colors & cross-env NODE_ENV=production webpack -p",
"dev": "webpack-dev-server --config webpack.dev.config.js --host 0.0.0.0 --progress --profile --colors"
},
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"cross-env": "^5.0.1",
"webpack": "^2.3.2",
"webpack-dev-server": "^2.4.2"
},
"dependencies": {
"babel-runtime": "^6.23.0",
"mobx": "^3.1.7"
},
"license": "MIT"
Expand Down
14 changes: 4 additions & 10 deletions src/gitment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const scope = 'public_repo'
function extendRenderer(instance, renderer) {
instance[renderer] = (container) => {
const targetContainer = getTargetContainer(container)
const render = instance.theme[renderer] || instance.defaultTheme[renderer]

const render = instance.theme[renderer]
autorun(() => {
const e = render(instance.state, instance)
if (targetContainer.firstChild) {
Expand Down Expand Up @@ -46,20 +45,17 @@ class Gitment {

constructor(options = {}) {
this.defaultTheme = defaultTheme
this.useTheme(defaultTheme)

Object.assign(this, {
id: window.location.href,
title: window.document.title,
link: window.location.href,
desc: '',
labels: [],
theme: defaultTheme,
oauth: {},
perPage: 20,
maxCommentHeight: 250,
}, options)

this.theme = Object.assign({}, defaultTheme, options.theme)
this.useTheme(this.theme)

const user = {}
Expand Down Expand Up @@ -127,10 +123,8 @@ class Gitment {
}

useTheme(theme = {}) {
this.theme = theme

const renderers = Object.keys(this.theme)
renderers.forEach(renderer => extendRenderer(this, renderer))
const renderers = Object.keys(theme)
renderers.forEach(renderer => extendRenderer(this,renderer))
}

update() {
Expand Down
1 change: 1 addition & 0 deletions src/theme/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function renderHeader({ meta, user, reactions }, instance) {
likeButton.classList.remove('liked')
likeButton.onclick = () => instance.like()
}

container.appendChild(likeButton)

const commentsCount = document.createElement('span')
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LS_ACCESS_TOKEN_KEY } from './constants'

export const isString = s => toString.call(s) === '[object String]'
export const isString = s => ({}).toString.call(s) === '[object String]'

export function getTargetContainer(container) {
let targetContainer
Expand Down
44 changes: 23 additions & 21 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
const path = require('path')

module.exports = {
context: path.join(__dirname, 'src'),
entry: './gitment.js',
devtool: 'source-map',
output: {
path: path.join(__dirname, 'dist'),
filename: 'gitment.browser.js',
libraryTarget: 'var',
library: 'Gitment',
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /^node_mocules/,
loaders: ['babel-loader'],
},
],
},
}
const PROD = process.env.NODE_ENV === 'production'
module.exports = function (env) {
return {
context: path.join(__dirname, 'src'),
entry: './gitment.js',
devtool: 'source-map',
output: {
path: path.join(__dirname, 'dist'),
filename: PROD ? 'gitment.browser.min.js' : 'gitment.browser.js',
libraryTarget: 'var',
library: 'Gitment',
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ['babel-loader'],
},
],
},
}
};
2 changes: 1 addition & 1 deletion webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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

loaders: ['babel-loader'],
},
],
Expand Down