Skip to content

Commit 391f46f

Browse files
committed
Webpack ngc aot
1 parent 0f5ce1e commit 391f46f

17 files changed

+235
-90
lines changed

ngc-webpack/config/constants.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
const { root } = require('./helpers.js');
2+
const EVENT = process.env.npm_lifecycle_event || '';
3+
const PROD = EVENT.includes('prod');
24

35
const BASE_OUTPUT_DIR = './dist';
46

57
module.exports = {
68
SRC_DIR: 'src',
7-
DLL_OUTPUT_DIR: BASE_OUTPUT_DIR + '/dll',
8-
EXCLUDE_SOURCE_MAPS: [
9-
// these packages have problems with their sourcemaps
10-
root('node_modules/@angular'),
11-
root('node_modules/rxjs')
12-
]
9+
OUTPUT_DIR: BASE_OUTPUT_DIR + '/' + (PROD ? 'prod' : 'dev'),
10+
AOT_OUTPUT_DIR: BASE_OUTPUT_DIR + '/aot',
11+
DLL_OUTPUT_DIR: BASE_OUTPUT_DIR + '/dll'
1312
};

ngc-webpack/config/helpers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@ function root(args) {
88
return path.join.apply(path, [_root].concat(args));
99
}
1010

11+
const highPriorityChunks = ['polyfills', 'common'];// later, higher
12+
function sortChunks (prev, next) {
13+
const prevName = prev.names[0];
14+
const nextName = next.names[0];
15+
16+
return highPriorityChunks.indexOf(prevName) < highPriorityChunks.indexOf(nextName);
17+
}
1118

1219
exports.root = root;
20+
exports.sortChunks = sortChunks;

ngc-webpack/config/webpack.common.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
const { EXCLUDE_SOURCE_MAPS, SRC_DIR} = require('./constants');
22
const webpack = require('webpack');
33
const { root, sortChunks } = require('./helpers.js');
4-
const CopyWebpackPlugin = require('copy-webpack-plugin');
4+
55
const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin');
66
const HtmlWebpackPlugin = require('html-webpack-plugin');
77
const EVENT = process.env.npm_lifecycle_event || '';
8-
const AOT = EVENT.includes('aot');
8+
const AOT = EVENT.includes('prod');
99

1010

