Skip to content

Commit eda4ce8

Browse files
author
Guillaume Chau
committed
refactor(ui): use package.json 'vuePlugins.ui' option
1 parent 951e17e commit eda4ce8

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

docs/dev-guide/ui-api.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The cli-ui exposes an API that allows augmenting the project configurations and
66

77
## UI files
88

9-
Inside each installed vue-cli plugins, the cli-ui will try to load an optional `ui.js` file in the root folder of the plugin. It will also try to load a `vue-cli-ui.js` file in the user project root so the UI can be manually extended on a per-project basis (also useful to quickly prototype a plugin). Note that you can also use folders (for example `ui/index.js`).
9+
Inside each installed vue-cli plugins, the cli-ui will try to load an optional `ui.js` file in the root folder of the plugin. Note that you can also use folders (for example `ui/index.js`).
1010

1111
The file should export a function which gets the api object as argument:
1212

@@ -30,6 +30,20 @@ Here is an example folder structure for a vue-cli plugin using the UI API:
3030
- logo.png
3131
```
3232

33+
### Project local plugins
34+
35+
If you need access to the plugin API in your project and don't want to create a full plugin for it, you can use the `vuePlugins.ui` option in your `package.json` file:
36+
37+
```json
38+
{
39+
"vuePlugins": {
40+
"ui": ["my-ui.js"]
41+
}
42+
}
43+
```
44+
45+
Each file will need to export a function taking the plugin API as the first argument.
46+
3347
## Dev mode
3448

3549
While building your plugin, you may want to run the cli-ui in Dev mode, so it will output useful logs to you:

docs/guide/plugins-and-presets.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ If you need access to the plugin API in your project and don't want to create a
8989

9090
Each file will need to export a function taking the plugin API as the first argument. For more information about the plugin API, check out the [Plugin Development Guide](../dev-guide/plugin-dev.md).
9191

92-
You can also create a `vue-cli-ui.js` file that will behave like a UI plugin. For more information, read the [UI Plugin API](../dev-guide/ui-api.md).
92+
You can also add files that will behave like UI plugins with the `vuePlugins.ui` option:
93+
94+
```json
95+
{
96+
"vuePlugins": {
97+
"ui": ["my-ui.js"]
98+
}
99+
}
100+
```
101+
102+
For more information, read the [UI Plugin API](../dev-guide/ui-api.md).
93103

94104
## Presets
95105

packages/@vue/cli-ui/apollo-server/connectors/plugins.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@ let eventsInstalled = false
5555
let installationStep
5656
let pluginsStore = new Map()
5757
let pluginApiInstances = new Map()
58+
let pkgStore = new Map()
5859

5960
async function list (file, context, { resetApi = true, lightApi = false, autoLoadApi = true } = {}) {
60-
const pkg = folders.readPackage(file, context)
61+
let pkg = folders.readPackage(file, context)
62+
let pkgContext = cwd.get()
63+
// Custom package.json location
64+
if (pkg.vuePlugins && pkg.vuePlugins.resolveFrom) {
65+
pkgContext = path.resolve(cwd.get(), pkg.vuePlugins.resolveFrom)
66+
pkg = folders.readPackage(pkgContext, context)
67+
}
68+
pkgStore.set(file, { pkgContext, pkg })
69+
6170
let plugins = []
6271
plugins = plugins.concat(findPlugins(pkg.devDependencies || {}, file))
6372
plugins = plugins.concat(findPlugins(pkg.dependencies || {}, file))
@@ -156,7 +165,16 @@ function resetPluginApi ({ file, lightApi }, context) {
156165
// Run Plugin API
157166
runPluginApi(path.resolve(__dirname, '../../'), pluginApi, context, 'ui-defaults')
158167
plugins.forEach(plugin => runPluginApi(plugin.id, pluginApi, context))
159-
runPluginApi(cwd.get(), pluginApi, context, 'vue-cli-ui')
168+
// Local plugins
169+
const { pkg, pkgContext } = pkgStore.get(file)
170+
if (pkg.vuePlugins && pkg.vuePlugins.ui) {
171+
const files = pkg.vuePlugins.ui
172+
if (Array.isArray(files)) {
173+
for (const file of files) {
174+
runPluginApi(pkgContext, pluginApi, context, file)
175+
}
176+
}
177+
}
160178
// Add client addons
161179
pluginApi.clientAddons.forEach(options => clientAddons.add(options, context))
162180
// Add views
@@ -190,20 +208,25 @@ function resetPluginApi ({ file, lightApi }, context) {
190208
})
191209
}
192210

193-
function runPluginApi (id, pluginApi, context, fileName = 'ui') {
211+
function runPluginApi (id, pluginApi, context, filename = 'ui') {
194212
let module
195213
try {
196-
module = loadModule(`${id}/${fileName}`, pluginApi.cwd, true)
214+
module = loadModule(`${id}/${filename}`, pluginApi.cwd, true)
197215
} catch (e) {
198216
if (process.env.VUE_CLI_DEBUG) {
199217
console.error(e)
200218
}
201219
}
202220
if (module) {
203-
pluginApi.pluginId = id
204-
module(pluginApi)
205-
log('Plugin API loaded for', id, chalk.grey(pluginApi.cwd))
206-
pluginApi.pluginId = null
221+
if (typeof module !== 'function') {
222+
log(`${chalk.red('ERROR')} while loading plugin API: no function exported, for`, filename !== 'ui' ? `${id}/${filename}` : id, chalk.grey(pluginApi.cwd))
223+
} else {
224+
const name = filename !== 'ui' ? `${id}/${filename}` : id
225+
log('Plugin API loaded for', name, chalk.grey(pluginApi.cwd))
226+
pluginApi.pluginId = id
227+
module(pluginApi)
228+
pluginApi.pluginId = null
229+
}
207230
}
208231

209232
// Locales

packages/@vue/cli-ui/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,8 @@
110110
"vue-cli-service lint",
111111
"git add"
112112
]
113+
},
114+
"vuePlugins": {
115+
"ui": ["ui-dev.js"]
113116
}
114117
}
File renamed without changes.

0 commit comments

Comments
 (0)