Skip to content

Commit 5df5427

Browse files
author
Guillaume Chau
committed
feat(ui): serve static files in plugin ui-public folders + support custom icons in routes
1 parent 9d8dc0b commit 5df5427

File tree

5 files changed

+58
-13
lines changed

5 files changed

+58
-13
lines changed

packages/@vue/cli-ui/src/components/ProjectNavButton.vue

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
<VueGroupButton
1212
class="flat big icon-button"
1313
:value="route.name"
14-
:icon-left="route.icon"
15-
/>
14+
:icon-left="!imageIcon && route.icon"
15+
>
16+
<img
17+
v-if="imageIcon"
18+
:src="route.icon"
19+
class="image-icon"
20+
>
21+
</VueGroupButton>
1622

1723
<template slot="popover">
18-
<div class="title">{{ $t(route.tooltip) }}</div>
24+
<div class="title">{{ $t(route.tooltip) }}</div>
1925

2026
<div v-if="badges" class="badges">
2127
<RouteBadge
@@ -55,6 +61,10 @@ export default {
5561
5662
firstNotHiddenBadge () {
5763
return this.badges && this.badges.find(b => !b.hidden)
64+
},
65+
66+
imageIcon () {
67+
return this.route.icon && this.route.icon.indexOf('.') !== -1
5868
}
5969
}
6070
}
@@ -101,6 +111,16 @@ $bg = darken($vue-ui-color-dark, 70%)
101111
.bullet
102112
border-color darken($bg, 8%)
103113
114+
.icon-button
115+
padding-left 0
116+
padding-right @padding-left
117+
h-box()
118+
box-center()
119+
120+
.image-icon
121+
max-width 24px
122+
max-height @width
123+
104124
.badges
105125
margin ($padding-item/2) 0
106126
display grid

packages/@vue/cli-ui/src/graphql-api/connectors/client-addons.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const path = require('path')
22
// Subs
33
const channels = require('../channels')
4+
// Utils
5+
const { getBasePath } = require('../utils/serve')
46

57
let addons = []
68

@@ -37,14 +39,6 @@ function getUrl (addon, context) {
3739
return addon.url || `${baseUrl}/_addon/${addon.id}/index.js`
3840
}
3941

40-
function getBasePath (filePath) {
41-
const index = filePath.lastIndexOf('/index.js')
42-
if (index !== -1) {
43-
return filePath.substr(0, index)
44-
}
45-
return filePath
46-
}
47-
4842
function serve (req, res) {
4943
const { id, 0: file } = req.params
5044
const addon = findOne(id)

packages/@vue/cli-ui/src/graphql-api/connectors/plugins.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const routes = require('./routes')
3131
const PluginApi = require('../api/PluginApi')
3232
// Utils
3333
const { getCommand } = require('../utils/command')
34+
const { getBasePath } = require('../utils/serve')
3435

3536
const PROGRESS_ID = 'plugin-installation'
3637

@@ -344,6 +345,18 @@ async function callAction ({ id, params }, context) {
344345
return { id, params, results, errors }
345346
}
346347

348+
function serve (req, res) {
349+
const { id, 0: file } = req.params
350+
const basePath = getBasePath(require.resolve(id), id)
351+
if (basePath) {
352+
res.sendFile(path.join(basePath, 'ui-public', file))
353+
return
354+
}
355+
356+
res.status(404)
357+
res.send(`Addon ${id} not found in loaded addons. Try opening a vue-cli project first?`)
358+
}
359+
347360
module.exports = {
348361
list,
349362
findOne,
@@ -358,5 +371,6 @@ module.exports = {
358371
resetPluginApi,
359372
getApi,
360373
finishInstall,
361-
callAction
374+
callAction,
375+
serve
362376
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
const path = require('path')
22
const express = require('express')
33
const fallback = require('express-history-api-fallback')
4-
4+
// Connectors
55
const clientAddons = require('./connectors/client-addons')
6+
const plugins = require('./connectors/plugins')
67

78
const distPath = path.resolve(__dirname, '../../dist')
89

910
module.exports = app => {
1011
app.use(express.static(distPath))
12+
app.use('/_plugin/:id/*', plugins.serve)
1113
app.use('/_addon/:id/*', clientAddons.serve)
1214
app.use(fallback(path.join(distPath, 'index.html')))
1315
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
exports.getBasePath = function (filePath, id = null) {
2+
{
3+
const index = filePath.lastIndexOf('/index.js')
4+
if (index !== -1) {
5+
filePath = filePath.substr(0, index)
6+
}
7+
}
8+
if (id) {
9+
const index = filePath.lastIndexOf(id)
10+
if (index !== -1) {
11+
filePath = filePath.substr(0, index + id.length)
12+
}
13+
}
14+
return filePath
15+
}

0 commit comments

Comments
 (0)