UNPKG

1.62 kBJavaScriptView Raw
1/**
2 * This task builds and launches the node.js server.
3 * This node.js server serves the application.
4 * Even if the server rendering is disabled, the server serves the
5 * static website.
6 * Then it launches browser-sync to proxy requests to the node.js server.
7 * Browser-sync allows live reloading ONLY if a public ressource has changed.
8 * For all other changes (like react components, component css, etc...),
9 * the webpackHotMiddleware reload the changing part of the app.
10 */
11import webpack from 'webpack';
12import webpackDevMiddleware from 'webpack-dev-middleware';
13import webpackHotMiddleware from 'webpack-hot-middleware';
14import BrowserSync from 'browser-sync';
15
16import webpackConfig from '../config/webpack/webpack.playground.config';
17
18export default async function start() {
19 await new Promise(resolve => {
20 const bundler = webpack(webpackConfig);
21
22 const wpMiddleware = webpackDevMiddleware(bundler, {
23 publicPath: webpackConfig.output.publicPath,
24 stats: webpackConfig.stats,
25 });
26 const hotMiddlewares = webpackHotMiddleware(bundler);
27 let doneOnce = false;
28 const handleBundleComplete = () => {
29 if (!doneOnce) {
30 const bs = BrowserSync.create();
31
32 const serverOptions = {
33 baseDir: './playground',
34
35 middleware: [wpMiddleware, hotMiddlewares],
36 };
37
38 bs.init({
39 server: serverOptions,
40 files: [
41 'playground/**/*.css',
42 'playground/**/*.html',
43 ],
44 }, resolve);
45 doneOnce = true;
46 }
47 };
48
49 bundler.plugin('done', () => handleBundleComplete());
50 });
51}