2017-05-11 10:04:15 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-05-11 10:59:08 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import {uniq} from 'lodash'
|
2017-05-11 18:11:00 +00:00
|
|
|
import hash from 'hash-sum'
|
2017-05-12 07:57:24 +00:00
|
|
|
import {chainFn} from './utils'
|
2017-05-11 10:59:08 +00:00
|
|
|
|
2017-05-11 10:04:15 +00:00
|
|
|
class Module {
|
|
|
|
constructor (nuxt) {
|
|
|
|
this.nuxt = nuxt
|
2017-05-11 10:59:08 +00:00
|
|
|
this.options = nuxt.options
|
|
|
|
}
|
|
|
|
|
|
|
|
addVendor (vendor) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!vendor) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
|
|
|
|
}
|
|
|
|
|
2017-05-11 18:11:00 +00:00
|
|
|
addTemplate (template) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 18:11:00 +00:00
|
|
|
if (!template) {
|
2017-05-11 10:59:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-11 18:11:00 +00:00
|
|
|
// Validate & parse source
|
|
|
|
const src = template.src || template
|
|
|
|
const srcPath = path.parse(src)
|
2017-05-14 22:33:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
|
|
|
|
// eslint-disable-next-line no-console
|
2017-05-11 18:11:00 +00:00
|
|
|
console.warn('[Nuxt] invalid template', template)
|
2017-05-11 10:59:08 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-11 18:11:00 +00:00
|
|
|
// Generate unique and human readable dst filename
|
2017-05-11 19:37:59 +00:00
|
|
|
const dst = template.fileName ||
|
2017-05-14 20:59:06 +00:00
|
|
|
(path.basename(srcPath.dir) + '.' + srcPath.name + '.' + hash(src) + srcPath.ext)
|
2017-05-11 18:11:00 +00:00
|
|
|
// Add to templates list
|
|
|
|
const templateObj = {
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
options: template.options
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
2017-05-11 18:47:53 +00:00
|
|
|
this.options.build.templates.push(templateObj)
|
2017-05-12 08:15:37 +00:00
|
|
|
// Watch template for changes
|
|
|
|
this.addWatch(src)
|
2017-05-11 18:11:00 +00:00
|
|
|
return templateObj
|
|
|
|
}
|
|
|
|
|
2017-05-12 08:15:37 +00:00
|
|
|
addWatch (pattern) {
|
|
|
|
this.options.build.watch.push(pattern)
|
|
|
|
}
|
|
|
|
|
2017-05-11 18:11:00 +00:00
|
|
|
addPlugin (template) {
|
|
|
|
const {dst} = this.addTemplate(template)
|
|
|
|
// Add to nuxt plugins
|
|
|
|
this.options.plugins.push({
|
|
|
|
src: '~/.nuxt/' + dst,
|
2017-05-14 20:56:30 +00:00
|
|
|
ssr: template.ssr,
|
2017-05-14 20:39:27 +00:00
|
|
|
injectAs: template.injectAs
|
2017-05-11 18:11:00 +00:00
|
|
|
})
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 18:54:00 +00:00
|
|
|
addServerMiddleware (middleware) {
|
2017-05-19 09:14:24 +00:00
|
|
|
this.options.serverMiddleware.push(middleware)
|
2017-05-12 10:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 07:57:24 +00:00
|
|
|
extendBuild (fn) {
|
|
|
|
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
extendRoutes (fn) {
|
|
|
|
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
installModule (moduleOpts) {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!moduleOpts) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Allows passing runtime options to each module
|
|
|
|
const options = moduleOpts.options || {}
|
2017-05-11 11:58:20 +00:00
|
|
|
let src = moduleOpts.src || moduleOpts
|
2017-05-11 10:59:08 +00:00
|
|
|
// Resolve module
|
|
|
|
let module
|
|
|
|
try {
|
|
|
|
if (typeof src === 'string') {
|
|
|
|
// Using ~ shorthand modules are resolved from project srcDir
|
|
|
|
if (src.indexOf('~') === 0) {
|
2017-05-11 11:58:20 +00:00
|
|
|
src = path.resolve(this.options.srcDir, src.substr(1))
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
2017-05-11 11:58:20 +00:00
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
module = eval('require')(src)
|
2017-05-11 10:59:08 +00:00
|
|
|
}
|
2017-05-14 23:01:41 +00:00
|
|
|
} catch (e) /* istanbul ignore next */ {
|
2017-05-11 10:59:08 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2017-05-11 11:58:20 +00:00
|
|
|
console.error('[Nuxt] Unable to resolve module', src)
|
2017-05-11 10:59:08 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Validate module
|
2017-05-14 22:33:31 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (!(module instanceof Function)) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('[Nuxt] Module should be a function', module)
|
|
|
|
}
|
|
|
|
// Call module with `this` context and pass options
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-05-11 19:04:50 +00:00
|
|
|
const result = module.call(this, options, err => {
|
2017-05-14 23:17:13 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 10:59:08 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
resolve(module)
|
|
|
|
})
|
2017-05-11 19:04:50 +00:00
|
|
|
if (result && result.then instanceof Function) {
|
|
|
|
return result.then(resolve)
|
|
|
|
}
|
2017-05-11 10:59:08 +00:00
|
|
|
})
|
2017-05-11 10:04:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Module
|