UNPKG

3.78 kBJavaScriptView Raw
1var PluginError = require("plugin-error");
2var log = require("fancy-log");
3var colors = require("ansi-colors");
4var through = require("through2");
5var path = require("path");
6var { findAllUi5StandardModules, findUi5ModuleName } = require("../ui5");
7var { sortBy } = require("lodash");
8
9var { Graph } = require("graphlib");
10
11const formatLibraryPreloadFile = modules => {
12 const oGraph = new Graph({ directed: true });
13
14 Object.entries(modules).forEach(([, mSrc]) => {
15 const mName = findUi5ModuleName(mSrc);
16 oGraph.setNode(mName, mSrc);
17 });
18
19 Object.entries(modules).forEach(([, mSrc]) => {
20 const mName = findUi5ModuleName(mSrc);
21 const aDeps = findAllUi5StandardModules(mSrc);
22 aDeps.forEach(sDep => {
23 if (oGraph.hasNode(sDep)) {
24 oGraph.setEdge(mName, sDep);
25 }
26 });
27 });
28
29 var rt = [];
30
31 for (; ;) {
32
33 const aSort = sortBy(
34 oGraph.nodes().map(sNode => ({
35 iDepCount: oGraph
36 .nodeEdges(sNode)
37 .filter(e => e.v == sNode).length,
38 sNode
39 })),
40 o => o.iDepCount
41 );
42
43 aSort
44 .filter(o => o.iDepCount <= 1)
45 .forEach(oNode => {
46 rt = rt.concat(oGraph.node(oNode.sNode));
47 oGraph.removeNode(oNode.sNode);
48 });
49
50 if (oGraph.nodeCount() == 0) {
51 break;
52 }
53
54 }
55
56 return rt.join("\r\n");
57};
58
59module.exports = function(options) {
60 options = options || {};
61 options.isLibrary = !!options.isLibrary;
62 options.fileName =
63 options.fileName ||
64 (options.isLibrary ? "library-preload.js" : "Component-preload.js");
65
66 if (typeof options.base !== "string") {
67 throw new PluginError("gulp-ui5-preload", "`base` parameter required");
68 }
69
70 var firstFile;
71 var preloadModules = {};
72
73 function collectFileContentsFromStream(file, enc, done) {
74 // ignore empty files
75 if (file.isNull()) {
76 done();
77 return;
78 }
79 // we don't do streams (yet)
80 if (file.isStream()) {
81 this.emit(
82 "error",
83 new PluginError(
84 "gulp-ui5-preload",
85 "File Content streams not yet supported"
86 )
87 );
88 done();
89 return;
90 }
91 if (!firstFile && file) {
92 firstFile = file;
93 }
94
95 try {
96 var resolvedPath =
97 (options.namespace
98 ? options.namespace.split(".").join("/") + "/"
99 : "") +
100 path
101 .relative(path.resolve(options.base), file.path)
102 .replace(/\\/g, "/");
103 preloadModules[resolvedPath] = file.contents.toString();
104 } catch (err) {
105 this.emit("error", new PluginError("gulp-ui5-preload", err));
106 done();
107 return;
108 }
109 done();
110 }
111
112 function pushCombinedFileToStream(done) {
113 if (!firstFile) {
114 done();
115 log.error(
116 "gulp-ui5-preload",
117 colors.red(
118 "WARNING: No files were passed to gulp-ui5-preload. Wrong path?. Skipping emit of Component-preload.js..."
119 )
120 );
121 return;
122 }
123
124 // remove logger
125
126 var contents = "";
127
128 var template = "jQuery.sap.registerPreloadedModules(JSON_CONTENT);";
129 var suffix = ".Component-preload";
130
131 if (options.isLibrary) {
132 contents = formatLibraryPreloadFile(preloadModules);
133 } else {
134 contents = template.replace("JSON_CONTENT", () =>
135 JSON.stringify(
136 {
137 name: options.namespace + suffix,
138 version: "2.0",
139 modules: preloadModules
140 },
141 null,
142 "\t"
143 )
144 );
145 }
146
147 var preloadFile = firstFile.clone({ contents: false });
148 preloadFile.contents = Buffer.from(contents, "UTF-8");
149 preloadFile.path = path.join(firstFile.base, options.fileName);
150
151 this.push(preloadFile);
152 done();
153 }
154
155 return through.obj(collectFileContentsFromStream, pushCombinedFileToStream);
156};