2020-02-19 14:58:06 +01:00
|
|
|
import { flatten, values, uniq, map } from "lodash/fp"
|
|
|
|
import { pipe } from "../common/core"
|
|
|
|
|
2019-08-19 22:18:23 +02:00
|
|
|
export const loadLibs = async (appName, appPackage) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const allLibraries = {}
|
2020-02-19 14:58:06 +01:00
|
|
|
|
|
|
|
for (let lib of libsFromPages(appPackage.pages)) {
|
2020-02-03 10:24:25 +01:00
|
|
|
const libModule = await import(makeLibraryUrl(appName, lib))
|
|
|
|
allLibraries[lib] = libModule
|
|
|
|
}
|
2019-08-19 22:18:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return allLibraries
|
2019-08-19 22:18:23 +02:00
|
|
|
}
|
2019-08-27 08:32:56 +02:00
|
|
|
|
2019-09-22 06:02:33 +02:00
|
|
|
export const loadLibUrls = (appName, appPackage) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const allLibraries = []
|
2020-02-19 14:58:06 +01:00
|
|
|
for (let lib of libsFromPages(appPackage.pages)) {
|
2020-02-03 10:24:25 +01:00
|
|
|
const libUrl = makeLibraryUrl(appName, lib)
|
|
|
|
allLibraries.push({ libName: lib, importPath: libUrl })
|
|
|
|
}
|
2019-09-22 06:02:33 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return allLibraries
|
2019-09-22 06:02:33 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 08:32:56 +02:00
|
|
|
export const loadLib = async (appName, lib, allLibs) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
allLibs[lib] = await import(makeLibraryUrl(appName, lib))
|
|
|
|
return allLibs
|
2019-08-27 08:32:56 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
export const makeLibraryUrl = (appName, lib) =>
|
|
|
|
`/_builder/${appName}/componentlibrary?lib=${encodeURI(lib)}`
|
2020-02-19 14:58:06 +01:00
|
|
|
|
2020-02-25 16:21:23 +01:00
|
|
|
export const libsFromPages = pages =>
|
|
|
|
pipe(pages, [values, map(p => p.componentLibraries), flatten, uniq])
|
2020-03-11 17:42:19 +01:00
|
|
|
|
|
|
|
export const libUrlsForPreview = (appPackage, pageName) => {
|
|
|
|
const resolve = path => {
|
|
|
|
let file = appPackage.components.libraryPaths[path]
|
|
|
|
if (file.startsWith("./")) file = file.substring(2)
|
|
|
|
if (file.startsWith("/")) file = file.substring(1)
|
|
|
|
|
|
|
|
let newPath = path
|
|
|
|
|
|
|
|
if (!newPath.startsWith("./") && !newPath.startsWith("/")) {
|
|
|
|
newPath = `/node_modules/${path}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
importPath: `/lib${newPath}/${file}`,
|
|
|
|
libName: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipe([appPackage.pages[pageName]], [libsFromPages, map(resolve)])
|
|
|
|
}
|