Nuxt/lib/core/module.js

176 lines
4.3 KiB
JavaScript
Raw Normal View History

const path = require('path')
const fs = require('fs')
const { uniq } = require('lodash')
const hash = require('hash-sum')
2018-01-11 16:11:50 +00:00
const { chainFn, sequence, printWarn } = require('../common/utils')
2017-05-11 10:59:08 +00:00
module.exports = class ModuleContainer {
2017-10-30 17:41:22 +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
2018-01-11 13:28:45 +00:00
this.requiredModules = {}
2017-07-17 19:26:41 +00:00
}
2017-10-30 17:41:22 +00:00
async ready() {
2018-01-11 13:28:45 +00:00
// Call before hook
2017-10-31 11:45:32 +00:00
await this.nuxt.callHook('modules:before', this, this.options.modules)
2018-01-11 13:28:45 +00:00
2017-10-30 17:41:22 +00:00
// Load every module in sequence
2017-07-17 19:26:41 +00:00
await sequence(this.options.modules, this.addModule.bind(this))
2018-01-11 13:28:45 +00:00
2017-10-30 21:39:08 +00:00
// Call done hook
await this.nuxt.callHook('modules:done', this)
2017-05-11 10:59:08 +00:00
}
2017-10-30 17:41:22 +00:00
addVendor(vendor) {
2017-05-14 23:01:41 +00:00
/* istanbul ignore if */
if (typeof vendor !== 'string' && !Array.isArray(vendor)) {
throw new Error('Invalid vendor: ' + vendor)
2017-05-11 10:59:08 +00:00
}
2018-01-11 13:28:45 +00:00
2017-05-11 10:59:08 +00:00
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
}
2017-10-30 17:41:22 +00:00
addTemplate(template) {
2017-05-14 23:01:41 +00:00
/* istanbul ignore if */
2018-01-11 15:13:52 +00:00
if (!template) {
2018-01-11 13:28:45 +00:00
throw new Error('Invalid template:' + template)
2017-05-11 10:59:08 +00:00
}
2018-01-11 13:28:45 +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)) {
2018-01-11 13:28:45 +00:00
throw new Error('Template not found:' + template)
2017-05-11 10:59:08 +00:00
}
2018-01-11 13:28:45 +00:00
// Generate unique and human readable dst filename
2018-01-11 13:28:45 +00:00
const dst =
template.fileName ||
2018-01-11 19:43:34 +00:00
path.basename(srcPath.dir) + `.${srcPath.name}.${hash(src)}` + srcPath.ext
2018-01-11 13:28:45 +00:00
// Add to templates list
const templateObj = {
src,
dst,
options: template.options
2017-05-11 10:59:08 +00:00
}
2018-01-11 13:28:45 +00:00
2017-05-11 18:47:53 +00:00
this.options.build.templates.push(templateObj)
return templateObj
}
2017-10-30 17:41:22 +00:00
addPlugin(template) {
2017-06-11 14:17:36 +00:00
const { dst } = this.addTemplate(template)
2018-01-11 13:28:45 +00:00
// Add to nuxt plugins
this.options.plugins.unshift({
2017-06-11 14:17:36 +00:00
src: path.join(this.options.buildDir, dst),
ssr: template.ssr
})
2017-05-11 10:59:08 +00:00
}
2018-02-07 11:58:48 +00:00
addLayout(template, name) {
const { dst, src } = this.addTemplate(template)
2018-02-14 19:31:11 +00:00
// Add to nuxt layouts
2018-02-14 19:32:41 +00:00
this.options.layouts[name || path.parse(src).name] = `./${dst}`
2018-02-07 11:58:48 +00:00
}
2017-10-30 17:41:22 +00:00
addServerMiddleware(middleware) {
2017-05-19 09:14:24 +00:00
this.options.serverMiddleware.push(middleware)
}
2017-10-30 17:41:22 +00:00
extendBuild(fn) {
2017-05-12 07:57:24 +00:00
this.options.build.extend = chainFn(this.options.build.extend, fn)
}
2017-10-30 17:41:22 +00:00
extendRoutes(fn) {
2018-01-11 13:28:45 +00:00
this.options.router.extendRoutes = chainFn(
this.options.router.extendRoutes,
fn
)
2017-05-11 10:59:08 +00:00
}
2017-10-30 17:41:22 +00:00
requireModule(moduleOpts) {
2018-01-11 13:28:45 +00:00
return this.addModule(moduleOpts, true /* require once */)
}
2017-10-30 17:41:22 +00:00
async addModule(moduleOpts, requireOnce) {
2018-01-11 13:28:45 +00:00
let src
let options
let handler
// Type 1: String
if (typeof moduleOpts === 'string') {
src = moduleOpts
2018-01-11 15:13:52 +00:00
} else if (Array.isArray(moduleOpts)) {
// Type 2: Babel style array
2018-01-11 13:28:45 +00:00
src = moduleOpts[0]
options = moduleOpts[1]
2018-01-11 15:13:52 +00:00
} else if (typeof moduleOpts === 'object') {
// Type 3: Pure object
2018-01-11 13:28:45 +00:00
src = moduleOpts.src
options = moduleOpts.options
handler = moduleOpts.handler
}
2017-07-03 11:11:40 +00:00
2018-01-11 13:28:45 +00:00
// Resolve handler
if (!handler) {
2018-01-11 19:43:34 +00:00
handler = require(this.nuxt.resolvePath(src))
2017-05-11 10:59:08 +00:00
}
2017-06-13 22:09:03 +00:00
2018-01-11 13:28:45 +00:00
// Validate handler
2017-05-14 22:33:31 +00:00
/* istanbul ignore if */
2018-01-11 13:28:45 +00:00
if (typeof handler !== 'function') {
throw new Error('Module should export a function: ' + src)
2017-05-11 10:59:08 +00:00
}
2017-07-03 11:11:40 +00:00
2018-01-11 13:28:45 +00:00
// Resolve module meta
const key = (handler.meta && handler.meta.name) || handler.name || src
2017-10-30 21:39:08 +00:00
// Update requiredModules
if (typeof key === 'string') {
if (requireOnce && this.requiredModules[key]) {
2017-06-05 08:52:02 +00:00
return
}
2018-01-11 13:28:45 +00:00
this.requiredModules[key] = { src, options, handler }
2017-06-05 08:52:02 +00:00
}
2017-07-03 11:11:40 +00:00
// Default module options to empty object
if (options === undefined) {
options = {}
}
2018-01-11 13:28:45 +00:00
return new Promise((resolve, reject) => {
// Prepare callback
const cb = err => {
2018-01-11 15:13:52 +00:00
printWarn(
'Supporting callbacks is depricated and will be removed in next releases. Consider using async/await.',
src
2018-01-11 13:28:45 +00:00
)
2018-01-11 15:13:52 +00:00
2017-05-14 23:17:13 +00:00
/* istanbul ignore if */
2017-05-11 10:59:08 +00:00
if (err) {
return reject(err)
}
2017-10-30 21:39:08 +00:00
resolve()
2018-01-11 13:28:45 +00:00
}
// Call module with `this` context and pass options
const result = handler.call(this, options, cb)
// If module send back a promise
2018-01-11 13:28:45 +00:00
if (result && result.then) {
2018-01-11 15:13:52 +00:00
return resolve(result)
2017-05-11 19:04:50 +00:00
}
2018-01-11 13:28:45 +00:00
// If not expecting a callback but returns no promise (=synchronous)
2018-01-11 13:28:45 +00:00
if (handler.length < 2) {
2017-10-30 21:39:08 +00:00
return resolve()
}
2017-05-11 10:59:08 +00:00
})
2017-05-11 10:04:15 +00:00
}
}