@@ -50,27 +50,53 @@ function createBundle(bundleName, bundleFiles, outputPath, currentDirectory, wat
50
50
. pipe ( gulp . dest ( outputPath ) ) ;
51
51
}
52
52
53
- function createWebpackBundle ( bundleName , entryScriptPath , outputPath , watchMode ) {
54
- console . log ( 'Creating webpack script bundle: ' + bundleName ) ;
55
-
53
+ function createWebpackManifestWriter ( bundleName , watchMode ) {
54
+ return function ( stats ) {
55
+ if ( ! watchMode && stats . compilation . errors && stats . compilation . errors . length ) {
56
+ console . error ( 'Failed to generate webpack: ' + bundleName ) ;
57
+
58
+ stats . compilation . errors . forEach ( function ( error ) {
59
+ console . error ( 'in ' + error . file + ':' ) ;
60
+ console . error ( error . message ) ;
61
+ } ) ;
62
+
63
+ throw new Error ( 'Error generating webpack' ) ;
64
+ }
65
+
66
+ // TODO: introduce a file lock here to avoid any concurrency issues (for now we just process these serially)
67
+ var manifestPath = path . join ( process . cwd ( ) , 'rev-manifest.json' ) ;
68
+
69
+ var hashedFileName = Object . keys ( stats . compilation . assets )
70
+ . filter ( function ( key ) { return key . endsWith ( '.js' ) ; } ) [ 0 ] ;
71
+
72
+ var manifestContents = fs . existsSync ( manifestPath ) ? JSON . parse ( fs . readFileSync ( manifestPath , 'utf8' ) ) : { } ;
73
+ manifestContents [ bundleName ] = hashedFileName ;
74
+
75
+ fs . writeFileSync ( manifestPath , JSON . stringify ( manifestContents , null , ' ' ) ) ;
76
+ } ;
77
+ }
78
+
79
+ function createWebpackConfig ( bundleName , entryScriptPath , watchMode , additionalWebpackConfig ) {
80
+ var webpackConfig = additionalWebpackConfig || { } ;
56
81
var baseName = bundleName . replace ( / ( .* ) \. .* $ / , '$1' ) ;
57
-
58
- return gulp . src ( entryScriptPath )
59
- . pipe ( gulpif ( watchMode , plumber ( ) ) )
60
- . pipe ( gulpWebpack ( {
61
- devtool : 'source-map' ,
62
- entry : { bundle : entryScriptPath } ,
63
- exclude : { } ,
64
- output : { filename : baseName + '-[chunkhash].js' } ,
65
- watch : watchMode ,
66
- module : {
67
- loaders : [ {
68
- test : / \. t s ( x ? ) $ / ,
69
- loaders : [ 'ts' ] ,
70
- exclude : / ( n o d e _ m o d u l e s ) /
71
- } ]
72
- } ,
73
- plugins : _ . without ( [
82
+ var loaders = _ . concat ( [ {
83
+ test : / \. t s ( x ? ) $ / ,
84
+ loaders : [ 'ts' ] ,
85
+ exclude : / ( n o d e _ m o d u l e s ) /
86
+ } ] , webpackConfig . loaders || [ ] ) ;
87
+
88
+ return {
89
+ devtool : 'source-map' ,
90
+ entry : { bundle : entryScriptPath } ,
91
+ exclude : { } ,
92
+ output : { filename : baseName + '-[chunkhash].js' } ,
93
+ watch : watchMode ,
94
+ module : {
95
+ loaders : loaders
96
+ } ,
97
+ externals : webpackConfig . externals ,
98
+ plugins : _ . chain ( webpackConfig . plugins || [ ] )
99
+ . concat ( [
74
100
new WebpackMd5Hash ( ) ,
75
101
new webpack . NoErrorsPlugin ( ) ,
76
102
new webpack . DefinePlugin ( {
@@ -82,35 +108,25 @@ function createWebpackBundle(bundleName, entryScriptPath, outputPath, watchMode)
82
108
compress : { warnings : false }
83
109
} ) ,
84
110
function ( ) {
85
- this . plugin ( 'done' , function ( stats ) {
86
- if ( ! watchMode && stats . compilation . errors && stats . compilation . errors . length ) {
87
- console . error ( 'Failed to generate webpack: ' + bundleName ) ;
88
-
89
- stats . compilation . errors . forEach ( function ( error ) {
90
- console . error ( 'in ' + error . file + ':' ) ;
91
- console . error ( error . message ) ;
92
- } ) ;
93
-
94
- throw new Error ( 'Error generating webpack' ) ;
95
- }
96
-
97
- // TODO: introduce a file lock here to avoid any concurrency issues (for now we just process these serially)
98
- var manifestPath = path . join ( process . cwd ( ) , 'rev-manifest.json' ) ;
99
-
100
- var hashedFileName = Object . keys ( stats . compilation . assets )
101
- . filter ( function ( key ) { return key . endsWith ( '.js' ) ; } ) [ 0 ] ;
102
-
103
- var manifestContents = fs . existsSync ( manifestPath ) ? JSON . parse ( fs . readFileSync ( manifestPath , 'utf8' ) ) : { } ;
104
- manifestContents [ bundleName ] = hashedFileName ;
105
-
106
- fs . writeFileSync ( manifestPath , JSON . stringify ( manifestContents , null , ' ' ) ) ;
107
- } ) ;
111
+ this . plugin ( 'done' , createWebpackManifestWriter ( bundleName , watchMode ) ) ;
108
112
}
109
- ] , undefined ) ,
110
- resolve : {
111
- extensions : [ '' , '.webpack.js' , '.web.js' , '.ts' , '.tsx' , '.js' , '.jsx' ]
112
- }
113
- } ) )
113
+ ] )
114
+ . without ( undefined )
115
+ . value ( ) ,
116
+ resolve : {
117
+ extensions : [ '' , '.webpack.js' , '.web.js' , '.ts' , '.tsx' , '.js' , '.jsx' ]
118
+ }
119
+ } ;
120
+ }
121
+
122
+ function createWebpackBundle ( bundleName , entryScriptPath , outputPath , watchMode , additionalWebpackConfig ) {
123
+ console . log ( 'Creating webpack script bundle: ' + bundleName ) ;
124
+
125
+ var webpackConfig = createWebpackConfig ( bundleName , entryScriptPath , watchMode , additionalWebpackConfig ) ;
126
+
127
+ return gulp . src ( entryScriptPath )
128
+ . pipe ( gulpif ( watchMode , plumber ( ) ) )
129
+ . pipe ( gulpWebpack ( webpackConfig ) )
114
130
. pipe ( gulp . dest ( outputPath ) ) ;
115
131
}
116
132
@@ -130,6 +146,7 @@ function runKarmaTests(config, callback) {
130
146
'**/*.tsx' : [ 'webpack' ]
131
147
} ,
132
148
webpack : {
149
+ plugins : config . webpack . plugins || [ ] ,
133
150
module : {
134
151
loaders : _ . concat (
135
152
[ {
0 commit comments