1 | 'use strict';
|
2 |
|
3 | var _promise = require('babel-runtime/core-js/promise');
|
4 |
|
5 | var _promise2 = _interopRequireDefault(_promise);
|
6 |
|
7 | var _regenerator = require('babel-runtime/regenerator');
|
8 |
|
9 | var _regenerator2 = _interopRequireDefault(_regenerator);
|
10 |
|
11 | var _keys = require('babel-runtime/core-js/object/keys');
|
12 |
|
13 | var _keys2 = _interopRequireDefault(_keys);
|
14 |
|
15 | var _getIterator2 = require('babel-runtime/core-js/get-iterator');
|
16 |
|
17 | var _getIterator3 = _interopRequireDefault(_getIterator2);
|
18 |
|
19 | var _assign = require('babel-runtime/core-js/object/assign');
|
20 |
|
21 | var _assign2 = _interopRequireDefault(_assign);
|
22 |
|
23 | var _koa = require('koa');
|
24 |
|
25 | var _koa2 = _interopRequireDefault(_koa);
|
26 |
|
27 | var _path = require('path');
|
28 |
|
29 | var _path2 = _interopRequireDefault(_path);
|
30 |
|
31 | var _webpack = require('webpack');
|
32 |
|
33 | var _webpack2 = _interopRequireDefault(_webpack);
|
34 |
|
35 | var _webpackMerge = require('webpack-merge');
|
36 |
|
37 | var _webpackMerge2 = _interopRequireDefault(_webpackMerge);
|
38 |
|
39 | var _webpackDocs = require('./webpack.docs.conf');
|
40 |
|
41 | var _webpackDocs2 = _interopRequireDefault(_webpackDocs);
|
42 |
|
43 | var _koaWebpackDevMiddleware = require('koa-webpack-dev-middleware');
|
44 |
|
45 | var _koaWebpackDevMiddleware2 = _interopRequireDefault(_koaWebpackDevMiddleware);
|
46 |
|
47 | var _koaWebpackHotMiddleware = require('koa-webpack-hot-middleware');
|
48 |
|
49 | var _koaWebpackHotMiddleware2 = _interopRequireDefault(_koaWebpackHotMiddleware);
|
50 |
|
51 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
52 |
|
53 | var _marked = _regenerator2.default.mark(readFile);
|
54 |
|
55 | module.exports = function (userWebpackConfig) {
|
56 | var compiler = void 0,
|
57 | app = void 0,
|
58 | webpackConfig = void 0;
|
59 |
|
60 | app = (0, _koa2.default)();
|
61 | webpackConfig = (0, _webpackMerge2.default)(_webpackDocs2.default, userWebpackConfig);
|
62 |
|
63 | webpackConfig.plugins = webpackConfig.plugins || [];
|
64 | webpackConfig.devServer = (0, _assign2.default)({}, webpackConfig.devServer, {
|
65 | hot: true,
|
66 | publicPath: webpackConfig.output.publicPath
|
67 | });
|
68 |
|
69 | if (Array.isArray(webpackConfig.entry)) {
|
70 | webpackConfig.entry.unshift('webpack-hot-middleware/client');
|
71 | } else {
|
72 | var _iteratorNormalCompletion = true;
|
73 | var _didIteratorError = false;
|
74 | var _iteratorError = undefined;
|
75 |
|
76 | try {
|
77 | for (var _iterator = (0, _getIterator3.default)((0, _keys2.default)(webpackConfig.entry)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
78 | var key = _step.value;
|
79 |
|
80 | webpackConfig.entry[key].unshift('webpack-hot-middleware/client?reload=true');
|
81 | }
|
82 | } catch (err) {
|
83 | _didIteratorError = true;
|
84 | _iteratorError = err;
|
85 | } finally {
|
86 | try {
|
87 | if (!_iteratorNormalCompletion && _iterator.return) {
|
88 | _iterator.return();
|
89 | }
|
90 | } finally {
|
91 | if (_didIteratorError) {
|
92 | throw _iteratorError;
|
93 | }
|
94 | }
|
95 | }
|
96 | }
|
97 |
|
98 | webpackConfig.plugins.unshift(new _webpack2.default.HotModuleReplacementPlugin());
|
99 | webpackConfig.plugins.unshift(new _webpack2.default.NoErrorsPlugin());
|
100 |
|
101 | compiler = (0, _webpack2.default)(webpackConfig);
|
102 |
|
103 | app.use(webpackDevServer(compiler, webpackConfig.devServer));
|
104 | app.use( _regenerator2.default.mark(function _callee(next) {
|
105 | return _regenerator2.default.wrap(function _callee$(_context) {
|
106 | while (1) {
|
107 | switch (_context.prev = _context.next) {
|
108 | case 0:
|
109 | this.type = 'text/html';
|
110 | _context.next = 3;
|
111 | return readFile(compiler, _path2.default.join(compiler.outputPath, 'index.html'));
|
112 |
|
113 | case 3:
|
114 | this.body = _context.sent;
|
115 |
|
116 | case 4:
|
117 | case 'end':
|
118 | return _context.stop();
|
119 | }
|
120 | }
|
121 | }, _callee, this);
|
122 | }));
|
123 |
|
124 | return {
|
125 | listen: function listen(port, callback) {
|
126 | app.listen(port, callback || function (err) {
|
127 | if (err) {
|
128 | console.log(err);
|
129 | return;
|
130 | }
|
131 | });
|
132 | }
|
133 | };
|
134 | };
|
135 |
|
136 | function webpackDevServer(compiler, devServer) {
|
137 | var hotOptions = {
|
138 | log: function log() {}
|
139 | };
|
140 |
|
141 | return compose([(0, _koaWebpackDevMiddleware2.default)(compiler, devServer), (0, _koaWebpackHotMiddleware2.default)(compiler, hotOptions)]);
|
142 | }
|
143 |
|
144 | function compose(middleware) {
|
145 | return (_regenerator2.default.mark(function _callee2(next) {
|
146 | var i;
|
147 | return _regenerator2.default.wrap(function _callee2$(_context3) {
|
148 | while (1) {
|
149 | switch (_context3.prev = _context3.next) {
|
150 | case 0:
|
151 | if (!next) {
|
152 | next = _regenerator2.default.mark(function noop() {
|
153 | return _regenerator2.default.wrap(function noop$(_context2) {
|
154 | while (1) {
|
155 | switch (_context2.prev = _context2.next) {
|
156 | case 0:
|
157 | case 'end':
|
158 | return _context2.stop();
|
159 | }
|
160 | }
|
161 | }, noop, this);
|
162 | });
|
163 | }
|
164 |
|
165 | i = middleware.length;
|
166 |
|
167 |
|
168 | while (i--) {
|
169 | next = middleware[i].call(this, next);
|
170 | }
|
171 |
|
172 | return _context3.delegateYield(next, 't0', 4);
|
173 |
|
174 | case 4:
|
175 | return _context3.abrupt('return', _context3.t0);
|
176 |
|
177 | case 5:
|
178 | case 'end':
|
179 | return _context3.stop();
|
180 | }
|
181 | }
|
182 | }, _callee2, this);
|
183 | })
|
184 | );
|
185 | }
|
186 |
|
187 | function readFile(compiler, filepath) {
|
188 | return _regenerator2.default.wrap(function readFile$(_context4) {
|
189 | while (1) {
|
190 | switch (_context4.prev = _context4.next) {
|
191 | case 0:
|
192 | return _context4.abrupt('return', new _promise2.default(function (resolve, reject) {
|
193 | compiler.outputFileSystem.readFile(filepath, function (err, result) {
|
194 | if (err) {
|
195 | reject(err);
|
196 | return;
|
197 | }
|
198 |
|
199 | resolve(result);
|
200 | });
|
201 | }));
|
202 |
|
203 | case 1:
|
204 | case 'end':
|
205 | return _context4.stop();
|
206 | }
|
207 | }
|
208 | }, _marked, this);
|
209 | } |
\ | No newline at end of file |