From 2862a5be8b2353747dd119e3075c074848ad7ad8 Mon Sep 17 00:00:00 2001 From: pimlie Date: Mon, 11 Feb 2019 12:00:11 +0100 Subject: [PATCH] feat: add browser build without ssr code --- scripts/rollup.config.js | 3 +- src/browser.js | 20 ++++++++++++ src/client/$meta.js | 14 +++++++++ src/index.js | 20 ++++++++++-- src/{shared => server}/$meta.js | 2 +- src/shared/{plugin.js => mixin.js} | 49 +++--------------------------- src/shared/options.js | 32 +++++++++++++++++++ 7 files changed, 90 insertions(+), 50 deletions(-) create mode 100644 src/browser.js create mode 100644 src/client/$meta.js rename src/{shared => server}/$meta.js (90%) rename src/shared/{plugin.js => mixin.js} (81%) create mode 100644 src/shared/options.js diff --git a/scripts/rollup.config.js b/scripts/rollup.config.js index 394b644..e6a2d0f 100644 --- a/scripts/rollup.config.js +++ b/scripts/rollup.config.js @@ -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', @@ -44,6 +44,7 @@ export default [{ ] }, { ...baseConfig, + input: 'src/index.js', output: { ...baseConfig.output, file: pkg.main, diff --git a/src/browser.js b/src/browser.js new file mode 100644 index 0000000..816946b --- /dev/null +++ b/src/browser.js @@ -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 diff --git a/src/client/$meta.js b/src/client/$meta.js new file mode 100644 index 0000000..cf7d5c0 --- /dev/null +++ b/src/client/$meta.js @@ -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) + } + } +} diff --git a/src/index.js b/src/index.js index 27a5bf0..0e1160f 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/src/shared/$meta.js b/src/server/$meta.js similarity index 90% rename from src/shared/$meta.js rename to src/server/$meta.js index a296fa4..ee40163 100644 --- a/src/shared/$meta.js +++ b/src/server/$meta.js @@ -1,5 +1,5 @@ -import inject from '../server/inject' import refresh from '../client/refresh' +import inject from './inject' export default function _$meta(options = {}) { /** diff --git a/src/shared/plugin.js b/src/shared/mixin.js similarity index 81% rename from src/shared/plugin.js rename to src/shared/mixin.js index 1dc7084..742c353 100644 --- a/src/shared/plugin.js +++ b/src/shared/mixin.js @@ -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 @@ -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 @@ -159,5 +118,5 @@ export default function VueMeta(Vue, options = {}) { }, 50) } }/**/ - }) + } } diff --git a/src/shared/options.js b/src/shared/options.js new file mode 100644 index 0000000..e230a96 --- /dev/null +++ b/src/shared/options.js @@ -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 +}