-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathui_bundler_env.js
84 lines (67 loc) · 2.37 KB
/
ui_bundler_env.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { fromRoot } from '../utils';
import { includes } from 'lodash';
const arr = v => [].concat(v || []);
export default class UiBundlerEnv {
constructor(workingDir) {
// the location that bundle entry files and all compiles files will
// be written
this.workingDir = workingDir;
// the context that the bundler is running in, this is not officially
// used for anything but it is serialized into the entry file to ensure
// that they are invalidated when the context changes
this.context = {};
// the plugins that are used to build this environment
// are tracked and embedded into the entry file so that when the
// environment changes we can rebuild the bundles
this.pluginInfo = [];
// regular expressions which will prevent webpack from parsing the file
this.noParse = [
/node_modules[\/\\](angular|elasticsearch-browser)[\/\\]/,
/node_modules[\/\\](mocha|moment)[\/\\]/
];
// webpack aliases, like require paths, mapping a prefix to a directory
this.aliases = {
ui: fromRoot('src/ui/public'),
test_harness: fromRoot('src/test_harness/public'),
querystring: 'querystring-browser',
};
// map of which plugins created which aliases
this.aliasOwners = {};
// loaders that are applied to webpack modules after all other processing
// NOTE: this is intentionally not exposed as a uiExport because it leaks
// too much of the webpack implementation to plugins, but is used by test_bundle
// core plugin to inject the instrumentation loader
this.postLoaders = [];
}
consumePlugin(plugin) {
const tag = `${plugin.id}@${plugin.version}`;
if (includes(this.pluginInfo, tag)) return;
if (plugin.publicDir) {
this.aliases[`plugins/${plugin.id}`] = plugin.publicDir;
}
this.pluginInfo.push(tag);
}
exportConsumer(type) {
switch (type) {
case 'noParse':
return (plugin, spec) => {
for (const re of arr(spec)) this.addNoParse(re);
};
case '__globalImportAliases__':
return (plugin, spec) => {
for (const key of Object.keys(spec)) {
this.aliases[key] = spec[key];
}
};
}
}
addContext(key, val) {
this.context[key] = val;
}
addPostLoader(loader) {
this.postLoaders.push(loader);
}
addNoParse(regExp) {
this.noParse.push(regExp);
}
}