forked from Outlander-Game-Studios/soulforged-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-all.js
62 lines (54 loc) · 1.79 KB
/
load-all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Vue from "vue";
const components = {};
const allPlugins = {};
if (typeof require.context === "undefined") {
require.context = () => ({
keys: () => [],
});
}
const requireComponents = require.context("./src/components/", true, /\.vue$/);
requireComponents.keys().forEach((fileName) => {
const componentName = fileName
.split("/")
.pop()
.replace(/\.\w+$/, "");
if (components[componentName]) {
throw new Error(
`Duplicate component definition found: ${componentName}, found in:\n${fileName}\n${components[componentName]}`
);
}
components[componentName] = fileName;
Vue.component(componentName, requireComponents(fileName).default);
});
const pluginSources = require.context("./src/plugins/", true, /\.vue$/);
const pluginDefinitions = require.context(
"./src/plugins/",
true,
/\/spec\.json$/
);
pluginDefinitions.keys().forEach((fileName) => {
const spec = pluginDefinitions(fileName);
const pluginPath = fileName.replace("spec.json", "");
spec.id = pluginPath.replace(/^.\//, "").replace(/\/$/, "");
if (allPlugins[spec.name]) {
spec.error = `Conflicting plugin name: ${spec.name}, found in:\n${fileName}`;
} else {
spec.componentNames = {};
Object.keys(spec.additions).forEach((slot) => {
const file = spec.additions[slot];
try {
const componentName = `PLUGIN_${spec.id}___${slot}`.replace(/-/g, "_");
spec.componentNames[slot] = componentName;
Vue.component(
componentName,
pluginSources(`${pluginPath}${file}`).default
);
} catch (e) {
spec.error = `Error in ${file}: ${e.toString()}`;
console.warn(`Failed to load plugin ${fileName}: ${spec.error}`);
}
});
}
allPlugins[spec.name] = spec;
});
global.allPlugins = Object.values(allPlugins);