Skip to content

Commit 5191531

Browse files
Add AoT compilation to Angular template
1 parent 03018f3 commit 5191531

File tree

7 files changed

+103
-27
lines changed

7 files changed

+103
-27
lines changed

templates/AngularSpa/ClientApp/app/app.module.browser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import { AppComponent } from './components/app/app.component';
1010
AppModuleShared
1111
],
1212
providers: [
13-
{ provide: 'ORIGIN_URL', useValue: location.origin }
13+
{ provide: 'ORIGIN_URL', useFactory: getOriginUrl }
1414
]
1515
})
1616
export class AppModule {
1717
}
18+
19+
export function getOriginUrl() {
20+
return location.origin;
21+
}
File renamed without changes.
File renamed without changes.

templates/AngularSpa/npm-shrinkwrap.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/AngularSpa/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
"@angular/animations": "4.2.5",
1010
"@angular/common": "4.2.5",
1111
"@angular/compiler": "4.2.5",
12+
"@angular/compiler-cli": "4.2.5",
1213
"@angular/core": "4.2.5",
1314
"@angular/forms": "4.2.5",
1415
"@angular/http": "4.2.5",
1516
"@angular/platform-browser": "4.2.5",
1617
"@angular/platform-browser-dynamic": "4.2.5",
1718
"@angular/platform-server": "4.2.5",
1819
"@angular/router": "4.2.5",
20+
"@ngtools/webpack": "1.5.0",
1921
"@types/node": "8.0.8",
2022
"angular2-template-loader": "0.6.2",
2123
"aspnet-prerendering": "^2.0.5",

templates/AngularSpa/webpack.config.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const webpack = require('webpack');
33
const merge = require('webpack-merge');
4+
const AotPlugin = require('@ngtools/webpack').AotPlugin;
45
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
56

67
module.exports = (env) => {
@@ -16,7 +17,7 @@ module.exports = (env) => {
1617
},
1718
module: {
1819
rules: [
19-
{ test: /\.ts$/, include: /ClientApp/, use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] },
20+
{ test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
2021
{ test: /\.html$/, use: 'html-loader?minimize=false' },
2122
{ test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
2223
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
@@ -28,7 +29,7 @@ module.exports = (env) => {
2829
// Configuration for client-side bundle suitable for running in browsers
2930
const clientBundleOutputDir = './wwwroot/dist';
3031
const clientBundleConfig = merge(sharedConfig, {
31-
entry: { 'main-client': './ClientApp/boot-client.ts' },
32+
entry: { 'main-client': './ClientApp/boot.client.ts' },
3233
output: { path: path.join(__dirname, clientBundleOutputDir) },
3334
plugins: [
3435
new webpack.DllReferencePlugin({
@@ -43,22 +44,34 @@ module.exports = (env) => {
4344
})
4445
] : [
4546
// Plugins that apply in production builds only
46-
new webpack.optimize.UglifyJsPlugin()
47+
new webpack.optimize.UglifyJsPlugin(),
48+
new AotPlugin({
49+
tsConfigPath: './tsconfig.json',
50+
entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
51+
exclude: ['./**/*.server.ts']
52+
})
4753
])
4854
});
4955

5056
// Configuration for server-side (prerendering) bundle suitable for running in Node
5157
const serverBundleConfig = merge(sharedConfig, {
5258
resolve: { mainFields: ['main'] },
53-
entry: { 'main-server': './ClientApp/boot-server.ts' },
59+
entry: { 'main-server': './ClientApp/boot.server.ts' },
5460
plugins: [
5561
new webpack.DllReferencePlugin({
5662
context: __dirname,
5763
manifest: require('./ClientApp/dist/vendor-manifest.json'),
5864
sourceType: 'commonjs2',
5965
name: './vendor'
6066
})
61-
],
67+
].concat(isDevBuild ? [] : [
68+
// Plugins that apply in production builds only
69+
new AotPlugin({
70+
tsConfigPath: './tsconfig.json',
71+
entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
72+
exclude: ['./**/*.client.ts']
73+
})
74+
]),
6275
output: {
6376
libraryTarget: 'commonjs',
6477
path: path.join(__dirname, './ClientApp/dist')

templates/AngularSpa/webpack.config.vendor.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ const path = require('path');
22
const webpack = require('webpack');
33
const ExtractTextPlugin = require('extract-text-webpack-plugin');
44
const merge = require('webpack-merge');
5+
const treeShakableModules = [
6+
'@angular/animations',
7+
'@angular/common',
8+
'@angular/compiler',
9+
'@angular/core',
10+
'@angular/forms',
11+
'@angular/http',
12+
'@angular/platform-browser',
13+
'@angular/platform-browser-dynamic',
14+
'@angular/router',
15+
'zone.js',
16+
];
17+
const nonTreeShakableModules = [
18+
'bootstrap',
19+
'bootstrap/dist/css/bootstrap.css',
20+
'es6-promise',
21+
'es6-shim',
22+
'event-source-polyfill',
23+
'jquery',
24+
];
25+
const allModules = treeShakableModules.concat(nonTreeShakableModules);
526

627
module.exports = (env) => {
728
const extractCSS = new ExtractTextPlugin('vendor.css');
@@ -14,26 +35,6 @@ module.exports = (env) => {
1435
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' }
1536
]
1637
},
17-
entry: {
18-
vendor: [
19-
'@angular/animations',
20-
'@angular/common',
21-
'@angular/compiler',
22-
'@angular/core',
23-
'@angular/forms',
24-
'@angular/http',
25-
'@angular/platform-browser',
26-
'@angular/platform-browser-dynamic',
27-
'@angular/router',
28-
'bootstrap',
29-
'bootstrap/dist/css/bootstrap.css',
30-
'es6-shim',
31-
'es6-promise',
32-
'event-source-polyfill',
33-
'jquery',
34-
'zone.js',
35-
]
36-
},
3738
output: {
3839
publicPath: '/dist/',
3940
filename: '[name].js',
@@ -48,6 +49,11 @@ module.exports = (env) => {
4849
};
4950

5051
const clientBundleConfig = merge(sharedConfig, {
52+
entry: {
53+
// To keep development builds fast, include all vendor dependencies in the vendor bundle.
54+
// But for production builds, leave the tree-shakable ones out so the AOT compiler can produce a smaller bundle.
55+
vendor: isDevBuild ? allModules : nonTreeShakableModules
56+
},
5157
output: { path: path.join(__dirname, 'wwwroot', 'dist') },
5258
module: {
5359
rules: [
@@ -68,14 +74,14 @@ module.exports = (env) => {
6874
const serverBundleConfig = merge(sharedConfig, {
6975
target: 'node',
7076
resolve: { mainFields: ['main'] },
77+
entry: { vendor: allModules.concat(['aspnet-prerendering']) },
7178
output: {
7279
path: path.join(__dirname, 'ClientApp', 'dist'),
7380
libraryTarget: 'commonjs2',
7481
},
7582
module: {
7683
rules: [ { test: /\.css(\?|$)/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] } ]
7784
},
78-
entry: { vendor: ['aspnet-prerendering'] },
7985
plugins: [
8086
new webpack.DllPlugin({
8187
path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),

0 commit comments

Comments
 (0)