UNPKG

2.15 kBJavaScriptView Raw
1var colors = require("colors");
2var through = require("through2");
3var keys = require("lodash/keys");
4var omit = require("lodash/omit");
5var defaultTo = require("lodash/defaultTo");
6var isPluginExcludedFromBuild = require("../node/is_plugin_excluded");
7var winston = require("winston");
8
9module.exports = function() {
10 return through.obj(function(data, enc, next) {
11 try {
12 next(null, addAtLoaderShim(data));
13 } catch (err) {
14 next(err);
15 }
16 });
17};
18
19/**
20 * Adds a node to the graph with an "@steal" shim
21 * @param {Object} data - The slim stream data object
22 * @return {Object} The mutated stream object
23 */
24function addAtLoaderShim(data) {
25 var graph = omit(data.graph, data.loader.configMain);
26
27 if (includesAtSteal(graph)) {
28 winston.warn(
29 colors.yellow(
30 `the @steal module is not fully supported in optimized builds`
31 )
32 );
33 data.graph["@steal"] = makeShimNode(data.loader.main);
34 }
35
36 return data;
37}
38
39/**
40 * Looks for "@steal" in the dependency graph
41 * @param {Object} graph - The dependency graph
42 * @return {boolean} true if found, false otherwise
43 */
44function includesAtSteal(graph) {
45 var found = false;
46
47 var isAtSteal = function(name) {
48 return name === "@steal";
49 };
50
51 keys(graph).forEach(function(name) {
52 var node = graph[name];
53
54 if (isPluginExcludedFromBuild(node)) {
55 return;
56 }
57
58 if (isAtSteal(name)) {
59 return (found = true);
60 }
61
62 defaultTo(node.dependencies, []).forEach(function(depName) {
63 if (isAtSteal(depName)) {
64 return (found = true);
65 }
66 });
67 });
68
69 return found;
70}
71
72/**
73 * Returns an @steal shim graph node
74 * @param {string} main - The main module name
75 * @return {Object} The faux "@steal" graph node
76 */
77function makeShimNode(main) {
78 return {
79 bundles: [main],
80 dependencies: ["@loader"],
81 deps: ["@loader"],
82 load: {
83 address: "",
84 metadata: {
85 format: "amd",
86 deps: ["@loader"],
87 dependencies: ["@loader"]
88 },
89 name: "@steal",
90 source: `
91 define("@steal", ["@loader"], function(atLoader) {
92 var steal = {};
93
94 steal.loader = atLoader;
95 steal.done = function() {
96 return Promise.resolve();
97 };
98
99 return steal;
100 });
101 `
102 }
103 };
104}