-
Notifications
You must be signed in to change notification settings - Fork 23
/
add_loader_shim.js
107 lines (93 loc) · 2.42 KB
/
add_loader_shim.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var colors = require("colors");
var through = require("through2");
var keys = require("lodash/keys");
var omit = require("lodash/omit");
var assign = require("lodash/assign");
var clone = require("lodash/cloneDeep");
var defaultTo = require("lodash/defaultTo");
var isPluginExcludedFromBuild = require("../node/is_plugin_excluded");
var winston = require("winston");
module.exports = function() {
return through.obj(function(data, enc, next) {
try {
next(null, addAtLoaderShim(data));
} catch (err) {
next(err);
}
});
};
/**
* Adds a node to the graph with an "@loader" shim
* The "@loader" shim contains the loader configuration properties
* @param {Object} data - The slim stream data object
* @return {Object} The mutated stream object
*/
function addAtLoaderShim(data) {
if (includesAtLoader(omit(data.graph, data.loader.configMain))) {
winston.warn(
colors.yellow(
`the @loader module is not fully supported in optimized builds`
)
);
var config = clone(
omit(data.loader.__loaderConfig, ["paths", "stealPath"])
);
// make sure the "window-production" config overrides previous values
assign(config, defaultTo(config.envs, {})["window-production"]);
data.graph["@loader"] = makeShimNode(data.loader.main, config);
}
return data;
}
/**
* Looks for "@loader" in the dependency graph
* @param {Object} graph - The dependency graph
* @return {boolean} true if found, false otherwise
*/
function includesAtLoader(graph) {
var found = false;
var isAtLoader = function(name) {
return name === "@loader";
};
keys(graph).forEach(function(name) {
var node = graph[name];
if (isPluginExcludedFromBuild(node)) {
return;
}
if (isAtLoader(name)) {
return (found = true);
}
defaultTo(node.dependencies, []).forEach(function(depName) {
if (isAtLoader(depName)) {
return (found = true);
}
});
});
return found;
}
/**
* Returns an @loader node with the source code returning the config object
* @param {string} main - The main module name
* @param {Object} config - The config object exposed by "@loader"
* @return {Object} The faux "@loader" graph node
*/
function makeShimNode(main, config) {
return {
bundles: [main],
dependencies: [],
deps: [],
load: {
address: "",
metadata: {
deps: [],
format: "amd",
dependencies: []
},
name: "@loader",
source: `
define("@loader", function() {
return ${JSON.stringify(config)};
});
`
}
};
}