UNPKG

7.54 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createEntryListFile = createEntryListFile;
7exports.createWebpackConfigArray = createWebpackConfigArray;
8exports.createWebpackConfigFilepathByIndex = createWebpackConfigFilepathByIndex;
9exports.buildToDir = buildToDir;
10exports.watchAndBuildToDir = watchAndBuildToDir;
11exports.devServer = devServer;
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
14
15function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
16
17function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
18
19var _path = require("path");
20
21var _rx = require("rx");
22
23var _rx2 = _interopRequireDefault(_rx);
24
25var _lodash = require("lodash");
26
27var _lodash2 = _interopRequireDefault(_lodash);
28
29var _coreIndex = require("./core/index");
30
31var _utilIndex = require("./util/index");
32
33/**
34 * @private
35 */
36
37function createEntryListFile(srcPatternList) {
38 return _rx.Observable.from(srcPatternList).selectMany(_coreIndex.createGlobResult).selectMany(_coreIndex.globResultToFile).selectMany(_coreIndex.addCodeToFile).map(_coreIndex.addElementToFile).map(_coreIndex.addEntryListToFile);
39}
40
41/**
42 * @private
43 */
44
45function createWebpackConfigArray(entryListFile, devServerConfigFilepath) {
46 return entryListFile.selectMany(_utilIndex.entryListFileToEntry).groupBy(function (entry) {
47 return entry.configFilepath;
48 }).selectMany(_coreIndex.groupedEntryListToWebpackConfig).map((0, _utilIndex.addDevServerToWebpackConfigCreator)(devServerConfigFilepath)).reduce(_coreIndex.webpackConfigReducer, []);
49 // Un-comment to demonstrate hot v.s. cold Observable
50 // .tap(::console.log)
51}
52
53/**
54 * @private
55 */
56
57function createWebpackConfigFilepathByIndex(webpackConfigArray) {
58 return webpackConfigArray.map(_utilIndex.webpackConfigArrayToIndexFilepathMap);
59}
60
61/**
62 * @public
63 */
64
65function buildToDir(destDir, srcPatternList) {
66
67 var sharedReplayEntryListFileStream = createEntryListFile(srcPatternList).shareReplay();
68
69 var publishedWebpackConfigArrayStream = createWebpackConfigArray(sharedReplayEntryListFileStream).publish();
70
71 var webpackConfigFilepathByIndexStream = createWebpackConfigFilepathByIndex(publishedWebpackConfigArrayStream);
72
73 var writeToFileResultStream = publishedWebpackConfigArrayStream.map(function (webpackConfigArray) {
74 return webpackConfigArray.map(function (_ref) {
75 var webpackConfig = _ref.webpackConfig;
76 return webpackConfig;
77 });
78 }).tap(_coreIndex.webpackConfigArrayInspector).map(_coreIndex.webpackConfigArrayToWebpackCompiler).selectMany(_coreIndex.webpackMultiCompilerToMultiStats).map(_coreIndex.webpackMultiStatsToWebpackSingleStatsArray).withLatestFrom(webpackConfigFilepathByIndexStream, _utilIndex.singleStatsArrayAndIndexFilepathMapCombiner).selectMany(function (w) {
79 return sharedReplayEntryListFileStream.map(function (e) {
80 return (0, _utilIndex.entryListFileAndFilepathWebpackStatsMapCombiner)(e, w);
81 });
82 }).map(_coreIndex.withOutputAssetsFileToMarkupFile).selectMany((0, _utilIndex.markupFileToWriteFileCreator)(destDir));
83
84 writeToFileResultStream.subscribe(function (it) {
85 return console.log("Next: " + it);
86 }, function (error) {
87 throw error;
88 }, function () {
89 return console.log("Done!");
90 });
91
92 publishedWebpackConfigArrayStream.connect();
93}
94
95/**
96 * @public
97 */
98
99function watchAndBuildToDir(destDir, srcPatternList) {
100
101 var sharedReplayEntryListFileStream = createEntryListFile(srcPatternList).shareReplay();
102
103 var publishedWebpackConfigArrayStream = createWebpackConfigArray(sharedReplayEntryListFileStream).publish();
104
105 var webpackConfigFilepathByIndexStream = createWebpackConfigFilepathByIndex(publishedWebpackConfigArrayStream);
106
107 var writeToFileResultStream = publishedWebpackConfigArrayStream.map(function (webpackConfigArray) {
108 return webpackConfigArray.map(function (_ref2) {
109 var webpackConfig = _ref2.webpackConfig;
110 return webpackConfig;
111 });
112 }).tap(_coreIndex.webpackConfigArrayInspector).selectMany(function webpackConfigArrayRunWithWatchToSingleStatsArray(webpackConfigArray) {
113 // Why selectMany? Because watch could be repeative.
114 // Instead of wrapping one value, now a series of values are emitted.
115 return _rx.Observable.from(webpackConfigArray).map(_coreIndex.webpackConfigArrayToWebpackCompiler).selectMany(_coreIndex.webpackCompilerRunWithWatchToSingleStatsWithIndex).scan(function (acc, it) {
116 acc = [].concat(_toConsumableArray(acc));
117 var index = it.index;
118
119 var rest = _objectWithoutProperties(it, ["index"]);
120
121 acc[index] = rest;
122
123 return acc;
124 }, new Array(webpackConfigArray.length)).takeWhile(function (webpackSingleStatsArray) {
125 return webpackSingleStatsArray.every(_lodash2["default"].identity);
126 });
127 }).withLatestFrom(webpackConfigFilepathByIndexStream, _utilIndex.singleStatsArrayAndIndexFilepathMapCombiner).selectMany(function (w) {
128 return sharedReplayEntryListFileStream.map(function (e) {
129 return (0, _utilIndex.entryListFileAndFilepathWebpackStatsMapCombiner)(e, w);
130 });
131 }).map(_coreIndex.withOutputAssetsFileToMarkupFile).selectMany((0, _utilIndex.markupFileToWriteFileCreator)(destDir));
132
133 writeToFileResultStream.subscribe(function (it) {
134 return console.log("Next: " + it);
135 }, function (error) {
136 throw error;
137 }, function () {
138 return console.log("Done!");
139 });
140
141 publishedWebpackConfigArrayStream.connect();
142}
143
144/**
145 * @public
146 */
147
148function devServer(relativeDevServerConfigFilepath, destDir, srcPatternList) {
149
150 var devServerConfigFilepath = (0, _path.resolve)(process.cwd(), relativeDevServerConfigFilepath);
151
152 var sharedReplayEntryListFileStream = createEntryListFile(srcPatternList).shareReplay();
153
154 var publishedWebpackConfigArrayStream = createWebpackConfigArray(sharedReplayEntryListFileStream, devServerConfigFilepath).publish();
155
156 var webpackConfigFilepathByIndexStream = createWebpackConfigFilepathByIndex(publishedWebpackConfigArrayStream);
157
158 var writeToFileResultStream = publishedWebpackConfigArrayStream.map(function (webpackConfigArray) {
159 return webpackConfigArray.map(function (_ref3) {
160 var webpackConfig = _ref3.webpackConfig;
161 return webpackConfig;
162 });
163 }).tap(_coreIndex.webpackConfigArrayInspector).map(_coreIndex.webpackConfigArrayToWebpackCompiler)
164 // Why selectMany? Because devServer just like watch could be repeative.
165 // Instead of wrapping one value, now a series of values are emitted.
166 .selectMany(_coreIndex.webpackMultiCompilerRunWithDevServerToSingleStatsArray).withLatestFrom(webpackConfigFilepathByIndexStream, _utilIndex.singleStatsArrayAndIndexFilepathMapCombiner).selectMany(function (w) {
167 return sharedReplayEntryListFileStream.map(function (e) {
168 return (0, _utilIndex.entryListFileAndFilepathWebpackStatsMapCombiner)(e, w);
169 });
170 }).map(_coreIndex.withOutputAssetsFileToMarkupFile).selectMany((0, _utilIndex.markupFileToWriteFileCreator)(destDir));
171
172 writeToFileResultStream.subscribe(function (it) {
173 return console.log("Next: " + it);
174 }, function (error) {
175 throw error;
176 }, function () {
177 return console.log("Done!");
178 });
179
180 publishedWebpackConfigArrayStream.connect();
181}
182
183// Watch
184
185// DevServer
186
187// DevServer
\No newline at end of file