@@ -30,6 +30,7 @@ var ts = require('gulp-typescript');
3030var del = require ( 'del' ) ;
3131var header = require ( 'gulp-header' ) ;
3232var replace = require ( 'gulp-replace' ) ;
33+ var filter = require ( 'gulp-filter' ) ;
3334
3435
3536/****************/
@@ -50,12 +51,35 @@ var paths = {
5051 ] ,
5152
5253 build : 'lib/' ,
54+
55+ curatedTypings : [ 'src/*.d.ts' ] ,
5356} ;
5457
55- // Create a separate project for buildProject that overrides the rootDir
58+ const TEMPORARY_TYPING_EXCLUDES = [
59+ '!lib/default-namespace.d.ts' ,
60+ '!lib/firebase-namespace.d.ts' ,
61+ '!lib/firebase-app.d.ts' ,
62+ '!lib/firebase-service.d.ts' ,
63+ '!lib/auth/*.d.ts' ,
64+ '!lib/database/*.d.ts' ,
65+ '!lib/firestore/*.d.ts' ,
66+ '!lib/instance-id/*.d.ts' ,
67+ '!lib/machine-learning/*.d.ts' ,
68+ '!lib/messaging/*.d.ts' ,
69+ '!lib/project-management/*.d.ts' ,
70+ '!lib/remote-config/*.d.ts' ,
71+ '!lib/security-rules/*.d.ts' ,
72+ '!lib/storage/*.d.ts' ,
73+ '!lib/utils/*.d.ts'
74+ ] ;
75+
76+ // Create a separate project for buildProject that overrides the rootDir.
5677// This ensures that the generated production files are in their own root
57- // rather than including both src and test in the lib dir.
58- var buildProject = ts . createProject ( 'tsconfig.json' , { rootDir : 'src' } ) ;
78+ // rather than including both src and test in the lib dir. Declaration
79+ // is used by TypeScript to determine if auto-generated typings should be
80+ // emitted.
81+ const declaration = process . env . TYPE_GENERATION_MODE === 'auto' ;
82+ var buildProject = ts . createProject ( 'tsconfig.json' , { rootDir : 'src' , declaration } ) ;
5983
6084var buildTest = ts . createProject ( 'tsconfig.json' ) ;
6185
@@ -71,16 +95,36 @@ gulp.task('cleanup', function() {
7195 ] ) ;
7296} ) ;
7397
98+ // Task used to compile the TypeScript project. If automatic typings
99+ // are set to be generated (determined by TYPE_GENERATION_MODE), declarations
100+ // for files terminating in -internal.d.ts are removed because we do not
101+ // want to expose internally used types to developers. As auto-generated
102+ // typings are a work-in-progress, we remove the *.d.ts files for modules
103+ // which we do not intend to auto-generate typings for yet.
74104gulp . task ( 'compile' , function ( ) {
75- return gulp . src ( paths . src )
105+ let workflow = gulp . src ( paths . src )
76106 // Compile Typescript into .js and .d.ts files
77107 . pipe ( buildProject ( ) )
78108
79109 // Add header
80- . pipe ( header ( banner ) )
81-
82- // Write to build directory
83- . pipe ( gulp . dest ( paths . build ) )
110+ . pipe ( header ( banner ) ) ;
111+
112+ // Exclude typings that are unintended (currently excludes all auto-generated
113+ // typings, but as services are refactored to auto-generate typings this will
114+ // change). Moreover, all *-internal.d.ts typings should not be exposed to
115+ // developers as it denotes internally used types.
116+ if ( declaration ) {
117+ const configuration = [
118+ 'lib/**/*.js' ,
119+ 'lib/**/*.d.ts' ,
120+ '!lib/**/*-internal.d.ts' ,
121+ ] . concat ( TEMPORARY_TYPING_EXCLUDES ) ;
122+
123+ workflow = workflow . pipe ( filter ( configuration ) ) ;
124+ }
125+
126+ // Write to build directory
127+ return workflow . pipe ( gulp . dest ( paths . build ) )
84128} ) ;
85129
86130/**
@@ -104,19 +148,23 @@ gulp.task('copyDatabase', function() {
104148} ) ;
105149
106150gulp . task ( 'copyTypings' , function ( ) {
107- return gulp . src ( 'src/*.d.ts' )
151+ let workflow = gulp . src ( 'src/*.d.ts' )
108152 // Add header
109- . pipe ( header ( banner ) )
110-
111- // Write to build directory
112- . pipe ( gulp . dest ( paths . build ) )
153+ . pipe ( header ( banner ) ) ;
154+
155+ if ( declaration ) {
156+ workflow = workflow . pipe ( filter ( paths . curatedTypings ) ) ;
157+ }
158+
159+ // Write to build directory
160+ return workflow . pipe ( gulp . dest ( paths . build ) )
113161} ) ;
114162
115163gulp . task ( 'compile_all' , gulp . series ( 'compile' , 'copyDatabase' , 'copyTypings' , 'compile_test' ) ) ;
116164
117165// Regenerates js every time a source file changes
118166gulp . task ( 'watch' , function ( ) {
119- gulp . watch ( paths . src . concat ( paths . test ) , { ignoreInitial : false } , gulp . series ( 'compile_all' ) ) ;
167+ gulp . watch ( paths . src . concat ( paths . test ) , { ignoreInitial : false } , gulp . series ( 'compile_all' ) ) ;
120168} ) ;
121169
122170// Build task
0 commit comments