Skip to content

Commit 8b0adcb

Browse files
committed
feat: refine 'resolvePlugin' and 'resolveScopePackage' and update test
1 parent 9424df3 commit 8b0adcb

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

__mocks__/@org/vuepress-plugin-a.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = '@org/vuepress-plugin-a'

__mocks__/@org/vuepress-plugin-b.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = '@org/vuepress-plugin-b'

__mocks__/vuepress-plugin-a.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'vuepress-plugin-a'

__mocks__/vuepress-plugin-b.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'vuepress-plugin-b'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
jest.mock('vuepress-plugin-a')
2+
jest.mock('@org/vuepress-plugin-a')
3+
4+
const {
5+
resolvePlugin,
6+
resolveScopePackage
7+
} = require('../../lib/plugin-api/util')
8+
9+
// const Plugin = require('../../lib/plugin-api/index')
10+
11+
describe('resolvePlugin', () => {
12+
test('should resolve scope packages correctly', () => {
13+
const pkg1 = resolveScopePackage('@vuepress/plugin-a')
14+
expect(pkg1.org).toBe('vuepress')
15+
expect(pkg1.name).toBe('plugin-a')
16+
const pkg2 = resolveScopePackage('vuepress/plugin-a')
17+
expect(pkg2).toBe(null)
18+
const pkg3 = resolveScopePackage('vuepress-plugin-a')
19+
expect(pkg3).toBe(null)
20+
})
21+
22+
test('shoould return raw when function or object is given', () => {
23+
const plugin1 = () => {}
24+
const plugin2 = {}
25+
expect(resolvePlugin(plugin1)).toBe(plugin1)
26+
expect(resolvePlugin(plugin2)).toBe(plugin2)
27+
})
28+
29+
// https://jestjs.io/docs/en/manual-mocks#mocking-node-modules
30+
test('shoould resolve fullname correctly', () => {
31+
expect(resolvePlugin('vuepress-plugin-a')).toBe('vuepress-plugin-a')
32+
expect(resolvePlugin('@org/vuepress-plugin-a')).toBe('@org/vuepress-plugin-a')
33+
})
34+
35+
// https://jestjs.io/docs/en/manual-mocks#mocking-node-modules
36+
test('shoould resolve shortcut correctly', () => {
37+
expect(resolvePlugin('a')).toBe('vuepress-plugin-a')
38+
expect(resolvePlugin('@org/a')).toBe('@org/vuepress-plugin-a')
39+
// special shortcut for vuepress
40+
expect(resolvePlugin('@vuepress/a')).toBe('@vuepress/plugin-a')
41+
})
42+
})

packages/@vuepress/core/lib/plugin-api/util.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
const logger = require('../util/logger')
22
const chalk = require('chalk')
33

4+
const SCOPE_PACKAGE_RE = /^@(.*)\/(.*)/
5+
6+
exports.resolveScopePackage = function (name) {
7+
if (SCOPE_PACKAGE_RE.test(name)) {
8+
return {
9+
org: RegExp.$1,
10+
name: RegExp.$2
11+
}
12+
}
13+
return null
14+
}
15+
416
exports.resolvePlugin = function (pluginRaw) {
517
if (typeof pluginRaw === 'function' || typeof pluginRaw === 'object') {
618
return pluginRaw
719
}
820
if (typeof pluginRaw === 'string') {
921
try {
10-
return require(pluginRaw.startsWith('vuepress-plugin-')
11-
? pluginRaw
12-
: `vuepress-plugin-${pluginRaw}`
13-
)
22+
return require(pluginRaw.startsWith('vuepress-plugin-') ? pluginRaw : `vuepress-plugin-${pluginRaw}`)
1423
} catch (err) {
24+
const pkg = exports.resolveScopePackage(pluginRaw)
1525
try {
16-
return require(pluginRaw.startsWith('@vuepress/plugin-')
17-
? pluginRaw
18-
: `@vuepress/plugin-${pluginRaw}`
19-
)
26+
if (pkg) {
27+
if (pkg.org === 'vuepress') {
28+
return require(pkg.name.startsWith('plugin-') ? pluginRaw : `@vuepress/plugin-${pkg.name}`)
29+
} else {
30+
return require(pkg.name.startsWith('vuepress-plugin-') ? pluginRaw : `@${pkg.org}/vuepress-plugin-${pkg.name}`)
31+
}
32+
} else {
33+
throw new Error(`[vuepress] Cannot resolve ${pluginRaw}.`)
34+
}
2035
} catch (err2) {
2136
console.error(chalk.red(logger.error(`\n[vuepress] Cannot resolve plugin: ${pluginRaw}\n`, false)))
2237
throw new Error(err2)

0 commit comments

Comments
 (0)