Created
February 17, 2018 15:43
-
-
Save wietseva/ce824e3b09504131055e76a550e918ea to your computer and use it in GitHub Desktop.
Extending pluginOptions in vue-cli plugin generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use
Import it in
generator.js
Call it with generator api