1 | var path = require("path");
|
2 | var fs = require("fs-extra");
|
3 | var through = require("through2");
|
4 | var denodeify = require("pdenodeify");
|
5 | var concat = require("lodash/concat");
|
6 | var includes = require("lodash/includes");
|
7 | var isString = require("lodash/isString");
|
8 | var isJavaScriptBundle = require("../bundle/is_js_bundle");
|
9 |
|
10 | var outputFile = denodeify(fs.outputFile);
|
11 |
|
12 | module.exports = function() {
|
13 | return through.obj(function(data, enc, next) {
|
14 | var promise = canWriteManifest(data.options) ?
|
15 | writeBundleManifest(data) :
|
16 | Promise.resolve(data);
|
17 |
|
18 | promise
|
19 | .then(function(result) {
|
20 | next(null, result);
|
21 | })
|
22 | .catch(next);
|
23 | });
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function canWriteManifest(options) {
|
31 | return options.bundleManifest === true || isString(options.bundleManifest);
|
32 | }
|
33 |
|
34 | function writeBundleManifest(data) {
|
35 | var manifest = {};
|
36 |
|
37 | var entryPointBundles = concat(
|
38 | data.mains,
|
39 | data.loader.bundle
|
40 | );
|
41 |
|
42 | entryPointBundles.forEach(function(bundleName) {
|
43 | manifest[bundleName] = getSharedBundlesOf(
|
44 | bundleName,
|
45 | data.loader.baseURL,
|
46 | data.bundles,
|
47 | data.mains
|
48 | );
|
49 | });
|
50 |
|
51 |
|
52 | var dest = isString(data.options.bundleManifest) ?
|
53 | data.options.bundleManifest :
|
54 | path.join(data.configuration.dest, "bundles.json");
|
55 |
|
56 |
|
57 | return outputFile(
|
58 | dest,
|
59 | JSON.stringify(manifest, null, "\t")
|
60 | ).then(function() {
|
61 | return data;
|
62 | });
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function getWeightOf(bundle) {
|
73 | var isCss = bundle.buildType === "css";
|
74 | var isMainBundle = bundle.bundles.length === 1;
|
75 |
|
76 | if (isCss) {
|
77 | return 1;
|
78 | } else if (isMainBundle) {
|
79 | return 2;
|
80 | } else {
|
81 | return 3;
|
82 | }
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function getSharedBundlesOf(name, baseUrl, bundles, mains) {
|
95 | var shared = {};
|
96 | var normalize = require("normalize-path");
|
97 |
|
98 |
|
99 | var singleMain = mains.length === 1 && mains[0];
|
100 |
|
101 | bundles.forEach(function(bundle) {
|
102 | if (includes(bundle.bundles, name) ||
|
103 | includes(bundle.bundles, singleMain)) {
|
104 | var relative = normalize(
|
105 | path.relative(
|
106 | baseUrl.replace("file:", ""),
|
107 | bundle.bundlePath
|
108 | )
|
109 | );
|
110 |
|
111 | shared[relative] = {
|
112 | weight: getWeightOf(bundle),
|
113 | type: isJavaScriptBundle(bundle) ? "script" : "style"
|
114 | };
|
115 | }
|
116 | });
|
117 |
|
118 | return shared;
|
119 | }
|