Plugins: Don't auto prepend app sub url to plugin asset paths (#81658)
* don't prepend app sub url to paths * simplify logo path * fix(plugins): dynamically prepend appSubUrl for System module resolving to work * fix(sandbox): support dynamic appSuburl prepend when loading plugin module.js * fix tests * update test name * fix tests * update fe + add some tests * refactor(plugins): move wrangleurl to utils, rename to resolveModulePath, update usage * chore: fix a typo * test(plugins): add missing name to utils test * reset test flag --------- Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
This commit is contained in:
co-authored by
Jack Westbrook
parent
18963dc3ae
commit
99feb928cf
@@ -33,7 +33,7 @@ func DefaultDecorateFuncs(cfg *config.Cfg) []DecorateFunc {
|
||||
return []DecorateFunc{
|
||||
AppDefaultNavURLDecorateFunc,
|
||||
TemplateDecorateFunc,
|
||||
AppChildDecorateFunc(cfg),
|
||||
AppChildDecorateFunc(),
|
||||
SkipHostEnvVarsDecorateFunc(cfg),
|
||||
}
|
||||
}
|
||||
@@ -133,27 +133,28 @@ func setDefaultNavURL(p *plugins.Plugin) {
|
||||
}
|
||||
|
||||
// AppChildDecorateFunc is a DecorateFunc that configures child plugins of app plugins.
|
||||
func AppChildDecorateFunc(cfg *config.Cfg) DecorateFunc {
|
||||
func AppChildDecorateFunc() DecorateFunc {
|
||||
return func(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
if p.Parent != nil && p.Parent.IsApp() {
|
||||
configureAppChildPlugin(cfg, p.Parent, p)
|
||||
configureAppChildPlugin(p.Parent, p)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
func configureAppChildPlugin(cfg *config.Cfg, parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
func configureAppChildPlugin(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
if !parent.IsApp() {
|
||||
return
|
||||
}
|
||||
child.IncludedInAppID = parent.ID
|
||||
child.BaseURL = parent.BaseURL
|
||||
|
||||
// TODO move this logic within assetpath package
|
||||
appSubPath := strings.ReplaceAll(strings.Replace(child.FS.Base(), parent.FS.Base(), "", 1), "\\", "/")
|
||||
if parent.IsCorePlugin() {
|
||||
child.Module = path.Join("core:plugin", parent.ID, appSubPath)
|
||||
} else {
|
||||
child.Module = path.Join("/", cfg.GrafanaAppSubURL, "/public/plugins", parent.ID, appSubPath, "module.js")
|
||||
child.Module = path.Join("public/plugins", parent.ID, appSubPath, "module.js")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,25 +108,17 @@ func Test_configureAppChildPlugin(t *testing.T) {
|
||||
},
|
||||
Class: plugins.ClassCore,
|
||||
FS: fakes.NewFakePluginFiles("c:\\grafana\\public\\app\\plugins\\app\\testdata-app"),
|
||||
BaseURL: "/public/app/plugins/app/testdata-app",
|
||||
BaseURL: "public/app/plugins/app/testdata-app",
|
||||
}
|
||||
|
||||
configureAppChildPlugin(&config.Cfg{}, parent, child)
|
||||
configureAppChildPlugin(parent, child)
|
||||
|
||||
require.Equal(t, "core:plugin/testdata-app/datasources/datasource", child.Module)
|
||||
require.Equal(t, "testdata-app", child.IncludedInAppID)
|
||||
require.Equal(t, "/public/app/plugins/app/testdata-app", child.BaseURL)
|
||||
|
||||
t.Run("App sub URL has no effect on Core plugins", func(t *testing.T) {
|
||||
configureAppChildPlugin(&config.Cfg{GrafanaAppSubURL: "/grafana"}, parent, child)
|
||||
|
||||
require.Equal(t, "core:plugin/testdata-app/datasources/datasource", child.Module)
|
||||
require.Equal(t, "testdata-app", child.IncludedInAppID)
|
||||
require.Equal(t, "/public/app/plugins/app/testdata-app", child.BaseURL)
|
||||
})
|
||||
require.Equal(t, "public/app/plugins/app/testdata-app", child.BaseURL)
|
||||
})
|
||||
|
||||
t.Run("When setting paths based on external plugin with app sub URL", func(t *testing.T) {
|
||||
t.Run("When setting paths based on external plugin", func(t *testing.T) {
|
||||
child := &plugins.Plugin{
|
||||
FS: fakes.NewFakePluginFiles("/plugins/parent-app/child-panel"),
|
||||
}
|
||||
@@ -137,14 +129,14 @@ func Test_configureAppChildPlugin(t *testing.T) {
|
||||
},
|
||||
Class: plugins.ClassExternal,
|
||||
FS: fakes.NewFakePluginFiles("/plugins/parent-app"),
|
||||
BaseURL: "/grafana/plugins/parent-app",
|
||||
BaseURL: "plugins/parent-app",
|
||||
}
|
||||
|
||||
configureAppChildPlugin(&config.Cfg{GrafanaAppSubURL: "/grafana"}, parent, child)
|
||||
configureAppChildPlugin(parent, child)
|
||||
|
||||
require.Equal(t, "/grafana/public/plugins/testdata-app/child-panel/module.js", child.Module)
|
||||
require.Equal(t, "public/plugins/testdata-app/child-panel/module.js", child.Module)
|
||||
require.Equal(t, "testdata-app", child.IncludedInAppID)
|
||||
require.Equal(t, "/grafana/plugins/parent-app", child.BaseURL)
|
||||
require.Equal(t, "plugins/parent-app", child.BaseURL)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user