Skip to content

Commit

Permalink
feat: add browser build without ssr code
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Feb 11, 2019
1 parent a4ca39b commit 2862a5b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 50 deletions.
3 changes: 2 additions & 1 deletion scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const banner = `/**
`.replace(/ {4}/gm, '').trim()

const baseConfig = {
input: 'src/index.js',
input: 'src/browser.js',
output: {
file: pkg.web,
format: 'umd',
Expand Down Expand Up @@ -44,6 +44,7 @@ export default [{
]
}, {
...baseConfig,
input: 'src/index.js',
output: {
...baseConfig.output,
file: pkg.main,
Expand Down
20 changes: 20 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { version } from '../package.json'
import createMixin from './shared/mixin'
import setOptions from './shared/options'
import $meta from './client/$meta'

/**
* Plugin install function.
* @param {Function} Vue - the Vue constructor.
*/
function VueMeta(Vue, options = {}) {
options = setOptions(options)

Vue.prototype.$meta = $meta(options)

Vue.mixin(createMixin(options))
}

VueMeta.version = version

export default VueMeta
14 changes: 14 additions & 0 deletions src/client/$meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import refresh from './refresh'

export default function _$meta(options = {}) {
/**
* Returns an injector for server-side rendering.
* @this {Object} - the Vue instance (a root component)
* @return {Object} - injector
*/
return function $meta() {
return {
refresh: refresh(options).bind(this)
}
}
}
20 changes: 17 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { version } from '../package.json'
import install from './shared/plugin'
import createMixin from './shared/mixin'
import setOptions from './shared/options'
import $meta from './server/$meta'

install.version = version
/**
* Plugin install function.
* @param {Function} Vue - the Vue constructor.
*/
function VueMeta(Vue, options = {}) {
options = setOptions(options)

export default install
Vue.prototype.$meta = $meta(options)

Vue.mixin(createMixin(options))
}

VueMeta.version = version

export default VueMeta
2 changes: 1 addition & 1 deletion src/shared/$meta.js → src/server/$meta.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inject from '../server/inject'
import refresh from '../client/refresh'
import inject from './inject'

export default function _$meta(options = {}) {
/**
Expand Down
49 changes: 4 additions & 45 deletions src/shared/plugin.js → src/shared/mixin.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,7 @@
import batchUpdate from '../client/batchUpdate'
import { isUndefined, isFunction, isObject } from '../shared/typeof'
import $meta from './$meta'

import {
keyName,
attribute,
ssrAttribute,
tagIDKeyName,
metaTemplateKeyName,
contentKeyName
} from './constants'

// automatic install
if (!isUndefined(window) && !isUndefined(window.Vue)) {
Vue.use(VueMeta)
}

/**
* Plugin install function.
* @param {Function} Vue - the Vue constructor.
*/
export default function VueMeta(Vue, options = {}) {
// set some default options
const defaultOptions = {
keyName,
contentKeyName,
metaTemplateKeyName,
attribute,
ssrAttribute,
tagIDKeyName
}

// combine options
options = isObject('object') ? options : {}

for (const key in defaultOptions) {
if (!options[key]) {
options[key] = defaultOptions[key]
}
}

// bind the $meta method to this component instance
Vue.prototype.$meta = $meta(options)
import { isUndefined, isFunction } from '../shared/typeof'

export default function createMixin(options) {
// store an id to keep track of DOM updates
let batchID = null

Expand All @@ -55,7 +14,7 @@ export default function VueMeta(Vue, options = {}) {
}

// watch for client side component updates
Vue.mixin({
return {
beforeCreate() {
// Add a marker to know if it uses metaInfo
// _vnode is used to know that it's attached to a real component
Expand Down Expand Up @@ -159,5 +118,5 @@ export default function VueMeta(Vue, options = {}) {
}, 50)
}
}/**/
})
}
}
32 changes: 32 additions & 0 deletions src/shared/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isObject } from '../shared/typeof'
import {
keyName,
attribute,
ssrAttribute,
tagIDKeyName,
metaTemplateKeyName,
contentKeyName
} from './constants'

// set some default options
const defaultOptions = {
keyName,
contentKeyName,
metaTemplateKeyName,
attribute,
ssrAttribute,
tagIDKeyName
}

export default function setOptions(options) {
// combine options
options = isObject('object') ? options : {}

for (const key in defaultOptions) {
if (!options[key]) {
options[key] = defaultOptions[key]
}
}

return options
}

0 comments on commit 2862a5b

Please sign in to comment.