Nuxt/lib/module.js

154 lines
4.1 KiB
JavaScript
Raw Permalink 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'
import {chainFn, sequence} from './utils'
const debug = require('debug')('nuxt:module')
2017-05-11 10:59:08 +00:00
2017-05-11 10:04:15 +00:00
class Module {
2017-05-31 14:26:49 +00:00
constructor (nuxt) {
2017-05-11 10:04:15 +00:00
this.nuxt = nuxt
2017-05-11 10:59:08 +00:00
this.options = nuxt.options
this.modules = []
this.initing = this.ready()
}
async ready () {
if (this.initing) {
await this.initing
return this
}
// Install all modules in sequence
await sequence(this.options.modules, this.addModule.bind(this))
return this
2017-05-11 10:59:08 +00:00
}
2017-05-31 14:26:49 +00:00
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-31 14:26:49 +00:00
addTemplate (template) {
2017-05-14 23:01:41 +00:00
/* istanbul ignore if */
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-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)) {
debug('[nuxt] invalid template', template)
2017-05-11 10:59:08 +00:00
return
}
// Generate unique and human readable dst filename
const dst = template.fileName ||
(path.basename(srcPath.dir) + '.' + srcPath.name + '.' + hash(src) + srcPath.ext)
// 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)
return templateObj
}
2017-05-31 14:26:49 +00:00
addPlugin (template) {
const {dst} = this.addTemplate(template)
// Add to nuxt plugins
this.options.plugins.push({
src: path.join(this.nuxt.buildDir, dst),
ssr: template.ssr
})
2017-05-11 10:59:08 +00:00
}
2017-05-31 14:26:49 +00:00
addServerMiddleware (middleware) {
2017-05-19 09:14:24 +00:00
this.options.serverMiddleware.push(middleware)
}
2017-05-31 14:26:49 +00:00
extendBuild (fn) {
2017-05-12 07:57:24 +00:00
this.options.build.extend = chainFn(this.options.build.extend, fn)
}
2017-05-31 14:26:49 +00:00
extendRoutes (fn) {
2017-05-12 07:57:24 +00:00
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
2017-05-11 10:59:08 +00:00
}
2017-05-31 14:26:49 +00:00
requireModule (moduleOpts) {
if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) {
2017-05-20 09:17:14 +00:00
return false
}
2017-05-21 16:12:28 +00:00
this.modules.push(moduleOpts.src || moduleOpts)
2017-05-20 09:17:14 +00:00
return this.addModule(moduleOpts)
}
2017-05-31 14:26:49 +00:00
addModule (moduleOpts) {
2017-05-14 23:01:41 +00:00
/* istanbul ignore if */
2017-05-11 10:59:08 +00:00
if (!moduleOpts) {
return
}
// Allow using babel style array options
2017-05-31 14:58:36 +00:00
if (Array.isArray(moduleOpts)) {
moduleOpts = {
src: moduleOpts[0],
options: moduleOpts[1]
}
}
2017-05-11 10:59:08 +00:00
// Allows passing runtime options to each module
const options = moduleOpts.options || (typeof moduleOpts === 'object' ? moduleOpts : {})
const originalSrc = moduleOpts.src || moduleOpts
2017-05-11 10:59:08 +00:00
// Resolve module
2017-05-24 13:02:33 +00:00
let module = originalSrc
2017-05-11 10:59:08 +00:00
try {
2017-05-24 13:02:33 +00:00
if (typeof module === 'string') {
// Using ~ or ./ shorthand modules are resolved from project srcDir
2017-05-24 13:02:33 +00:00
if (module.indexOf('~') === 0 || module.indexOf('./') === 0) {
module = path.join(this.options.srcDir, module.substr(1))
2017-05-11 10:59:08 +00:00
}
2017-05-11 11:58:20 +00:00
// eslint-disable-next-line no-eval
2017-05-24 13:02:33 +00:00
module = eval('require')(module)
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-24 13:02:33 +00:00
console.error('[nuxt] Unable to resolve module', module)
2017-05-11 10:59:08 +00:00
// eslint-disable-next-line no-console
console.error(e)
2017-05-21 15:41:00 +00:00
process.exit(0)
2017-05-11 10:59:08 +00:00
}
// Validate module
2017-05-14 22:33:31 +00:00
/* istanbul ignore if */
2017-05-24 13:02:33 +00:00
if (typeof module !== 'function') {
2017-05-11 10:59:08 +00:00
// eslint-disable-next-line no-console
console.error(`[nuxt] Module [${originalSrc}] should export a function`)
process.exit(1)
2017-05-11 10:59:08 +00:00
}
// 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)
})
// If module send back a promise
2017-05-11 19:04:50 +00:00
if (result && result.then instanceof Function) {
return result.then(resolve)
}
// If not expecting a callback but returns no promise (=synchronous)
if (module.length < 2) {
return resolve(module)
}
2017-05-11 10:59:08 +00:00
})
2017-05-11 10:04:15 +00:00
}
}
export default Module