1 | var colors = require("colors");
|
2 | var through = require("through2");
|
3 | var keys = require("lodash/keys");
|
4 | var omit = require("lodash/omit");
|
5 | var defaultTo = require("lodash/defaultTo");
|
6 | var isPluginExcludedFromBuild = require("../node/is_plugin_excluded");
|
7 | var winston = require("winston");
|
8 |
|
9 | module.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 |
|
21 |
|
22 |
|
23 |
|
24 | function 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 |
|
41 |
|
42 |
|
43 |
|
44 | function 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 |
|
74 |
|
75 |
|
76 |
|
77 | function 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 | }
|