UNPKG

1.98 kBJavaScriptView Raw
1import createFindPanel from './create-find-panel'
2import isRequireable from './is-requireable'
3import loadResource from './load-resource'
4
5async function loadModule(app) {
6 let module = {}
7
8 try {
9 const response = await fetch(`//${app}/panels.json`)
10 const data = await response.json()
11
12 if (data.module) {
13 module = data.module
14 }
15
16 await loadResource(data.logic)
17 } catch (err) {
18 console.error(err)
19 if (err instanceof SyntaxError) {
20 throw new Error(
21 `We can't load ${app}.
22 We can't find your app's logic source.
23
24 If you're inlining your app with Panels, make sure the script tag is inserted after panels
25 and that the required export matches ${app}.
26
27 If your app is expected to be fetched from a remote source, make sure that
28 '//${app}/panels.json' in place and that it at least includes the logic key pointing to
29 your application's JS logic URI.
30
31 {
32 "logic": "https://panels.com/my-application-logic.js"
33 }`
34 )
35 }
36 }
37
38 return module
39}
40
41const DENY = () => false
42
43export default async function get(app, createContext) {
44 let data
45 let name = app
46 let props = {}
47
48 if (!isRequireable(name)) {
49 const inline = window.panelsJson && window.panelsJson[name]
50 if (inline) {
51 data = inline.module
52
53 if (!isRequireable(data.name)) {
54 console.error(`You should embed your app's panels bundle script tag`)
55 }
56 }
57
58 if (!data) {
59 data = await loadModule(app)
60 }
61
62 if (data.name) {
63 name = data.name
64 }
65 if (data.props) {
66 props = data.props
67 }
68 }
69
70 /* eslint-disable global-require */
71 const { access = DENY, notify, lookup, panels, setup, types } = require(name)
72
73 const context = createContext(app, name)
74
75 return {
76 access,
77 findPanel: createFindPanel(panels, lookup),
78 name,
79 notify,
80 store: typeof setup === 'function' && (await setup(app, props, context)),
81 types,
82 }
83}