Nuxt/lib/module.js

113 lines
2.8 KiB
JavaScript
Raw Normal View History

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'
import hash from 'hash-sum'
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) {
if (!vendor) {
return
}
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
}
addTemplate (template) {
if (!template) {
2017-05-11 10:59:08 +00:00
return
}
// Validate & parse source
const src = template.src || template
const srcPath = path.parse(src)
2017-05-11 10:59:08 +00:00
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
// eslint-disable-next-line no-console
console.warn('[Nuxt] invalid template', template)
2017-05-11 10:59:08 +00:00
return
}
// Generate unique and human readable dst filename
const dst = template.dst ||
((template.dstName || (path.basename(srcPath.dir) + '.' + srcPath.name)) + '.' + hash(src) + (template.dstExt || srcPath.ext))
// Add to templates list
const templateObj = {
src,
dst,
options: template.options
2017-05-11 10:59:08 +00:00
}
this.options.build.templatesFiles.push(templateObj)
return templateObj
}
addPlugin (template) {
const {dst} = this.addTemplate(template)
// Add to nuxt plugins
this.options.plugins.push({
src: '~/.nuxt/' + dst,
ssr: Boolean(template.ssr)
})
2017-05-11 10:59:08 +00:00
}
extendBuild (extendFn) {
if (!(extendFn instanceof Function)) {
return
}
// Add extendFn to chain
const _extend = this.options.build.extend
this.options.build.extend = function () {
extendFn.apply(this, arguments)
if (_extend) {
_extend.apply(this, arguments)
}
}
}
installModule (moduleOpts) {
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
}
} catch (e) {
// 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
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 11:58:20 +00:00
return module.call(this, options, err => {
2017-05-11 10:59:08 +00:00
if (err) {
return reject(err)
}
resolve(module)
})
})
2017-05-11 10:04:15 +00:00
}
}
export default Module