Skip to content

Commit 60fb36a

Browse files
committed
Added serve task as well as some interfaces for safer maintainability
1 parent 6c1c956 commit 60fb36a

File tree

7 files changed

+88
-42
lines changed

7 files changed

+88
-42
lines changed

addon/ng2/commands/serve.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ PortFinder.basePort = 49152;
1111
const getPort = Promise.denodeify(PortFinder.getPort);
1212
const defaultPort = process.env.PORT || 4200;
1313

14+
export interface IServeTaskOptions {
15+
port?: number;
16+
host?: string;
17+
proxy?: string;
18+
insecureProxy?: boolean;
19+
watcher?: string;
20+
liveReload?: boolean;
21+
liveReloadHost?: string;
22+
liveReloadPort?: number;
23+
liveReloadBaseUrl?: string;
24+
liveReloadLiveCss?: boolean;
25+
environment?: string;
26+
outputPath?: string;
27+
ssl?: boolean;
28+
sslKey?: string;
29+
sslCert?: string;
30+
}
1431

1532
module.exports = Command.extend({
1633
name: 'serve',
@@ -35,12 +52,12 @@ module.exports = Command.extend({
3552
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' }
3653
],
3754

38-
run: function(commandOptions) {
55+
run: function(commandOptions: IServeTaskOptions) {
3956
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
4057

4158
return this._checkExpressPort(commandOptions)
4259
.then(this._autoFindLiveReloadPort.bind(this))
43-
.then(function(commandOptions) {
60+
.then(function(commandOptions: IServeTaskOptions) {
4461
commandOptions = assign({}, commandOptions, {
4562
baseURL: this.project.config(commandOptions.environment).baseURL || '/'
4663
});
@@ -53,11 +70,13 @@ module.exports = Command.extend({
5370
}
5471
}
5572

56-
var ServeTask = require('../tasks/serve');
57-
var serve = new ServeTask({
73+
const ServeWebpackTask = (require('../tasks/serve-webpack.ts'))
74+
// var ServeTask = require('../tasks/serve');
75+
76+
var serve = new ServeWebpackTask({
5877
ui: this.ui,
5978
analytics: this.analytics,
60-
project: this.project
79+
project: this.project,
6180
});
6281

6382
return win.checkWindowsElevation(this.ui).then(function() {
@@ -66,9 +85,9 @@ module.exports = Command.extend({
6685
}.bind(this));
6786
},
6887

69-
_checkExpressPort: function(commandOptions) {
88+
_checkExpressPort: function(commandOptions: IServeTaskOptions) {
7089
return getPort({ port: commandOptions.port, host: commandOptions.host })
71-
.then(function(foundPort) {
90+
.then(function(foundPort: number) {
7291

7392
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
7493
var message = 'Port ' + commandOptions.port + ' is already in use.';
@@ -82,9 +101,9 @@ module.exports = Command.extend({
82101
}.bind(this));
83102
},
84103

85-
_autoFindLiveReloadPort: function(commandOptions) {
104+
_autoFindLiveReloadPort: function(commandOptions: IServeTaskOptions) {
86105
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
87-
.then(function(foundPort) {
106+
.then(function(foundPort: number) {
88107

89108
// if live reload port matches express port, try one higher
90109
if (foundPort === commandOptions.port) {

addon/ng2/models/save-for-later.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// new webpack.optimize.CommonsChunkPlugin({
2-
// name: ['polyfills', 'vendor'].reverse()
3-
// }),
1+
42
// new webpack.LoaderOptionsPlugin({
53
// minimize: true
64
// }),

addon/ng2/models/webpack-build-config.ts

+6-21
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,11 @@ export const webpackOutputOptions = {
2525

2626
export interface IWebpackDevServerConfigurationOptions {
2727
contentBase?: string;
28-
// or: contentBase: "http://localhost/",
29-
3028
hot?: boolean;
31-
// Enable special support for Hot Module Replacement
32-
// Page is no longer updated, but a "webpackHotUpdate" message is send to the content
33-
// Use "webpack/hot/dev-server" as additional module in your entry point
34-
// Note: this does _not_ add the `HotModuleReplacementPlugin` like the CLI option does.
35-
36-
// Set this as true if you want to access dev server from arbitrary url.
37-
// This is handy if you are using a html5 router.
3829
historyApiFallback?: boolean;
39-
40-
// Set this if you want to enable gzip compression for assets
4130
compress?: boolean;
42-
43-
// Set this if you want webpack-dev-server to delegate a single path to an arbitrary server.
44-
// Use "*" to proxy all paths to the specified server.
45-
// This is useful if you want to get rid of 'http://localhost:8080/' in script[src],
46-
// and has many other use cases (see https://github.com/webpack/webpack-dev-server/pull/127 ).
4731
proxy?: {[key: string] : string};
48-
49-
// pass [static options](http://expressjs.com/en/4x/api.html#express.static) to inner express server
5032
staticOptions?: any;
51-
// webpack-dev-middleware options
5233
quiet?: boolean;
5334
noInfo?: boolean;
5435
lazy?: boolean;
@@ -60,6 +41,7 @@ export interface IWebpackDevServerConfigurationOptions {
6041
publicPath?: string;
6142
headers?: { [key:string]: string };
6243
stats?: { colors: boolean; };
44+
inline: boolean;
6345
}
6446

6547
// Webpack Configuration Object
@@ -69,13 +51,13 @@ export const webpackCommonConfig = {
6951
// Resolve loaders from the CLI node_modules rather than the projects
7052
context: path.resolve(__dirname, './'),
7153
entry: {
72-
main: ngAppResolve('./src/main.ts'),
54+
main: [ngAppResolve('./src/main.ts')],
7355
vendor: ngAppResolve('./src/vendor.ts'),
7456
polyfills: ngAppResolve('./src/polyfills.ts')
7557
},
7658

7759
output: {
78-
path: './dist',
60+
path: ngAppResolve('./dist'),
7961
filename: '[name].bundle.js'
8062
},
8163

@@ -124,6 +106,9 @@ export const webpackCommonConfig = {
124106
]
125107
},
126108
plugins: [
109+
new webpack.optimize.CommonsChunkPlugin({
110+
name: ['polyfills', 'vendor'].reverse()
111+
}),
127112
new HtmlWebpackPlugin(baseHtmlTemplateConfig)
128113
],
129114
resolve: {

addon/ng2/tasks/build-webpack-watch.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {webpackCommonConfig, webpackOutputOptions} from '../models/webpack-build-config.ts';
1+
import {webpackCommonConfig, webpackOutputOptions} from '../models/webpack-build-config';
2+
import {IServeTaskOptions} from '../commands/serve';
23

34
const Task = require('ember-cli/lib/models/task');
45
const webpack = require('webpack');
@@ -13,7 +14,7 @@ let lastHash = null;
1314

1415
module.exports = Task.extend({
1516
run: () => {
16-
let commandOptions = this.options;
17+
let commandOptions: IServeTaskOptions = this.options;
1718

1819
return new Promise( (resolve, reject) => {
1920
webpackCompiler.watch({}, (err, stats) => {

addon/ng2/tasks/build-webpack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {webpackCommonConfig, webpackOutputOptions} from '../models/webpack-build-config.ts';
1+
import {webpackCommonConfig, webpackOutputOptions} from '../models/webpack-build-config';
22

33
// Configure build and output;
44
const Task = require('ember-cli/lib/models/task');

addon/ng2/tasks/serve-webpack.ts

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,60 @@
1-
import {webpackCommonConfig, webpackOutputOptions} from '../models/webpack-build-config.ts';
1+
import {webpackCommonConfig, webpackOutputOptions, IWebpackDevServerConfigurationOptions} from '../models/webpack-build-config.ts';
2+
import {IServeTaskOptions} from '../commands/serve';
3+
4+
const path = require('path');
5+
const chalk = require('chalk');
26

3-
webpackCommonConfig.entry.main.unshift("webpack-dev-server/client?http://localhost:8080/");
47

58
const Task = require('ember-cli/lib/models/task');
69
const webpack = require('webpack');
710
const WebpackDevServer = require('webpack-dev-server');
8-
const webpackCompiler = webpack(webpackCommonConfig);
911
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
1012

11-
const server = new WebpackDevServer(webpackCompiler);
1213

1314

15+
let lastHash = null;
16+
17+
module.exports = Task.extend({
18+
run: (commandOptions: IServeTaskOptions) => {
19+
20+
webpackCommonConfig.entry.main.unshift(`webpack-dev-server/client?http://localhost:${commandOptions.port}/`);
21+
const webpackCompiler = webpack(webpackCommonConfig);
22+
23+
webpackCompiler.apply(new ProgressPlugin({
24+
profile: true,
25+
colors: true
26+
}));
27+
28+
const webpackDevServerConfiguration: IWebpackDevServerConfigurationOptions = {
29+
contentBase: path.resolve(process.cwd(), './src'),
30+
historyApiFallback: true,
31+
stats: { colors: true },
32+
inline: true
33+
};
34+
35+
const server = new WebpackDevServer(webpackCompiler, webpackDevServerConfiguration);
36+
const serveMessage:string = chalk.green(`\n*\n*\n NG Live Development Server is running on http://localhost:${commandOptions.port}.\n*\n*`);
37+
38+
return new Promise((resolve, reject) => {
39+
server.listen(commandOptions.port, "localhost", function(err, stats) {
40+
if(err) {
41+
lastHash = null;
42+
console.error(err.stack || err);
43+
if(err.details) console.error(err.details);
44+
reject(err.details);
45+
}
46+
47+
if(stats && stats.hash && stats.hash !== lastHash) {
48+
lastHash = stats.hash;
49+
process.stdout.write(stats.toString(webpackOutputOptions) + "\n" + serveMessage + "\n");
50+
}
51+
52+
process.stdout.write(serveMessage);
53+
});
54+
})
55+
}
56+
});
1457

1558

1659

1760

18-
server.listen(8080);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"ts-loader": "^0.8.2",
6868
"typescript": "^1.9.0-dev.20160611-1.0",
6969
"typings": "^0.8.1",
70-
"webpack": "2.1.0-beta.13"
70+
"webpack": "2.1.0-beta.13",
71+
"webpack-dev-server": "^1.14.1"
7172
},
7273
"ember-addon": {
7374
"paths": [

0 commit comments

Comments
 (0)