This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DevServer.js
217 lines (182 loc) · 6.15 KB
/
DevServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DevServer = undefined;
var _SASSCompiler = require('./SASSCompiler');
var _watch = require('./watch');
var _tinyLr = require('tiny-lr');
var _tinyLr2 = _interopRequireDefault(_tinyLr);
var _webpackDevServer = require('webpack-dev-server');
var _webpackDevServer2 = _interopRequireDefault(_webpackDevServer);
var _webpack = require('webpack');
var _webpack2 = _interopRequireDefault(_webpack);
var _path = require('path');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var LIVERELOAD_PORT = 35729,
WEB_PORT = 3000;
/**
* A lightweight development server that rapidly recompiles the JavaScript and SASS files when they are edited and
* updates the page.
*
* Utilizes the Webpack development server.
*
* Includes react hot loader to further optimize the development process of the React applications.
*
* Please install and enable the LiveReload browser extension for the CSS reloading to work.
*
* @class DevServer
* @param {string} script - a full system path to a JavaScript file
* @param {string} style - a full system path to a SASS file
* @param {string} devDir - a full system path to a directory in which to put any compiled development resources
* @param {number} [port=3000] - a port at which to start the dev server
* @param {boolean} [react=true] - false to disable the react hot loader plugin
* @see {@link http://gaearon.github.io/react-hot-loader/|React Hot Loader}
* @example
* import {DevServer} from 'webcompiler';
* import {join} from 'path';
*
* const rootDir = join(__dirname, '..'),
* devDir = join(rootDir, 'development'),
* server = new DevServer(join(devDir, 'script.js'), join(devDir, 'app.scss'), devDir);
*
* server.run(rootDir);
* // now navigate to http://localhost:3000 using your favorite browser ( preferably Chrome :) )
*/
var DevServer = exports.DevServer = function () {
/**
* recompiles SASS and notifies LiveReload
*
* @method compileSASS
* @memberof DevServer
* @private
* @instance
*/
/**
* a port at which to start the dev server
*
* @member {number} port
* @memberof DevServer
* @private
* @instance
*/
function DevServer(script, style, devDir) {
var _this = this;
var port = arguments.length <= 3 || arguments[3] === undefined ? WEB_PORT : arguments[3];
var react = arguments.length <= 4 || arguments[4] === undefined ? true : arguments[4];
(0, _classCallCheck3.default)(this, DevServer);
var sass = new _SASSCompiler.SASSCompiler(),
loaders = [];
if (react) {
loaders.push('react-hot');
}
loaders.push('babel');
this.port = port;
this.lr = (0, _tinyLr2.default)();
this.compileSASS = sass.fe.bind(sass, style, (0, _path.join)(devDir, 'style.css'), function () {
_this.lr.changed({ body: { files: ['style.css'] } });
});
this.server = new _webpackDevServer2.default((0, _webpack2.default)({
cache: {},
debug: true,
devtool: 'eval-source-map',
entry: ['webpack-dev-server/client?http://localhost:' + this.port, 'webpack/hot/only-dev-server', script],
output: {
path: devDir,
filename: 'script.js',
publicPath: '/'
},
plugins: [new _webpack2.default.HotModuleReplacementPlugin(), new _webpack2.default.NoErrorsPlugin()],
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loaders: loaders
}]
}
}), {
contentBase: devDir,
publicPath: '/',
hot: true,
historyApiFallback: true
});
this.server.app.get('*', function (req, res) {
res.send('<!DOCTYPE html>\n<html lang="en">\n <head>\n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge">\n <meta name="viewport" content="width=device-width, initial-scale=1">\n <title>Development server - Webcompiler</title>\n <link href="/style.css" rel="stylesheet">\n </head>\n <body>\n <div id="app"></div>\n <script src="/script.js" async defer></script>\n </body>\n</html>');
});
}
/**
* Compile SASS and start watching for file changes
*
* @memberof DevServer
* @instance
* @method watchSASS
* @param {string} watchDir - the directory in which to watch for the changes in the SASS files
* @example
* server.watchSASS('/path/to/some/directory');
*/
/**
* the Webpack development server
*
* @member {WebpackDevServer} server
* @memberof DevServer
* @private
* @instance
*/
/**
* a LiveReload server
*
* @member {tinylr.Server} lr
* @memberof DevServer
* @private
* @instance
*/
(0, _createClass3.default)(DevServer, [{
key: 'watchSASS',
value: function watchSASS(watchDir) {
this.lr.listen(LIVERELOAD_PORT);
this.compileSASS();
(0, _watch.watch)(watchDir, 'scss', this.compileSASS);
}
/**
* Starts the Webpack development server
*
* @memberof DevServer
* @instance
* @method watchJS
* @example
* server.watchJS();
*/
}, {
key: 'watchJS',
value: function watchJS() {
var port = this.port;
this.server.listen(port, '0.0.0.0', function (error) {
if (error) {
return console.error(error);
}
console.log('Started the development server at localhost:' + port);
});
}
/**
* Starts the Webpack development server, compiles SASS and starts watching for file changes
*
* @memberof DevServer
* @instance
* @method run
* @param {string} watchDir - the directory in which to watch for the changes in the SASS files
* @example
* server.run('/path/to/some/directory');
*/
}, {
key: 'run',
value: function run(watchDir) {
this.watchJS();
this.watchSASS(watchDir);
}
}]);
return DevServer;
}();