Skip to content

Instantly share code, notes, and snippets.

@wietseva
Created February 17, 2018 15:43
Show Gist options
  • Save wietseva/ce824e3b09504131055e76a550e918ea to your computer and use it in GitHub Desktop.
Save wietseva/ce824e3b09504131055e76a550e918ea to your computer and use it in GitHub Desktop.
Extending pluginOptions in vue-cli plugin generator
const fs = require('fs')
const isObject = val => val && typeof val === 'object'
let pluginOptions = null
module.exports = (api, cb) => {
// Look up pluginOptions from the vue.config.js file
// only do this once when its not yet defined
const vueConfig = api.resolve('vue.config.js')
if (!pluginOptions && fs.existsSync(vueConfig)) {
let projectOptions = require(vueConfig)
if (isObject(projectOptions.pluginOptions)) {
pluginOptions = projectOptions.pluginOptions
} else {
pluginOptions = {}
}
}
api.extendPackage((pkg) => {
// extend pluginOptions
// with those already defined in the pkg
if (isObject(pkg.vue) && isObject(pkg.vue.pluginOptions)) {
Object.assign(pluginOptions, pkg.vue.pluginOptions)
}
pluginOptions = cb(pluginOptions)
return {
vue: { pluginOptions }
}
})
}
@wietseva
Copy link
Author

How to use

Import it in generator.js

const extendPluginOptions = require('./extendPluginOptions.js')

Call it with generator api

module.exports = (api, options) => {
  // modify plugin options
  extendPluginOptions(api, pluginOptions => {
    plugoptions.newOption = "some new option"   
    return pluginOptions
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment