1- var gulp = require ( 'gulp' ) ;
2- var elixir = require ( 'laravel-elixir' ) ;
3- var ts = require ( 'gulp-typescript' ) ;
41var concat = require ( 'gulp-concat' ) ;
2+ var ts = require ( 'gulp-typescript' ) ;
3+ var gulp = require ( 'gulp' ) ;
4+ var Elixir = require ( 'laravel-elixir' ) ;
5+ var fileExists = require ( 'file-exists' ) ;
6+ var path = require ( 'path' ) ;
7+
58var _ = require ( 'underscore' ) ;
69
7- var Task = elixir . Task ;
10+ var $ = Elixir . Plugins ;
11+ var config = Elixir . config ;
812
9- elixir . extend ( 'typescript' , function ( outputFileName , outputFolder , search , options ) {
13+ // overwrite elixir config values
14+ var tsFolder = 'resources/assets/typescript' ; // would be config.get('assets.js.typescript.folder');
15+ var tsOutput = config . get ( 'public.js.outputFolder' ) ;
1016
11- var pluginName = 'typescript' ;
12- var assetPath = './' + elixir . config . assetsPath ;
17+ Elixir . extend ( 'typescript' , function ( src , output , options ) {
18+ var paths = prepGulpPaths ( src , output ) ;
1319
14- outputFolder = outputFolder || './public/js/' ;
15- search = search || '/typescript/**/*.ts' ;
20+ new Elixir . Task ( 'typescript' , function ( ) {
21+ this . log ( paths . src , paths . output ) ;
1622
17- options = _ . extend ( {
18- sortOutput : true
19- } , options ) ;
23+ // check if there is an tsconfig.json file --> initialize ts project
24+ var tsProject = null ;
25+ var tsConfigPath = path . join ( tsFolder , 'tsconfig.json' ) ;
26+ if ( fileExists ( tsConfigPath ) ) {
27+ tsProject = ts . createProject ( tsConfigPath , options ) ;
28+ } else {
29+ // useful default options
30+ options = _ . extend ( {
31+ sortOutput : true
32+ } , options ) ;
33+ }
2034
21- new Task ( pluginName , function ( ) {
22- var tsResult = gulp . src ( assetPath + search )
23- . pipe ( ts ( options ) )
35+ return (
36+ gulp
37+ . src ( paths . src . path )
38+ . pipe ( $ . if ( config . sourcemaps , $ . sourcemaps . init ( ) ) )
39+ . pipe ( ts ( tsProject == null ? options : tsProject )
2440 . on ( 'error' , function ( e ) {
25- new elixir . Notification ( ) . error ( e , 'TypeScript Compilation Failed!' ) ;
41+ new Elixir . Notification ( ) . error ( e , 'TypeScript Compilation Failed!' ) ;
42+
2643 this . emit ( 'end' ) ;
27- } ) ;
28- return tsResult
29- . pipe ( concat ( outputFileName ) )
30- . pipe ( gulp . dest ( outputFolder ) ) ;
31- . pipe ( new elixir . Notification ( 'TypeScript Compiled!' ) ) ;
44+ } ) )
45+ . pipe ( $ . concat ( paths . output . name ) )
46+ . pipe ( $ . if ( config . production , $ . uglify ( ) ) )
47+ . pipe ( $ . if ( config . sourcemaps , $ . sourcemaps . write ( '.' ) ) )
48+ . pipe ( gulp . dest ( paths . output . baseDir ) )
49+ . pipe ( new Elixir . Notification ( 'TypeScript Compiled!' ) )
50+ ) ;
3251 } )
33- . watch ( assetPath + '/typescript/**' ) ;
52+ . watch ( path . join ( paths . src . baseDir , "**/*.ts" ) )
53+ . ignore ( paths . output . path ) ;
3454} ) ;
55+
56+ /**
57+ * Prep the Gulp src and output paths.
58+ *
59+ * @param {string|Array } src
60+ * @param {string|null } output
61+ * @return {GulpPaths }
62+ */
63+ var prepGulpPaths = function ( src , output ) {
64+ return new Elixir . GulpPaths ( )
65+ . src ( src , tsFolder )
66+ . output ( output || tsOutput , 'app.js' ) ;
67+ } ;
0 commit comments