Permalink
Browse files

feat(manifest): refactor & openGraph support

  • Loading branch information...
pi0 committed Jun 5, 2017
1 parent ab1cb77 commit 0768246f94818cd0693a28a3f7eff851b2ea3858
Showing with 57 additions and 45 deletions.
  1. +57 −45 modules/manifest/index.js
@@ -6,21 +6,14 @@ const hash = require('hash-sum')
const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
// const defaultIcon = Object.assign({
// src: fixUrl(`${routerBase}/icon.png`),
// sizes: '512x512',
// type: 'image/png'
// }, options.defaultIcon, this.options.manifest.defaultIcon)
module.exports = function nuxtManifest (options) {
// routerBase and publicPath
const routerBase = this.options.router.base
let publicPath = fixUrl(`${routerBase}/${this.options.build.publicPath}`)
if (isUrl(this.options.build.publicPath)) { // CDN
publicPath = this.options.build.publicPath
if (publicPath.indexOf('//') === 0) {
publicPath = '/' + publicPath
publicPath = '/' + publicPath // escape fixUrl
}
}
@@ -36,48 +29,24 @@ module.exports = function nuxtManifest (options) {
theme_color: (this.options.loading && this.options.loading.color) || '#3f51b5',
lang: 'en',
// Special options
_head: true,
_icons: true
// internal options
_meta: true,
_icons: true,
_openGraph: false
}
// Combine sources
const manifest = _.defaultsDeep({}, options, this.options.manifest, defaults)
// Extract options
const _head = Boolean(manifest._head)
const _icons = Boolean(manifest._icons)
// Cleanup internals
delete manifest.src
delete manifest._head
delete manifest._icons
const manifestSource = JSON.stringify(manifest)
const manifestFileName = `manifest.${hash(manifestSource)}.json`
// Register webpack plugin to emit manifest
this.options.build.plugins.push({
apply (compiler) {
compiler.plugin('emit', function (compilation, cb) {
compilation.assets[manifestFileName] = {
source: () => manifestSource,
size: () => manifestSource.length
}
cb()
})
}
})
// Generate icons
if (manifest._icons) {
// TODO
// Add manifest meta
if (!_.find(this.options.head.link, {rel: 'manifest'})) {
this.options.head.link.push({rel: 'manifest', href: fixUrl(`${publicPath}/${manifestFileName}`)})
} else {
console.warn('Manifest meta already provided! Skipping manifest.')
delete manifest._icons
}
// More meta
if (_head) {
// Head Meta Tags
if (manifest._meta) {
// Add favicon
if (!_.find(this.options.head.link, {rel: 'shortcut icon'})) {
this.options.head.link.push({rel: 'shortcut icon', href: manifest.icons[0].src})
@@ -109,13 +78,56 @@ module.exports = function nuxtManifest (options) {
}
this.options.head.htmlAttrs.lang = manifest.lang
}
delete manifest._meta
}
// Generate icons
if (_icons) {
// TODO
// OpenGraph headers
if (manifest._openGraph) {
// Add og:type
if (!_.find(this.options.head.meta, {property: 'og:type'})) {
this.options.head.meta.push({property: 'og:type', content: manifest.type || 'website'})
}
// Add og:title
if (!_.find(this.options.head.meta, {property: 'og:title'})) {
this.options.head.meta.push({property: 'og:title', content: manifest.name})
}
// Add og:description
if (!_.find(this.options.head.meta, {property: 'og:description'})) {
this.options.head.meta.push({property: 'og:description', content: manifest.description})
}
delete manifest._openGraph
}
// Cleanup internals
delete manifest.src
// Stringify manifest & generate hash
const manifestSource = JSON.stringify(manifest)
const manifestFileName = `manifest.${hash(manifestSource)}.json`
// Register webpack plugin to emit manifest
this.options.build.plugins.push({
apply (compiler) {
compiler.plugin('emit', function (compilation, cb) {
compilation.assets[manifestFileName] = {
source: () => manifestSource,
size: () => manifestSource.length
}
cb()
})
}
})
// Add manifest meta
if (!_.find(this.options.head.link, {rel: 'manifest'})) {
this.options.head.link.push({rel: 'manifest', href: fixUrl(`${publicPath}/${manifestFileName}`)})
} else {
console.warn('Manifest meta already provided! Skipping manifest.')
}
}

0 comments on commit 0768246

Please sign in to comment.