From 5039459f061e621cba2265feedb807cac0527e47 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 11 May 2017 15:29:08 +0430 Subject: [PATCH] basic API ported from nuxt-modules --- lib/module.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/lib/module.js b/lib/module.js index 54ff424059..b2303eb36a 100755 --- a/lib/module.js +++ b/lib/module.js @@ -1,8 +1,98 @@ 'use strict' +import path from 'path' +import fs from 'fs' +import {uniq} from 'lodash' + class Module { constructor (nuxt) { this.nuxt = nuxt + this.options = nuxt.options + } + + addVendor (vendor) { + if (!vendor) { + return + } + this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor)) + } + + addPlugin (plugin) { + if (!plugin) { + return + } + const ssr = Boolean(plugin.ssr) + const copyOnly = Boolean(plugin.copyOnly) + const src = plugin.src || plugin + if (!src || typeof src !== 'string' || !fs.existsSync(src)) { + // eslint-disable-next-line no-console + console.warn('[Nuxt] invalid plugin', plugin) + return + } + // Copy plugin to project + const fileName = path.basename(src) + // TODO: Build removes this? + const dst = path.resolve(this.nuxt.rootDir, '.nuxt', fileName) + fs.copySync(src, dst) + // Add to nuxt plugins + if (!copyOnly) { + this.options.plugins.push({src: dst, ssr}) + } + } + + 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 || {} + const src = moduleOpts.src || moduleOpts + // Resolve module + let module + try { + if (typeof src === 'string') { + // Using ~ shorthand modules are resolved from project srcDir + if (src.indexOf('~') === 0) { + module = require(path.resolve(this.options.srcDir, src.substr(1))) + } else { + module = require(src) + } + } + } catch (e) { + // eslint-disable-next-line no-console + console.error('[Nuxt] Unable to resolve module', module) + // 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) => { + return module.apply(this, options, err => { + if (err) { + return reject(err) + } + resolve(module) + }) + }) } }