Skip to content

Commit 8c1bf8b

Browse files
committed
feat(serve): add option to serve app from dist folder
Add a flag to allow serving the app from the dist folder.
1 parent a537dce commit 8c1bf8b

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

packages/@angular/cli/commands/e2e.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface E2eTaskOptions extends ServeTaskOptions {
1414
webdriverUpdate: boolean;
1515
specs: string[];
1616
elementExplorer: boolean;
17+
useDist: boolean;
1718
}
1819

1920
const E2eCommand = Command.extend({
@@ -65,7 +66,14 @@ const E2eCommand = Command.extend({
6566
Compile and Serve the app.
6667
All non-reload related serve options are also available (e.g. --port=4400).
6768
`
68-
}
69+
},
70+
{
71+
name: 'use-dist',
72+
type: Boolean,
73+
default: false,
74+
aliases: ['ud'],
75+
description: 'Uses the files in the dist folder to run tests instead of serving from memory.',
76+
},
6977
], [
7078
{
7179
name: 'port',

packages/@angular/cli/commands/serve.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface ServeTaskOptions extends BuildOptions {
2626
sslCert?: string;
2727
open?: boolean;
2828
hmr?: boolean;
29+
useDist?: boolean;
2930
}
3031

3132
// Expose options unrelated to live-reload to other commands that need to run serve
@@ -93,14 +94,21 @@ export const baseServeCommandOptions: any = overrideOptions([
9394
type: Boolean,
9495
default: false,
9596
description: 'Enable hot module replacement.',
96-
}
97-
], [
97+
},
9898
{
99-
name: 'watch',
100-
default: true,
101-
description: 'Rebuild on change.'
102-
}
103-
]);
99+
name: 'use-dist',
100+
type: Boolean,
101+
default: false,
102+
aliases: ['ud'],
103+
description: 'Uses the files in the dist folder to run tests instead of re-building.',
104+
},
105+
], [
106+
{
107+
name: 'watch',
108+
default: true,
109+
description: 'Rebuild on change.'
110+
}
111+
]);
104112

105113
const ServeCommand = Command.extend({
106114
name: 'serve',

packages/@angular/cli/tasks/serve.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ export default Task.extend({
3131
if (projectConfig.project && projectConfig.project.ejected) {
3232
throw new SilentError('An ejected project cannot use the build command anymore.');
3333
}
34-
rimraf.sync(path.resolve(this.project.root, outputPath));
34+
35+
const contentBase = path.resolve(this.project.root, outputPath);
36+
if (serveTaskOptions.useDist) {
37+
if(!fs.existsSync(contentBase)) {
38+
throw new SilentError('Dist directory not found');
39+
}
40+
} else {
41+
rimraf.sync(contentBase);
42+
}
3543

3644
const serveDefaults = {
3745
// default deployUrl to '' on serve to prevent the default from .angular-cli.json
@@ -151,6 +159,12 @@ export default Task.extend({
151159
overlay: serveTaskOptions.target === 'development'
152160
};
153161

162+
if (serveTaskOptions.useDist) {
163+
webpackDevServerConfiguration.contentBase = contentBase;
164+
webpackDevServerConfiguration.lazy = true;
165+
webpackDevServerConfiguration.filename = appConfig.main;
166+
}
167+
154168
if (sslKey != null && sslCert != null) {
155169
webpackDevServerConfiguration.key = sslKey;
156170
webpackDevServerConfiguration.cert = sslCert;
@@ -182,7 +196,10 @@ export default Task.extend({
182196
return reject(err);
183197
}
184198
if (serveTaskOptions.open) {
185-
opn(serverAddress);
199+
opn(serverAddress);
200+
}
201+
if(serveTaskOptions.useDist && rebuildDoneCb) {
202+
rebuildDoneCb();
186203
}
187204
});
188205
})

0 commit comments

Comments
 (0)