mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-05-06 20:09:14 -05:00
refactor: use pluginResolution utils in more places, add tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const path = require('path')
|
||||
const { matchesPluginId } = require('@vue/cli-shared-utils')
|
||||
|
||||
class PluginAPI {
|
||||
/**
|
||||
@@ -27,10 +28,7 @@ class PluginAPI {
|
||||
* @return {boolean}
|
||||
*/
|
||||
hasPlugin (id) {
|
||||
const prefixRE = /^(@vue\/|vue-)cli-plugin-/
|
||||
return this.service.plugins.some(p => {
|
||||
return p.id === id || p.id.replace(prefixRE, '') === id
|
||||
})
|
||||
return this.service.plugins.some(p => matchesPluginId(id, p.id))
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
const {
|
||||
isPlugin,
|
||||
isOfficialPlugin,
|
||||
toShortPluginId,
|
||||
resolvePluginId,
|
||||
matchesPluginId
|
||||
} = require('../lib/pluginResolution')
|
||||
|
||||
test('isPlugin', () => {
|
||||
expect(isPlugin('foobar')).toBe(false)
|
||||
expect(isPlugin('@vue/cli-plugin-foo')).toBe(true)
|
||||
expect(isPlugin('vue-cli-plugin-foo')).toBe(true)
|
||||
expect(isPlugin('@foo/vue-cli-plugin-foo')).toBe(true)
|
||||
})
|
||||
|
||||
test('isOfficialPlugin', () => {
|
||||
expect(isOfficialPlugin('@vue/foo')).toBe(false)
|
||||
expect(isOfficialPlugin('@vue/cli-plugin-foo')).toBe(true)
|
||||
expect(isOfficialPlugin('vue-cli-plugin-foo')).toBe(false)
|
||||
expect(isOfficialPlugin('@foo/vue-cli-plugin-foo')).toBe(false)
|
||||
})
|
||||
|
||||
test('toShortPluginId', () => {
|
||||
expect(toShortPluginId('@vue/cli-plugin-foo')).toBe('foo')
|
||||
expect(toShortPluginId('vue-cli-plugin-foo')).toBe('foo')
|
||||
expect(toShortPluginId('@foo/vue-cli-plugin-foo')).toBe('foo')
|
||||
})
|
||||
|
||||
test('resolvePluginId', () => {
|
||||
// already full
|
||||
expect(resolvePluginId('@vue/cli-plugin-foo')).toBe('@vue/cli-plugin-foo')
|
||||
expect(resolvePluginId('vue-cli-plugin-foo')).toBe('vue-cli-plugin-foo')
|
||||
expect(resolvePluginId('@foo/vue-cli-plugin-foo')).toBe('@foo/vue-cli-plugin-foo')
|
||||
|
||||
// scoped short
|
||||
expect(resolvePluginId('@vue/foo')).toBe('@vue/cli-plugin-foo')
|
||||
expect(resolvePluginId('@foo/foo')).toBe('@foo/vue-cli-plugin-foo')
|
||||
|
||||
// default short
|
||||
expect(resolvePluginId('foo')).toBe('vue-cli-plugin-foo')
|
||||
})
|
||||
|
||||
test('matchesPluginId', () => {
|
||||
// full
|
||||
expect(matchesPluginId('@vue/cli-plugin-foo', '@vue/cli-plugin-foo')).toBe(true)
|
||||
expect(matchesPluginId('vue-cli-plugin-foo', 'vue-cli-plugin-foo')).toBe(true)
|
||||
expect(matchesPluginId('@foo/vue-cli-plugin-foo', '@foo/vue-cli-plugin-foo')).toBe(true)
|
||||
|
||||
// short without scope
|
||||
expect(matchesPluginId('foo', '@vue/cli-plugin-foo')).toBe(true)
|
||||
expect(matchesPluginId('foo', 'vue-cli-plugin-foo')).toBe(true)
|
||||
expect(matchesPluginId('foo', '@foo/vue-cli-plugin-foo')).toBe(true)
|
||||
|
||||
// short with scope
|
||||
expect(matchesPluginId('@vue/foo', '@vue/cli-plugin-foo')).toBe(true)
|
||||
expect(matchesPluginId('@foo/foo', '@foo/vue-cli-plugin-foo')).toBe(true)
|
||||
})
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
const pluginRE = /^(@vue\/|vue-|@[\w-]+\/vue-)cli-plugin-/
|
||||
const scopeRE = /^@[\w-]+\//
|
||||
const officialRE = /^@vue\//
|
||||
|
||||
exports.isPlugin = id => pluginRE.test(id)
|
||||
|
||||
exports.isOfficialPlugin = id => /^@vue\//.test(id)
|
||||
exports.isOfficialPlugin = id => exports.isPlugin(id) && officialRE.test(id)
|
||||
|
||||
exports.toShortPluginId = id => id.replace(pluginRE, '')
|
||||
|
||||
@@ -18,11 +19,24 @@ exports.resolvePluginId = id => {
|
||||
if (id.charAt(0) === '@') {
|
||||
const scopeMatch = id.match(scopeRE)
|
||||
if (scopeMatch) {
|
||||
const scope = scopeMatch[0]
|
||||
const shortId = id.replace(scopeRE, '')
|
||||
return `${scopeMatch[0]}vue-cli-plugin-${shortId}`
|
||||
return `${scope}${scope === '@vue/' ? `` : `vue-`}cli-plugin-${shortId}`
|
||||
}
|
||||
}
|
||||
// default short
|
||||
// e.g. foo
|
||||
return `vue-cli-plugin-${id}`
|
||||
}
|
||||
|
||||
exports.matchesPluginId = (input, full) => {
|
||||
const short = full.replace(pluginRE, '')
|
||||
return (
|
||||
// input is full
|
||||
full === input ||
|
||||
// input is short without scope
|
||||
short === input ||
|
||||
// input is short with scope
|
||||
short === input.replace(scopeRE, '')
|
||||
)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const GeneratorAPI = require('./GeneratorAPI')
|
||||
const sortObject = require('./util/sortObject')
|
||||
const writeFileTree = require('./util/writeFileTree')
|
||||
const configTransforms = require('./util/configTransforms')
|
||||
const { toShortPluginId } = require('@vue/cli-shared-utils')
|
||||
const { toShortPluginId, matchesPluginId } = require('@vue/cli-shared-utils')
|
||||
|
||||
const logger = require('@vue/cli-shared-utils/lib/logger')
|
||||
const logTypes = {
|
||||
@@ -138,14 +138,11 @@ module.exports = class Generator {
|
||||
}
|
||||
|
||||
hasPlugin (_id) {
|
||||
const prefixRE = /^(@vue\/|vue-)cli-plugin-/
|
||||
return [
|
||||
...this.plugins.map(p => p.id),
|
||||
...Object.keys(this.pkg.devDependencies || {}),
|
||||
...Object.keys(this.pkg.dependencies || {})
|
||||
].some(id => {
|
||||
return id === _id || id.replace(prefixRE, '') === _id
|
||||
})
|
||||
].some(id => matchesPluginId(_id, id))
|
||||
}
|
||||
|
||||
printExitLogs () {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const chalk = require('chalk')
|
||||
const { toShortPluginId } = require('@vue/cli-shared-utils')
|
||||
|
||||
module.exports = function formatFeatures (preset, lead, joiner) {
|
||||
const features = []
|
||||
@@ -16,7 +17,7 @@ module.exports = function formatFeatures (preset, lead, joiner) {
|
||||
})
|
||||
features.push.apply(features, plugins)
|
||||
return features.map(dep => {
|
||||
dep = dep.replace(/^(@vue\/|vue-)cli-plugin-/, '')
|
||||
dep = toShortPluginId(dep)
|
||||
return `${lead || ''}${chalk.yellow(dep)}`
|
||||
}).join(joiner || ', ')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user