1111
module.exports = {
1212
entry: {
13-
app: root(SRC_DIR + '/main.browser' + (AOT ? '.aot' : '')),
14-
polyfills: root(SRC_DIR + '/polyfills.browser' + (AOT ? '.aot' : '')),
15-
vendors: root(SRC_DIR + '/vendors')
13+
app: root(SRC_DIR + '/main' + (AOT ? '.aot' : '')),
14+
polyfills: root(SRC_DIR + '/polyfills' + (AOT ? '.aot' : ''))
1615
},
1716

1817
resolve: {
@@ -23,16 +22,13 @@ module.exports = {
2322

2423
module: {
2524
rules: [
26-
{
27-
test: /\.ts$/,
28-
loader: 'tslint-loader',
29-
enforce: 'pre',
30-
exclude: /(node_modules|output)/
31-
},
3225
{
3326
test: /\.js$/,
3427
loader: 'source-map-loader',
35-
exclude: [EXCLUDE_SOURCE_MAPS]
28+
exclude: [
29+
root('node_modules/@angular'),
30+
root('node_modules/rxjs')
31+
]
3632
},
3733
{test: /\.json$/, loader: 'json-loader'},
3834
{test: /\.html/, loader: 'raw-loader'},
@@ -56,7 +52,7 @@ module.exports = {
5652
title: 'index',
5753
filename: 'index.html',
5854
template: root(SRC_DIR + '/index.html'),
59-
chunks: ['app', 'polyfills', 'vendors'],
55+
chunks: ['app', 'polyfills'],
6056
chunksSortMode: sortChunks
6157
})
6258
],

ngc-webpack/config/webpack.dev.js

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
1-
import {
2-
DEV_PORT, HOST, USE_DEV_SERVER_PROXY, DEV_SERVER_PROXY_CONFIG, DEV_SERVER_WATCH_OPTIONS,
3-
DEV_SOURCE_MAPS, OUTPUT_DIR, DLL_OUTPUT_DIR, AOT_OUTPUT_DIR, SRC_DIR
4-
} from './constants';
5-
const {DefinePlugin, DllReferencePlugin, SourceMapDevToolPlugin} = require('webpack');
6-
const {CheckerPlugin} = require('awesome-typescript-loader');
7-
const CopyWebpackPlugin = require('copy-webpack-plugin');
1+
const { OUTPUT_DIR, DLL_OUTPUT_DIR } = require('./constants');
2+
const { DefinePlugin, DllReferencePlugin, SourceMapDevToolPlugin } = require('webpack');
3+
const { CheckerPlugin } = require('awesome-typescript-loader');
84
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
95
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
106
const BellOnBundlerErrorPlugin = require('bell-on-bundler-error-plugin');
11-
const {hasProcessFlag, root, testDll} = require('./helpers.js');
7+
const { root } = require('./helpers.js');
128
const webpackMerge = require('webpack-merge');
139
const commonConfig = require('./webpack.common');
1410

15-
const EVENT = process.env.npm_lifecycle_event || '';
16-
const CONSTANTS = {
17-
AOT: EVENT.includes('aot'),
18-
ENV: JSON.stringify('development'),
19-
HMR: hasProcessFlag('hot'),
20-
HOST: HOST,
21-
PORT: DEV_PORT,
22-
REST_ENDPOINT_BASE: '\'' + process.env['npm_config_rest_endpoint'] + '\'',
23-
IFRAME_BASE: '\'' + process.env['npm_config_iframe_base'] + '\'',
24-
};
25-
26-
testDll();
27-
console.log(`Starting dev server on: http://${HOST}:${CONSTANTS.PORT}`);
2811

2912
module.exports = webpackMerge(commonConfig, {
3013

31-
// See https://github.com/webpack/webpack/issues/2145
32-
// Using SourceMapDevToolPlugin instead of devtool.
33-
// devtool: DEV_SOURCE_MAPS,
34-
3514
output: {
3615
filename: '[name].js',
3716
sourceMapFilename: '[name].map',
3817
path: root(OUTPUT_DIR),
39-
publicPath: 'http://' + CONSTANTS.HOST + ':' + CONSTANTS.PORT + '/',
4018
},
4119

4220
performance: {
@@ -51,9 +29,8 @@ module.exports = webpackMerge(commonConfig, {
5129
'@angularclass/hmr-loader?pretty=true&prod=false',
5230
'awesome-typescript-loader',
5331
'angular2-template-loader',
54-
'angular2-router-loader?loader=system&genDir=' + AOT_OUTPUT_DIR + '/src/app&aot=' + CONSTANTS.AOT
55-
],
56-
exclude: [/\.(spec|e2e|d)\.ts$/]
32+
'angular2-router-loader'
33+
]
5734
}
5835
]
5936
},
@@ -63,7 +40,6 @@ module.exports = webpackMerge(commonConfig, {
6340
// We need this plugin to detect a `--watch` mode. It may be removed later
6441
// after https://github.com/webpack/webpack/issues/3460 will be resolved.
6542
new CheckerPlugin(),
66-
new DefinePlugin(CONSTANTS),
6743
new DllReferencePlugin({
6844
context: '.',
6945
manifest: require(root(DLL_OUTPUT_DIR + `/polyfill-manifest.json`))
@@ -72,7 +48,9 @@ module.exports = webpackMerge(commonConfig, {
7248
context: '.',
7349
manifest: require(root(DLL_OUTPUT_DIR + `/vendor-manifest.json`))
7450
}),
75-
new CopyWebpackPlugin([{from: root(DLL_OUTPUT_DIR)}]),
51+
new DefinePlugin({
52+
ENV: JSON.stringify('development')
53+
}),
7654
new BellOnBundlerErrorPlugin(),
7755
new AddAssetHtmlPlugin({
7856
includeSourcemap: false,
@@ -86,8 +64,8 @@ module.exports = webpackMerge(commonConfig, {
8664
}),
8765
new SourceMapDevToolPlugin({
8866
filename: '[file].map',
89-
include: ['app'],
90-
exclude: ['vendor', 'polyfills'],
67+
include: ['src'],
68+
exclude: ['polyfills'],
9169
columns: false
9270
}),
9371
new LoaderOptionsPlugin({
@@ -97,13 +75,16 @@ module.exports = webpackMerge(commonConfig, {
9775
],
9876

9977
devServer: {
100-
contentBase: CONSTANTS.AOT ? root(AOT_OUTPUT_DIR) : root(OUTPUT_DIR),
101-
port: CONSTANTS.PORT,
78+
contentBase: root(OUTPUT_DIR),
79+
port: 8000,
10280
historyApiFallback: {
10381
disableDotRule: true,
10482
},
105-
host: CONSTANTS.HOST,
106-
watchOptions: DEV_SERVER_WATCH_OPTIONS,
107-
proxy: USE_DEV_SERVER_PROXY ? DEV_SERVER_PROXY_CONFIG : {}
83+
84+
watchOptions: {
85+
poll: undefined,
86+
aggregateTimeout: 300,
87+
ignored: /node_modules/
88+
}
10889
}
10990
});

ngc-webpack/config/webpack.prod.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { OUTPUT_DIR, AOT_OUTPUT_DIR, SRC_DIR } = require('./constants');
2+
const { DefinePlugin, NoEmitOnErrorsPlugin } = require('webpack');
3+
const webpack = require('webpack');
4+
5+
const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
6+
const { root } = require('./helpers.js');
7+
const webpackMerge = require('webpack-merge');
8+
const commonConfig = require('./webpack.common');
9+
10+
11+
module.exports = webpackMerge(commonConfig, {
12+
13+
devtool: 'source-map',
14+
15+
output: {
16+
filename: '[name].[hash].js',
17+
chunkFilename: '[id].[hash].chunk.js',
18+
path: root(OUTPUT_DIR)
19+
},
20+
21+
module: {
22+
rules: [
23+
{
24+
test: /\.ts$/,
25+
loaders: [
26+
'awesome-typescript-loader?configFileName=' + root('tsconfig.aot.json'),
27+
'angular2-template-loader',
28+
'angular2-router-loader?loader=system&genDir=' + AOT_OUTPUT_DIR + '/app&aot=true'
29+
],
30+
exclude: [/\.(spec|e2e|d)\.ts$/]
31+
}
32+
]
33+
},
34+
35+
plugins: [
36+
new webpack.optimize.CommonsChunkPlugin({
37+
name: ['app', 'vendors', 'polyfills']
38+
}),
39+
new DefinePlugin({
40+
ENV: JSON.stringify('production')
41+
}),
42+
new NoEmitOnErrorsPlugin(),
43+
new UglifyJsPlugin({
44+
beautify: false,
45+
comments: false
46+
})
47+
]
48+
});

ngc-webpack/package.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,17 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7+
"build:dll": "npm run clean:awcache && npm run clean:dll && webpack",
8+
"serve:dev": "webpack-dev-server --open --inline --hot",
9+
"start": "npm run serve:dev",
710

8-
"build:dll": "webpack",
9-
10-
"dev:hmr": "webpack-dev-server --open --inline --hot",
11-
12-
"build:watch": "npm run clean:aot && tsc -p tsconfig.json -w",
13-
"serve": "lite-server -c bs-config.dev.json",
14-
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
15-
"clean:jit": "rimraf build-jit",
16-
"copy:jit": "copyfiles -f \"src/**/*.html\" build-jit",
17-
"build:jit": "npm run clean:jit && npm run copy:jit && tsc -p tsconfig.jit.json && rollup -c rollup-config.jit.js",
18-
"serve:jit": "lite-server -c bs-config.jit.json",
19-
"check:jit": "source-map-explorer build-jit/build.js",
20-
"clean:aot": "rimraf \"src/**/*.{ngfactory.ts,js,js.map,metadata.json,ngsummary.json}\" aot build-aot",
21-
"build:aot": "npm run clean:aot && ngc -p tsconfig.aot.json && rollup -c rollup-config.aot.js",
22-
"serve:aot": "lite-server -c bs-config.aot.json",
23-
"check:aot": "source-map-explorer build-aot/build.js"
11+
"build:ngc": "npm run clean:ngc && ngc -p tsconfig.aot.json",
12+
"build:prod": "npm run build:ngc && npm run clean:prod && webpack",
13+
14+
"clean:dll": "rimraf dist/dll",
15+
"clean:awcache": "rimraf dist/awcache",
16+
"clean:ngc": "rimraf dist/aot",
17+
"clean:prod": "npm run clean:awcache && rimraf dist/prod"
2418
},
2519
"author": "",
2620
"license": "ISC",
@@ -30,22 +24,28 @@
3024
"@angular/core": "^4.0.2",
3125
"@angular/platform-browser": "^4.0.2",
3226
"@angular/platform-browser-dynamic": "^4.0.2",
33-
3427
"core-js": "^2.4.1",
3528
"rxjs": "^5.3.0",
3629
"zone.js": "^0.8.5"
3730
},
3831
"devDependencies": {
3932
"@angular/compiler-cli": "^4.0.2",
40-
41-
"awesome-typescript-loader": "3.1.2",
42-
"bell-on-bundler-error-plugin": "1.0.8",
4333
"@angularclass/hmr": "1.2.2",
4434
"@angularclass/hmr-loader": "3.0.2",
45-
35+
"@types/node": "^7.0.13",
36+
"add-asset-html-webpack-plugin": "^1.0.2",
37+
"angular2-router-loader": "^0.3.5",
38+
"angular2-template-loader": "^0.6.2",
39+
"awesome-typescript-loader": "3.1.2",
40+
"bell-on-bundler-error-plugin": "1.0.8",
41+
"html-webpack-plugin": "^2.28.0",
42+
"raw-loader": "^0.5.1",
4643
"source-map-explorer": "^1.3.3",
44+
"source-map-loader": "^0.2.1",
45+
"ts-helpers": "^1.1.2",
4746
"typescript": "^2.2.2",
4847
"webpack": "^2.4.1",
49-
"webpack-dev-server": "^2.4.2"
48+
"webpack-dev-server": "^2.4.2",
49+
"webpack-merge": "^4.1.0"
5050
}
5151
}

ngc-webpack/src/app/app.module.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { NgModule } from '@angular/core';
1+
import { NgModule, ApplicationRef } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
3+
import { removeNgStyles, createNewHosts, createInputTransfer } from '@angularclass/hmr';
34

45
import { AppComponent } from './app.component';
56
import { ChildComponent } from './child/child.component';
@@ -11,4 +12,25 @@ import { ChildComponent } from './child/child.component';
1112
providers: [],
1213
bootstrap: [AppComponent]
1314
})
14-
export class AppModule { }
15+
export class AppModule {
16+
constructor(public appRef: ApplicationRef) { }
17+
18+
hmrOnInit(store) {
19+
if (!store || !store.rootState) return;
20+
if ('restoreInputValues' in store) { store.restoreInputValues(); }
21+
this.appRef.tick();
22+
Object.keys(store).forEach(prop => delete store[prop]);
23+
}
24+
25+
hmrOnDestroy(store) {
26+
const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
27+
store.disposeOldHosts = createNewHosts(cmpLocation);
28+
store.restoreInputValues = createInputTransfer();
29+
removeNgStyles();
30+
}
31+
32+
hmrAfterDestroy(store) {
33+
store.disposeOldHosts();
34+
delete store.disposeOldHosts;
35+
}
36+
}

ngc-webpack/src/app/main.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

ngc-webpack/src/environment.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
declare var ENV: string;
2+
import { enableDebugTools, disableDebugTools } from '@angular/platform-browser';
3+
import { enableProdMode, ApplicationRef } from '@angular/core';
4+
5+
// Angular debug tools in the dev console
6+
// https://github.com/angular/angular/blob/86405345b781a9dc2438c0fbe3e9409245647019/TOOLS_JS.md
7+
let _decorateModuleRef = function identity<T>(value: T): T { return value; };
8+
9+
if ('production' === ENV) {
10+
// Production
11+
disableDebugTools();
12+
enableProdMode();
13+
} else {
14+
_decorateModuleRef = (modRef: any) => {
15+
const appRef = modRef.injector.get(ApplicationRef);
16+
const cmpRef = appRef.components[0];
17+
18+
let _ng = (<any>window).ng;
19+
enableDebugTools(cmpRef);
20+
(<any>window).ng.probe = _ng.probe;
21+
(<any>window).ng.coreTokens = _ng.coreTokens;
22+
return modRef;
23+
};
24+
}
25+
26+
export const decorateModuleRef = _decorateModuleRef;

0 commit comments

Comments
 (0)