Skip to content

Commit b9b279f

Browse files
committed
~ getting production running
1 parent 61b959b commit b9b279f

File tree

7 files changed

+315
-4
lines changed

7 files changed

+315
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ npm-debug.log
5555
/aot
5656
/src/**/*.js
5757
/src/**/*.js.map
58+
/src/**/*.ngfactory.ts
59+
/src/**/*.css.shim.ts
60+
/src/**/*.css.ts
5861

5962
# Doc #
6063
/doc/

config/webpack.aot.js

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
/**
2+
* @author: @AngularClass
3+
*/
4+
5+
const helpers = require('./helpers');
6+
const webpackMerge = require('webpack-merge'); // used to merge webpack configs
7+
const commonConfig = require('./webpack.common.js'); // the settings that are common to prod and dev
8+
const ngtools = require('@ngtools/webpack');
9+
10+
/**
11+
* Webpack Plugins
12+
*/
13+
const DedupePlugin = require('webpack/lib/optimize/DedupePlugin');
14+
const DefinePlugin = require('webpack/lib/DefinePlugin');
15+
const IgnorePlugin = require('webpack/lib/IgnorePlugin');
16+
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
17+
const NormalModuleReplacementPlugin = require('webpack/lib/NormalModuleReplacementPlugin');
18+
const ProvidePlugin = require('webpack/lib/ProvidePlugin');
19+
const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
20+
const WebpackMd5Hash = require('webpack-md5-hash');
21+
22+
/**
23+
* Webpack Constants
24+
*/
25+
const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
26+
const HOST = process.env.HOST || 'localhost';
27+
const PORT = process.env.PORT || 8080;
28+
const METADATA = webpackMerge(commonConfig({
29+
env: ENV
30+
}).metadata, {
31+
host: HOST,
32+
port: PORT,
33+
ENV: ENV,
34+
HMR: false
35+
});
36+
37+
module.exports = function (env) {
38+
return webpackMerge(commonConfig({
39+
env: ENV
40+
}), {
41+
42+
/**
43+
* Developer tool to enhance debugging
44+
*
45+
* See: http://webpack.github.io/docs/configuration.html#devtool
46+
* See: https://github.com/webpack/docs/wiki/build-performance#sourcemaps
47+
*/
48+
devtool: 'source-map',
49+
50+
/*
51+
* The entry point for the bundle
52+
* Our Angular.js app
53+
*
54+
* See: http://webpack.github.io/docs/configuration.html#entry
55+
*/
56+
entry: {
57+
58+
// 'polyfills': './src/polyfills.browser',
59+
// 'vendor': './src/vendor.browser',
60+
'main': './src/main.browser.aot'
61+
62+
},
63+
64+
/**
65+
* Options affecting the output of the compilation.
66+
*
67+
* See: http://webpack.github.io/docs/configuration.html#output
68+
*/
69+
output: {
70+
71+
/**
72+
* The output directory as absolute path (required).
73+
*
74+
* See: http://webpack.github.io/docs/configuration.html#output-path
75+
*/
76+
path: helpers.root('dist'),
77+
78+
/**
79+
* Specifies the name of each output file on disk.
80+
* IMPORTANT: You must not specify an absolute path here!
81+
*
82+
* See: http://webpack.github.io/docs/configuration.html#output-filename
83+
*/
84+
filename: '[name].[chunkhash].bundle.js',
85+
86+
/**
87+
* The filename of the SourceMaps for the JavaScript files.
88+
* They are inside the output.path directory.
89+
*
90+
* See: http://webpack.github.io/docs/configuration.html#output-sourcemapfilename
91+
*/
92+
sourceMapFilename: '[name].[chunkhash].bundle.map',
93+
94+
/**
95+
* The filename of non-entry chunks as relative path
96+
* inside the output.path directory.
97+
*
98+
* See: http://webpack.github.io/docs/configuration.html#output-chunkfilename
99+
*/
100+
chunkFilename: '[id].[chunkhash].chunk.js'
101+
102+
},
103+
104+
/**
105+
* Add additional plugins to the compiler.
106+
*
107+
* See: http://webpack.github.io/docs/configuration.html#plugins
108+
*/
109+
plugins: [
110+
111+
/**
112+
* Plugin: WebpackMd5Hash
113+
* Description: Plugin to replace a standard webpack chunkhash with md5.
114+
*
115+
* See: https://www.npmjs.com/package/webpack-md5-hash
116+
*/
117+
new WebpackMd5Hash(),
118+
119+
new ngtools.AotPlugin({
120+
tsConfigPath: './tsconfig.aot.json',
121+
baseDir: helpers.root('src'),
122+
entryModule: helpers.root('src/app/app.module') + '#AppModule'
123+
}),
124+
125+
/**
126+
* Plugin: DedupePlugin
127+
* Description: Prevents the inclusion of duplicate code into your bundle
128+
* and instead applies a copy of the function at runtime.
129+
*
130+
* See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin
131+
* See: https://github.com/webpack/docs/wiki/optimization#deduplication
132+
*/
133+
// new DedupePlugin(), // see: https://github.com/angular/angular-cli/issues/1587
134+
135+
/**
136+
* Plugin: DefinePlugin
137+
* Description: Define free variables.
138+
* Useful for having development builds with debug logging or adding global constants.
139+
*
140+
* Environment helpers
141+
*
142+
* See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin
143+
*/
144+
// NOTE: when adding more properties make sure you include them in custom-typings.d.ts
145+
new DefinePlugin({
146+
'ENV': JSON.stringify(METADATA.ENV),
147+
'HMR': METADATA.HMR,
148+
'process.env': {
149+
'ENV': JSON.stringify(METADATA.ENV),
150+
'NODE_ENV': JSON.stringify(METADATA.ENV),
151+
'HMR': METADATA.HMR,
152+
}
153+
}),
154+
155+
/**
156+
* Plugin: UglifyJsPlugin
157+
* Description: Minimize all JavaScript output of chunks.
158+
* Loaders are switched into minimizing mode.
159+
*
160+
* See: https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
161+
*/
162+
// NOTE: To debug prod builds uncomment //debug lines and comment //prod lines
163+
new UglifyJsPlugin({
164+
// beautify: true, //debug
165+
// mangle: false, //debug
166+
// dead_code: false, //debug
167+
// unused: false, //debug
168+
// deadCode: false, //debug
169+
// compress: {
170+
// screw_ie8: true,
171+
// keep_fnames: true,
172+
// drop_debugger: false,
173+
// dead_code: false,
174+
// unused: false
175+
// }, // debug
176+
// comments: true, //debug
177+
178+
179+
beautify: false, //prod
180+
mangle: {
181+
screw_ie8: true,
182+
keep_fnames: true
183+
}, //prod
184+
compress: {
185+
screw_ie8: true
186+
}, //prod
187+
comments: false //prod
188+
}),
189+
190+
/**
191+
* Plugin: NormalModuleReplacementPlugin
192+
* Description: Replace resources that matches resourceRegExp with newResource
193+
*
194+
* See: http://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
195+
*/
196+
197+
new NormalModuleReplacementPlugin(
198+
/angular2-hmr/,
199+
helpers.root('config/modules/angular2-hmr-prod.js')
200+
),
201+
202+
/**
203+
* Plugin: IgnorePlugin
204+
* Description: Don’t generate modules for requests matching the provided RegExp.
205+
*
206+
* See: http://webpack.github.io/docs/list-of-plugins.html#ignoreplugin
207+
*/
208+
209+
// new IgnorePlugin(/angular2-hmr/),
210+
211+
/**
212+
* Plugin: CompressionPlugin
213+
* Description: Prepares compressed versions of assets to serve
214+
* them with Content-Encoding
215+
*
216+
* See: https://github.com/webpack/compression-webpack-plugin
217+
*/
218+
// install compression-webpack-plugin
219+
// new CompressionPlugin({
220+
// regExp: /\.css$|\.html$|\.js$|\.map$/,
221+
// threshold: 2 * 1024
222+
// })
223+
224+
/**
225+
* Plugin LoaderOptionsPlugin (experimental)
226+
*
227+
* See: https://gist.github.com/sokra/27b24881210b56bbaff7
228+
*/
229+
new LoaderOptionsPlugin({
230+
debug: false,
231+
options: {
232+
233+
/**
234+
* Static analysis linter for TypeScript advanced options configuration
235+
* Description: An extensible linter for the TypeScript language.
236+
*
237+
* See: https://github.com/wbuchwalter/tslint-loader
238+
*/
239+
tslint: {
240+
emitErrors: true,
241+
failOnHint: true,
242+
resourcePath: 'src'
243+
},
244+
245+
246+
/**
247+
* Html loader advanced options
248+
*
249+
* See: https://github.com/webpack/html-loader#advanced-options
250+
*/
251+
// TODO: Need to workaround Angular 2's html syntax => #id [bind] (event) *ngFor
252+
htmlLoader: {
253+
minimize: true,
254+
removeAttributeQuotes: false,
255+
caseSensitive: true,
256+
customAttrSurround: [
257+
[/#/, /(?:)/],
258+
[/\*/, /(?:)/],
259+
[/\[?\(?/, /(?:)/]
260+
],
261+
customAttrAssign: [/\)?\]?=/]
262+
},
263+
264+
}
265+
}),
266+
267+
],
268+
269+
/*
270+
* Include polyfills or mocks for various node stuff
271+
* Description: Node configuration
272+
*
273+
* See: https://webpack.github.io/docs/configuration.html#node
274+
*/
275+
node: {
276+
global: true,
277+
crypto: 'empty',
278+
process: false,
279+
module: false,
280+
clearImmediate: false,
281+
setImmediate: false
282+
}
283+
284+
});
285+
}

config/webpack.common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ module.exports = function (options) {
154154
* See: http://webpack.github.io/docs/configuration.html#plugins
155155
*/
156156
plugins: [
157+
157158
new AssetsPlugin({
158159
path: helpers.root('dist'),
159160
filename: 'webpack-assets.json',
@@ -167,6 +168,7 @@ module.exports = function (options) {
167168
* See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
168169
*/
169170
new ForkCheckerPlugin(),
171+
170172
/*
171173
* Plugin: CommonsChunkPlugin
172174
* Description: Shares common code between the pages.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"homepage": "https://github.com/angularclass/angular2-webpack-starter",
1212
"license": "MIT",
1313
"scripts": {
14+
"aot": "ngc -p ./tsconfig.aot.json",
1415
"build:dev": "webpack --config config/webpack.dev.js --progress --profile",
1516
"build:docker": "npm run build:prod && docker build -t angular2-webpack-start:latest .",
1617
"build:prod": "webpack --config config/webpack.prod.js --progress --profile --bail",
18+
"build:aot": "webpack --config config/webpack.aot.js --progress --profile --bail",
1719
"build": "npm run build:dev",
1820
"ci": "npm run lint && npm test && npm run e2e",
1921
"clean:dist": "npm run rimraf -- dist",
@@ -80,6 +82,7 @@
8082
"zone.js": "~0.6.17"
8183
},
8284
"devDependencies": {
85+
"@ngtools/webpack": "^1.1.0",
8386
"@types/hammerjs": "^2.0.33",
8487
"@types/jasmine": "^2.2.34",
8588
"@types/node": "^6.0.38",

src/main.browser.aot.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Angular bootstraping
3+
*/
4+
import { platformBrowser } from '@angular/platform-browser';
5+
import { AppModuleNgFactory } from '../aot/src/app/app.module.ngfactory';
6+
7+
/*
8+
* Bootstrap our Angular app with a top level NgModule
9+
*/
10+
platformBrowser()
11+
.bootstrapModuleFactory(AppModuleNgFactory)
12+
.catch(err => console.error(err));
13+
14+
// needed for hmr
15+
// in prod this is replace for document ready
16+
// bootloader(main);

src/main.browser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
55
import { decorateModuleRef } from './app/environment';
66
import { bootloader } from '@angularclass/hmr';
7+
78
/*
89
* App Module
910
* our top level module that holds all of our components
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
"sourceMap": true,
1111
"noEmitHelpers": true,
1212
"strictNullChecks": false,
13-
"baseUrl": "./src",
13+
"rootDir": "./src",
14+
"outDir": "./dist",
1415
"lib": [
1516
"dom",
1617
"es6"
1718
],
1819
"types": [
19-
"hammerjs"
20+
"hammerjs",
21+
"node"
2022
]
2123
},
2224
"files": [
2325
"src/app/app.module.ts",
24-
"src/main.browser.ts",
2526
"src/custom-typings.d.ts"
2627
],
2728
"exclude": [
@@ -30,6 +31,6 @@
3031
],
3132
"angularCompilerOptions": {
3233
"genDir": "aot",
33-
"skipMetadataEmit": true
34+
"skipMetadataEmit" : true
3435
}
3536
}

0 commit comments

Comments
 (0)