1
1
'use strict' ;
2
2
3
+ var fs = require ( 'fs' ) ;
3
4
var path = require ( 'path' ) ;
4
5
var process = require ( 'process' ) ;
5
6
var gulp = require ( 'gulp' ) ;
@@ -11,30 +12,162 @@ var rev = require('gulp-rev');
11
12
var babel = require ( 'gulp-babel' ) ;
12
13
var gulpif = require ( 'gulp-if' ) ;
13
14
var plumber = require ( 'gulp-plumber' ) ;
14
- var teamcity = require ( 'eslint-teamcity' ) ;
15
+ var teamcityESLintFormatter = require ( 'eslint-teamcity' ) ;
16
+ var gulpWebpack = require ( 'gulp-webpack' ) ;
17
+ var webpack = require ( 'webpack' ) ;
18
+ var typings = require ( 'gulp-typings' ) ;
19
+ var tslint = require ( 'gulp-tslint' ) ;
20
+ var _ = require ( 'lodash' ) ;
21
+ var WebpackMd5Hash = require ( 'webpack-md5-hash' ) ;
22
+ var Server = require ( 'karma' ) . Server ;
15
23
16
- function lint ( scripts ) {
24
+ function lintJavaScript ( scripts ) {
17
25
return gulp . src ( scripts )
18
26
. pipe ( eslint ( ) )
19
- . pipe ( eslint . format ( process . env . TEAMCITY_VERSION ? teamcity : undefined ) )
27
+ . pipe ( eslint . format ( process . env . TEAMCITY_VERSION ? teamcityESLintFormatter : undefined ) )
20
28
. pipe ( eslint . failAfterError ( ) ) ;
21
29
}
22
30
23
- function createBundle ( bundleName , bundleFiles , outputPath , currentDirectory , catchErrors ) {
31
+ function lintTypeScript ( scripts ) {
32
+ return gulp . src ( scripts )
33
+ . pipe ( tslint ( {
34
+ formatter : process . env . TEAMCITY_VERSION ? 'tslint-teamcity-reporter' : undefined
35
+ } ) )
36
+ . pipe ( tslint . report ( ) ) ;
37
+ }
38
+
39
+ function createBundle ( bundleName , bundleFiles , outputPath , currentDirectory , watchMode ) {
24
40
console . log ( 'Creating script bundle: ' + bundleName ) ;
25
41
26
42
return gulp . src ( bundleFiles )
27
- . pipe ( gulpif ( catchErrors , plumber ( ) ) )
43
+ . pipe ( gulpif ( watchMode , plumber ( ) ) )
28
44
. pipe ( sourcemaps . init ( ) )
29
45
. pipe ( babel ( ) )
30
46
. pipe ( uglify ( ) )
31
47
. pipe ( concat ( { path : bundleName , cwd : currentDirectory } ) )
32
48
. pipe ( rev ( ) )
33
49
. pipe ( sourcemaps . write ( '.' ) )
34
- . pipe ( gulp . dest ( path . join ( outputPath ) ) ) ;
50
+ . pipe ( gulp . dest ( outputPath ) ) ;
51
+ }
52
+
53
+ function createWebpackBundle ( bundleName , entryScriptPath , outputPath , watchMode ) {
54
+ console . log ( 'Creating webpack script bundle: ' + bundleName ) ;
55
+
56
+ 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 ( [
74
+ new WebpackMd5Hash ( ) ,
75
+ new webpack . NoErrorsPlugin ( ) ,
76
+ new webpack . DefinePlugin ( {
77
+ 'process.env' : {
78
+ 'NODE_ENV' : JSON . stringify ( process . env . TEAMCITY_VERSION ? 'production' : 'development' )
79
+ }
80
+ } ) ,
81
+ process . env . TEAMCITY_VERSION && new webpack . optimize . UglifyJsPlugin ( {
82
+ compress : { warnings : false }
83
+ } ) ,
84
+ 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
+ } ) ;
108
+ }
109
+ ] , undefined ) ,
110
+ resolve : {
111
+ extensions : [ '' , '.webpack.js' , '.web.js' , '.ts' , '.tsx' , '.js' , '.jsx' ]
112
+ }
113
+ } ) )
114
+ . pipe ( gulp . dest ( outputPath ) ) ;
115
+ }
116
+
117
+ function restoreTypings ( typingsPaths ) {
118
+ return gulp . src ( typingsPaths )
119
+ . pipe ( typings ( ) ) ;
120
+ }
121
+
122
+ function runKarmaTests ( config , callback ) {
123
+ new Server ( {
124
+ basePath : '' ,
125
+ frameworks : config . frameworks ,
126
+ files : config . files ,
127
+ exclude : [ ] ,
128
+ preprocessors : {
129
+ '**/*.ts' : [ 'webpack' ] ,
130
+ '**/*.tsx' : [ 'webpack' ]
131
+ } ,
132
+ webpack : {
133
+ module : {
134
+ loaders : _ . concat (
135
+ [ {
136
+ test : / \. t s ( x ? ) $ / ,
137
+ loaders : [ 'ts' ] ,
138
+ exclude : / ( n o d e _ m o d u l e s ) /
139
+ } ] ,
140
+ config . webpack . loaders || [ ] ) ,
141
+ noParse : [
142
+ / \/ s i n o n \. j s / ,
143
+ ]
144
+ } ,
145
+ resolve : {
146
+ extensions : [ '' , '.webpack.js' , '.web.js' , '.ts' , '.tsx' , '.js' , '.jsx' ] ,
147
+ alias : {
148
+ sinon : 'sinon/pkg/sinon.js' ,
149
+ }
150
+ } ,
151
+ externals : config . webpack . externals
152
+ } ,
153
+ reporters : [ process . env . TEAMCITY_VERSION ? 'teamcity' : 'mocha' ] ,
154
+ mochaReporter : {
155
+ output : 'autowatch'
156
+ } ,
157
+ port : 9876 ,
158
+ colors : true ,
159
+ autoWatch : true ,
160
+ browsers : [ 'PhantomJS' ] ,
161
+ concurrency : Infinity ,
162
+ singleRun : ! config . watch
163
+ } , callback ) . start ( ) ;
35
164
}
36
165
37
166
module . exports = {
38
- lint : lint ,
39
- createBundle : createBundle
167
+ lintJavaScript : lintJavaScript ,
168
+ lintTypeScript : lintTypeScript ,
169
+ createBundle : createBundle ,
170
+ createWebpackBundle : createWebpackBundle ,
171
+ restoreTypings : restoreTypings ,
172
+ runKarmaTests : runKarmaTests
40
173
} ;
0 commit comments