1
- var gulp = require ( 'gulp' ) ;
2
- var elixir = require ( 'laravel-elixir' ) ;
3
- var ts = require ( 'gulp-typescript' ) ;
4
1
var 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
+
5
8
var _ = require ( 'underscore' ) ;
6
9
7
- var Task = elixir . Task ;
10
+ var $ = Elixir . Plugins ;
11
+ var config = Elixir . config ;
8
12
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' ) ;
10
16
11
- var pluginName = 'typescript' ;
12
- var assetPath = './' + elixir . config . assetsPath ;
17
+ Elixir . extend ( 'typescript' , function ( src , output , options ) {
18
+ var paths = prepGulpPaths ( src , output ) ;
13
19
14
- outputFolder = outputFolder || './public/js/' ;
15
- search = search || '/typescript/**/*.ts' ;
20
+ new Elixir . Task ( 'typescript' , function ( ) {
21
+ this . log ( paths . src , paths . output ) ;
16
22
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
+ }
20
34
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 )
24
40
. on ( 'error' , function ( e ) {
25
- new elixir . Notification ( ) . error ( e , 'TypeScript Compilation Failed!' ) ;
41
+ new Elixir . Notification ( ) . error ( e , 'TypeScript Compilation Failed!' ) ;
42
+
26
43
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
+ ) ;
32
51
} )
33
- . watch ( assetPath + '/typescript/**' ) ;
52
+ . watch ( path . join ( paths . src . baseDir , "**/*.ts" ) )
53
+ . ignore ( paths . output . path ) ;
34
54
} ) ;
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