Skip to content

Commit

Permalink
refactor: extract plugin resolution into shared-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 4, 2018
1 parent 0f2ee80 commit edff5b4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/PluginAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PluginAPI {
/**
* Check if the project has a given plugin.
*
* @param {string} id - Plugin id, can omit the (@vue/|vue-)-cli-plugin- prefix
* @param {string} id - Plugin id, can omit the (@vue/|vue-|@scope/vue)-cli-plugin- prefix
* @return {boolean}
*/
hasPlugin (id) {
Expand Down
5 changes: 2 additions & 3 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Config = require('webpack-chain')
const PluginAPI = require('./PluginAPI')
const loadEnv = require('./util/loadEnv')
const defaultsDeep = require('lodash.defaultsdeep')
const { warn, error } = require('@vue/cli-shared-utils')
const { warn, error, isPlugin } = require('@vue/cli-shared-utils')

const { defaults, validate } = require('./options')

Expand Down Expand Up @@ -104,10 +104,9 @@ module.exports = class Service {
? builtInPlugins.concat(inlinePlugins)
: inlinePlugins
} else {
const prefixRE = /^(@vue\/|vue-)cli-plugin-/
const projectPlugins = Object.keys(this.pkg.dependencies || {})
.concat(Object.keys(this.pkg.devDependencies || {}))
.filter(p => prefixRE.test(p))
.filter(isPlugin)
.map(idToPlugin)
return builtInPlugins.concat(projectPlugins)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@vue/cli-shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
'logger',
'spinner',
'validate',
'openBrowser'
'openBrowser',
'pluginResolution'
].forEach(m => {
Object.assign(exports, require(`./lib/${m}`))
})
7 changes: 7 additions & 0 deletions packages/@vue/cli-shared-utils/lib/pluginResolution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const pluginRE = /^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/

exports.isPlugin = id => pluginRE.test(id)

exports.isOfficial = id => /^@vue\//.test(id)

exports.toShortId = id => id.replace(pluginRE, '')
4 changes: 2 additions & 2 deletions packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const debug = require('debug')
const GeneratorAPI = require('./GeneratorAPI')
const sortObject = require('./util/sortObject')
const writeFileTree = require('./util/writeFileTree')
const { toShortId } = require('@vue/cli-shared-utils')
const configTransforms = require('./util/configTransforms')
const logger = require('@vue/cli-shared-utils/lib/logger')
const { toShortId } = require('./util/pluginResolution')

const logger = require('@vue/cli-shared-utils/lib/logger')
const logTypes = {
log: logger.log,
info: logger.info,
Expand Down
8 changes: 4 additions & 4 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const globby = require('globby')
const isBinary = require('isbinaryfile')
const yaml = require('yaml-front-matter')
const mergeDeps = require('./util/mergeDeps')
const { isOfficial, toShortId } = require('@vue/cli-shared-utils')

const isString = val => typeof val === 'string'
const isFunction = val => typeof val === 'function'
Expand Down Expand Up @@ -39,11 +40,10 @@ class GeneratorAPI {
this.pluginsData = generator.plugins
.filter(({ id }) => id !== `@vue/cli-service`)
.map(({ id }) => {
const name = id.replace(/^(@vue|vue-)\/cli-plugin-/, '')
const isOfficial = /^@vue/.test(id)
const name = toShortId(id)
return {
name: name,
link: isOfficial
link: isOfficial(id)
? `https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-${name}`
: getLink(id)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ class GeneratorAPI {
/**
* Check if the project has a given plugin.
*
* @param {string} id - Plugin id, can omit the (@vue/|vue-)-cli-plugin- prefix
* @param {string} id - Plugin id, can omit the (@vue/|vue-|@scope/vue)-cli-plugin- prefix
* @return {boolean}
*/
hasPlugin (id) {
Expand Down
12 changes: 12 additions & 0 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
// attempt to locate the plugin in package.json
const findPlugin = deps => {
if (!deps) return

// custom scoped plugin
let name
if (pluginName.charAt(0) === '@') {
const scopeRE = /^@[\w-]+\//
const scopeMatch = pluginName.match(scopeRE)
const shortId = pluginName.replace(scopeRE, '')
if (scopeMatch && deps[name = `${scopeMatch[0]}vue-cli-plugin-${shortId}`]) {
return name
}
}

// official, non-scoped & full name
if (deps[name = `@vue/cli-plugin-${pluginName}`] ||
deps[name = `vue-cli-plugin-${pluginName}`] ||
deps[name = pluginName]) {
Expand Down
7 changes: 0 additions & 7 deletions packages/@vue/cli/lib/util/pluginResolution.js

This file was deleted.

0 comments on commit edff5b4

Please sign in to comment.