Skip to content

Commit a23ee43

Browse files
committed
chore(webpack): use helpers file
1 parent f7a7971 commit a23ee43

3 files changed

Lines changed: 53 additions & 135 deletions

File tree

webpack.config.js

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// @AngularClass
22

3-
/*
4-
* Helper: root(), and rootDir() are defined at the bottom
5-
*/
6-
var path = require('path');
73
var webpack = require('webpack');
8-
var CopyWebpackPlugin = require('copy-webpack-plugin');
9-
var HtmlWebpackPlugin = require('html-webpack-plugin');
4+
var helpers = require('./helpers');
5+
6+
var CopyWebpackPlugin = require('copy-webpack-plugin');
7+
var HtmlWebpackPlugin = require('html-webpack-plugin');
8+
109
var ENV = process.env.ENV = process.env.NODE_ENV = 'development';
1110

1211
var metadata = {
@@ -19,7 +18,7 @@ var metadata = {
1918
/*
2019
* Config
2120
*/
22-
module.exports = {
21+
module.exports = helpers.validate({
2322
// static data for index.html
2423
metadata: metadata,
2524
// for faster builds use 'eval'
@@ -32,29 +31,21 @@ module.exports = {
3231

3332
// Config for our build files
3433
output: {
35-
path: root('dist'),
36-
filename: '[name].bundle.js',
37-
sourceMapFilename: '[name].map',
38-
chunkFilename: '[id].chunk.js'
34+
path: helpers.root('dist')
3935
},
4036

4137
resolve: {
42-
// ensure loader extensions match
43-
extensions: prepend(['.ts','.js','.json','.css','.html'], '.async') // ensure .async.ts etc also works
38+
extensions: ['', '.ts', '.async.ts', '.js']
4439
},
4540

4641
module: {
4742
preLoaders: [
48-
// { test: /\.ts$/, loader: 'tslint-loader', exclude: [ root('node_modules') ] },
49-
// TODO(gdi2290): `exclude: [ root('node_modules/rxjs') ]` fixed with rxjs 5 beta.2 release
50-
{ test: /\.js$/, loader: "source-map-loader", exclude: [ root('node_modules/rxjs') ] }
43+
// TODO(gdi2290): `exclude: [ helpers.root('node_modules/rxjs') ]` fixed with rxjs 5 beta.3 release
44+
{ test: /\.js$/, loader: "source-map-loader", exclude: [ helpers.root('node_modules/rxjs') ] }
5145
],
5246
loaders: [
53-
// Support Angular 2 async routes via .async.ts
54-
{ test: /\.async\.ts$/, loaders: ['es6-promise-loader', 'ts-loader'], exclude: [ /\.(spec|e2e)\.ts$/ ] },
55-
5647
// Support for .ts files.
57-
{ test: /\.ts$/, loader: 'ts-loader', exclude: [ /\.(spec|e2e|async)\.ts$/ ] },
48+
{ test: /\.ts$/, loader: 'ts-loader', exclude: [ /\.(spec|e2e)\.ts$/ ] },
5849

5950
// Support for *.json files.
6051
{ test: /\.json$/, loader: 'json-loader' },
@@ -63,9 +54,8 @@ module.exports = {
6354
{ test: /\.css$/, loader: 'raw-loader' },
6455

6556
// support for .html as raw text
66-
{ test: /\.html$/, loader: 'raw-loader', exclude: [ root('src/index.html') ] }
57+
{ test: /\.html$/, loader: 'raw-loader', exclude: [ helpers.root('src/index.html') ] }
6758

68-
// if you add a loader include the resolve file extension above
6959
]
7060
},
7161

@@ -86,11 +76,7 @@ module.exports = {
8676
],
8777

8878
// Other module loader config
89-
tslint: {
90-
emitErrors: false,
91-
failOnHint: false,
92-
resourcePath: 'src'
93-
},
79+
9480
// our Webpack Development Server config
9581
devServer: {
9682
port: metadata.port,
@@ -101,25 +87,4 @@ module.exports = {
10187
},
10288
// we need this due to problems with es6-shim
10389
node: {global: 'window', progress: false, crypto: 'empty', module: false, clearImmediate: false, setImmediate: false}
104-
};
105-
106-
// Helper functions
107-
108-
function root(args) {
109-
args = Array.prototype.slice.call(arguments, 0);
110-
return path.join.apply(path, [__dirname].concat(args));
111-
}
112-
113-
function prepend(extensions, args) {
114-
args = args || [];
115-
if (!Array.isArray(args)) { args = [args] }
116-
return extensions.reduce(function(memo, val) {
117-
return memo.concat(val, args.map(function(prefix) {
118-
return prefix + val
119-
}));
120-
}, ['']);
121-
}
122-
function rootNode(args) {
123-
args = Array.prototype.slice.call(arguments, 0);
124-
return root.apply(path, ['node_modules'].concat(args));
125-
}
90+
});

webpack.prod.config.js

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/*
44
* Helper: root(), and rootDir() are defined at the bottom
55
*/
6-
var path = require('path');
7-
var zlib = require('zlib');
6+
var helpers = require('./helpers');
87
// Webpack Plugins
98
var webpack = require('webpack');
109
var ProvidePlugin = require('webpack/lib/ProvidePlugin');
@@ -32,11 +31,12 @@ var metadata = {
3231
/*
3332
* Config
3433
*/
35-
module.exports = {
34+
module.exports = helpers.validate({
3635
// static data for index.html
3736
metadata: metadata,
38-
// for faster builds use 'eval'
37+
3938
devtool: 'source-map',
39+
cache: false,
4040
debug: false,
4141

4242
entry: {
@@ -46,7 +46,7 @@ module.exports = {
4646

4747
// Config for our build files
4848
output: {
49-
path: root('dist'),
49+
path: helpers.root('dist'),
5050
filename: '[name].[chunkhash].bundle.js',
5151
sourceMapFilename: '[name].[chunkhash].bundle.map',
5252
chunkFilename: '[id].[chunkhash].chunk.js'
@@ -55,7 +55,7 @@ module.exports = {
5555
resolve: {
5656
cache: false,
5757
// ensure loader extensions match
58-
extensions: prepend(['.ts','.js','.json','.css','.html'], '.async') // ensure .async.ts etc also works
58+
extensions: ['', '.ts','.js']
5959
},
6060

6161
module: {
@@ -64,24 +64,19 @@ module.exports = {
6464
test: /\.ts$/,
6565
loader: 'tslint-loader',
6666
exclude: [
67-
root('node_modules')
67+
helpers.root('node_modules')
6868
]
6969
},
7070
{
7171
test: /\.js$/,
72-
loader: "source-map-loader",
72+
loader: 'source-map-loader',
7373
exclude: [
74-
root('node_modules/rxjs')
74+
helpers.root('node_modules/rxjs')
7575
]
7676
}
7777
],
7878
loaders: [
7979
// Support Angular 2 async routes via .async.ts
80-
{
81-
test: /\.async\.ts$/,
82-
loaders: ['es6-promise-loader', 'ts-loader'],
83-
exclude: [ /\.(spec|e2e)\.ts$/ ]
84-
},
8580
// Support for .ts files.
8681
{
8782
test: /\.ts$/,
@@ -93,19 +88,32 @@ module.exports = {
9388
'noEmitHelpers': true,
9489
}
9590
},
96-
exclude: [ /\.(spec|e2e|async)\.ts$/ ]
91+
exclude: [
92+
/\.(spec|e2e)\.ts$/
93+
]
9794
},
9895

9996
// Support for *.json files.
100-
{ test: /\.json$/, loader: 'json-loader' },
97+
{
98+
test: /\.json$/,
99+
loader: 'json-loader'
100+
},
101101

102102
// Support for CSS as raw text
103-
{ test: /\.css$/, loader: 'raw-loader' },
103+
{
104+
test: /\.css$/,
105+
loader: 'raw-loader'
106+
},
104107

105108
// support for .html as raw text
106-
{ test: /\.html$/, loader: 'raw-loader', exclude: [ root('src/index.html') ] }
109+
{
110+
test: /\.html$/,
111+
loader: 'raw-loader',
112+
exclude: [
113+
helpers.root('src/index.html')
114+
]
115+
}
107116

108-
// if you add a loader include the file extension
109117
]
110118
},
111119

@@ -158,13 +166,13 @@ module.exports = {
158166
// TODO(mastertinner): enable mangling as soon as angular2 beta.4 is out
159167
// mangle: { screw_ie8 : true },//prod
160168
mangle: false,
161-
compress : { screw_ie8 : true},//prod
169+
compress : { screw_ie8 : true },//prod
162170
comments: false//prod
163171

164172
}),
165173
// include uglify in production
166174
new CompressionPlugin({
167-
algorithm: gzipMaxLevel,
175+
algorithm: helpers.gzipMaxLevel,
168176
regExp: /\.css$|\.html$|\.js$|\.map$/,
169177
threshold: 2 * 1024
170178
})
@@ -185,29 +193,4 @@ module.exports = {
185193
clearImmediate: false,
186194
setImmediate: false
187195
}
188-
};
189-
190-
// Helper functions
191-
function gzipMaxLevel(buffer, callback) {
192-
return zlib['gzip'](buffer, {level: 9}, callback)
193-
}
194-
195-
function root(args) {
196-
args = Array.prototype.slice.call(arguments, 0);
197-
return path.join.apply(path, [__dirname].concat(args));
198-
}
199-
200-
function rootNode(args) {
201-
args = Array.prototype.slice.call(arguments, 0);
202-
return root.apply(path, ['node_modules'].concat(args));
203-
}
204-
205-
function prepend(extensions, args) {
206-
args = args || [];
207-
if (!Array.isArray(args)) { args = [args] }
208-
return extensions.reduce(function(memo, val) {
209-
return memo.concat(val, args.map(function(prefix) {
210-
return prefix + val
211-
}));
212-
}, ['']);
213-
}
196+
});

webpack.test.config.js

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// @AngularClass
22

3-
/*
4-
* Helper: root(), and rootDir() are defined at the bottom
5-
*/
6-
var path = require('path');
3+
var helpers = require('./helpers');
74
// Webpack Plugins
85
var ProvidePlugin = require('webpack/lib/ProvidePlugin');
96
var DefinePlugin = require('webpack/lib/DefinePlugin');
@@ -12,10 +9,9 @@ var ENV = process.env.ENV = process.env.NODE_ENV = 'test';
129
/*
1310
* Config
1411
*/
15-
module.exports = {
12+
module.exports = helpers.validate({
1613
resolve: {
17-
cache: false,
18-
extensions: prepend(['.ts','.js','.json','.css','.html'], '.async') // ensure .async.ts etc also works
14+
extensions: ['', '.ts','.js']
1915
},
2016
devtool: 'inline-source-map',
2117
module: {
@@ -24,23 +20,18 @@ module.exports = {
2420
test: /\.ts$/,
2521
loader: 'tslint-loader',
2622
exclude: [
27-
root('node_modules')
23+
helpers.root('node_modules')
2824
]
2925
},
3026
{
3127
test: /\.js$/,
3228
loader: "source-map-loader",
3329
exclude: [
34-
root('node_modules/rxjs')
30+
helpers.root('node_modules/rxjs')
3531
]
3632
}
3733
],
3834
loaders: [
39-
{
40-
test: /\.async\.ts$/,
41-
loaders: ['es6-promise-loader', 'ts-loader'],
42-
exclude: [ /\.(spec|e2e)\.ts$/ ]
43-
},
4435
{
4536
test: /\.ts$/,
4637
loader: 'ts-loader',
@@ -60,7 +51,7 @@ module.exports = {
6051
// instrument only testing sources with Istanbul
6152
{
6253
test: /\.(js|ts)$/,
63-
include: root('src'),
54+
include: helpers.root('src'),
6455
loader: 'istanbul-instrumenter-loader',
6556
exclude: [
6657
/\.(e2e|spec)\.ts$/,
@@ -69,8 +60,8 @@ module.exports = {
6960
}
7061
],
7162
noParse: [
72-
root('zone.js/dist'),
73-
root('angular2/bundles')
63+
helpers.root('zone.js/dist'),
64+
helpers.root('angular2/bundles')
7465
]
7566
},
7667
stats: { colors: true, reasons: true },
@@ -101,26 +92,5 @@ module.exports = {
10192
clearImmediate: false,
10293
setImmediate: false
10394
}
104-
};
105-
106-
// Helper functions
107-
108-
function root(args) {
109-
args = Array.prototype.slice.call(arguments, 0);
110-
return path.join.apply(path, [__dirname].concat(args));
111-
}
112-
113-
function rootNode(args) {
114-
args = Array.prototype.slice.call(arguments, 0);
115-
return root.apply(path, ['node_modules'].concat(args));
116-
}
95+
});
11796

118-
function prepend(extensions, args) {
119-
args = args || [];
120-
if (!Array.isArray(args)) { args = [args] }
121-
return extensions.reduce(function(memo, val) {
122-
return memo.concat(val, args.map(function(prefix) {
123-
return prefix + val
124-
}));
125-
}, ['']);
126-
}

0 commit comments

Comments
 (0)