Repository navigation
Expand file tree
/
Copy pathserve-webpack.ts
More file actions
61 lines (48 loc) · 2.19 KB
/
Copy pathserve-webpack.ts
File metadata and controls
61 lines (48 loc) · 2.19 KB
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
import * as path from 'path';
import * as chalk from 'chalk';
import * as Task from 'ember-cli/lib/models/task';
import * as webpack from 'webpack';
import * as WebpackDevServer from 'webpack-dev-server';
import * as ProgressPlugin from 'webpack/lib/ProgressPlugin';
import { webpackDevServerOutputOptions } from '../models/';
import { NgCliWebpackConfig } from '../models/webpack-config';
import { ServeTaskOptions } from '../commands/serve';
import { CliConfig } from '../models/config';
module.exports = Task.extend({
run: function(commandOptions: ServeTaskOptions) {
let lastHash = null;
let webpackCompiler: any;
var config: NgCliWebpackConfig = new NgCliWebpackConfig(this.project, commandOptions.environment).config;
// This allows for live reload of page when changes are made to repo.
// https://webpack.github.io/docs/webpack-dev-server.html#inline-mode
config.entry.main.unshift(`webpack-dev-server/client?http://localhost:${commandOptions.port}/`);
webpackCompiler = webpack(config);
webpackCompiler.apply(new ProgressPlugin({
profile: true,
colors: true
}));
const webpackDevServerConfiguration: IWebpackDevServerConfigurationOptions = {
contentBase: path.resolve(this.project.root, `./${CliConfig.fromProject().defaults.sourceDir}`),
historyApiFallback: true,
stats: webpackDevServerOutputOptions,
inline: true
};
const serveMessage:string = chalk.green(`\n*\n*\n NG Live Development Server is running on http://localhost:${commandOptions.port}.\n*\n*`);
const server = new WebpackDevServer(webpackCompiler, webpackDevServerConfiguration);
return new Promise((resolve, reject) => {
server.listen(commandOptions.port, 'localhost', function(err, stats) {
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
reject(err.details);
}
if(stats && stats.hash && stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString(webpackOutputOptions) + '\n' + serveMessage + '\n');
}
process.stdout.write(serveMessage);
});
})
}
});