11'use strict' ;
22
3+ var fs = require ( 'fs' ) ;
34var path = require ( 'path' ) ;
45var process = require ( 'process' ) ;
56var gulp = require ( 'gulp' ) ;
@@ -11,30 +12,162 @@ var rev = require('gulp-rev');
1112var babel = require ( 'gulp-babel' ) ;
1213var gulpif = require ( 'gulp-if' ) ;
1314var 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 ;
1523
16- function lint ( scripts ) {
24+ function lintJavaScript ( scripts ) {
1725 return gulp . src ( scripts )
1826 . pipe ( eslint ( ) )
19- . pipe ( eslint . format ( process . env . TEAMCITY_VERSION ? teamcity : undefined ) )
27+ . pipe ( eslint . format ( process . env . TEAMCITY_VERSION ? teamcityESLintFormatter : undefined ) )
2028 . pipe ( eslint . failAfterError ( ) ) ;
2129}
2230
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 ) {
2440 console . log ( 'Creating script bundle: ' + bundleName ) ;
2541
2642 return gulp . src ( bundleFiles )
27- . pipe ( gulpif ( catchErrors , plumber ( ) ) )
43+ . pipe ( gulpif ( watchMode , plumber ( ) ) )
2844 . pipe ( sourcemaps . init ( ) )
2945 . pipe ( babel ( ) )
3046 . pipe ( uglify ( ) )
3147 . pipe ( concat ( { path : bundleName , cwd : currentDirectory } ) )
3248 . pipe ( rev ( ) )
3349 . 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 ( ) ;
35164}
36165
37166module . 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
40173} ;
0 commit comments