@@ -13,36 +13,41 @@ const del = require("del");
1313const needsUpdate = require ( "./needsUpdate" ) ;
1414const mkdirp = require ( "./mkdirp" ) ;
1515const { reportDiagnostics } = require ( "./diagnostics" ) ;
16- const { PassThrough } = require ( "stream" ) ;
1716
1817class CompilationGulp extends gulp . Gulp {
1918 /**
20- * @param {import("gulp-help").GulpHelp | import("gulp").Gulp } gulp
19+ * @param {boolean } [verbose]
2120 */
22- constructor ( gulp ) {
23- super ( ) ;
24- // forward notifications to the outer gulp.
25- this . on ( "task_start" , e => gulp . emit ( "task_start" , e ) ) ;
26- this . on ( "task_stop" , e => gulp . emit ( "task_stop" , e ) ) ;
27- this . on ( "task_err" , e => gulp . emit ( "task_err" , e ) ) ;
28- this . on ( "task_not_found" , e => gulp . emit ( "task_not_found" , e ) ) ;
29- this . on ( "task_recursion" , e => gulp . emit ( "task_recursion" , e ) ) ;
30- this . on ( "err" , e => gulp . emit ( "err" , e ) ) ;
21+ fork ( verbose ) {
22+ const child = new ForkedGulp ( this . tasks ) ;
23+ if ( verbose ) {
24+ this . on ( "task_start" , e => gulp . emit ( "task_start" , e ) ) ;
25+ this . on ( "task_stop" , e => gulp . emit ( "task_stop" , e ) ) ;
26+ this . on ( "task_err" , e => gulp . emit ( "task_err" , e ) ) ;
27+ this . on ( "task_not_found" , e => gulp . emit ( "task_not_found" , e ) ) ;
28+ this . on ( "task_recursion" , e => gulp . emit ( "task_recursion" , e ) ) ;
29+ }
30+ return child ;
3131 }
32+ }
3233
33- dispose ( ) {
34- this . removeAllListeners ( ) ;
35- this . reset ( ) ;
34+ class ForkedGulp extends gulp . Gulp {
35+ /**
36+ * @param {gulp.Gulp["tasks"] } tasks
37+ */
38+ constructor ( tasks ) {
39+ super ( ) ;
40+ this . tasks = tasks ;
3641 }
37-
38- // Do not reset tasks when `gulp.start()` is called
42+
43+ // Do not reset tasks
3944 _resetAllTasks ( ) { }
4045 _resetSpecificTasks ( ) { }
4146 _resetTask ( ) { }
4247}
4348
4449// internal `Gulp` instance for compilation artifacts.
45- const compilationGulp = new CompilationGulp ( gulp ) ;
50+ const compilationGulp = new CompilationGulp ( ) ;
4651
4752/** @type {Map<ResolvedProjectSpec, ProjectGraph> } */
4853const projectGraphCache = new Map ( ) ;
@@ -60,7 +65,9 @@ function createCompiler(projectSpec, options) {
6065 const resolvedOptions = resolveProjectOptions ( options ) ;
6166 const resolvedProjectSpec = resolveProjectSpec ( projectSpec , resolvedOptions . paths , /*referrer*/ undefined ) ;
6267 const taskName = compileTaskName ( ensureCompileTask ( getOrCreateProjectGraph ( resolvedProjectSpec , resolvedOptions . paths ) , resolvedOptions ) , resolvedOptions . typescript ) ;
63- return ( ) => new Promise ( ( resolve , reject ) => compilationGulp . start ( taskName , err => err ? reject ( err ) : resolve ( err ) ) ) ;
68+ return ( ) => new Promise ( ( resolve , reject ) => compilationGulp
69+ . fork ( resolvedOptions . verbose )
70+ . start ( taskName , err => err ? reject ( err ) : resolve ( ) ) ) ;
6471}
6572exports . createCompiler = createCompiler ;
6673
@@ -74,13 +81,13 @@ exports.createCompiler = createCompiler;
7481 * @property {string } [cwd] The path to use for the current working directory. Defaults to `process.cwd()`.
7582 * @property {string } [base] The path to use as the base for relative paths. Defaults to `cwd`.
7683 * @property {string } [typescript] A module specifier or path (relative to gulpfile.js) to the version of TypeScript to use.
77- * @property {Hook } [js] Pipeline hook for .js file outputs. For multiple steps, use `stream-combiner`.
78- * @property {Hook } [dts] Pipeline hook for .d.ts file outputs. For multiple steps, use `stream-combiner`.
84+ * @property {Hook } [js] Pipeline hook for .js file outputs.
85+ * @property {Hook } [dts] Pipeline hook for .d.ts file outputs.
7986 * @property {boolean } [verbose] Indicates whether verbose logging is enabled.
8087 * @property {boolean } [force] Force recompilation (no up-to-date check).
8188 * @property {boolean } [inProcess] Indicates whether to run gulp-typescript in-process or out-of-process (default).
8289 *
83- * @typedef {NodeJS.ReadWriteStream | (( ) => NodeJS.ReadWriteStream) } Hook
90+ * @typedef {(stream: NodeJS.ReadableStream ) => NodeJS.ReadWriteStream } Hook
8491 */
8592function compile ( projectSpec , options ) {
8693 const compiler = createCompiler ( projectSpec , options ) ;
@@ -97,7 +104,9 @@ function createCleaner(projectSpec, options) {
97104 const paths = resolvePathOptions ( options ) ;
98105 const resolvedProjectSpec = resolveProjectSpec ( projectSpec , paths , /*referrer*/ undefined ) ;
99106 const taskName = cleanTaskName ( ensureCleanTask ( getOrCreateProjectGraph ( resolvedProjectSpec , paths ) ) ) ;
100- return ( ) => new Promise ( ( resolve , reject ) => compilationGulp . start ( taskName , err => err ? reject ( err ) : resolve ( err ) ) ) ;
107+ return ( ) => new Promise ( ( resolve , reject ) => compilationGulp
108+ . fork ( )
109+ . start ( taskName , err => err ? reject ( err ) : resolve ( ) ) ) ;
101110}
102111exports . createCleaner = createCleaner ;
103112
@@ -134,6 +143,7 @@ exports.addTypeScript = addTypeScript;
134143 * @property {string } [cwd] The path to use for the current working directory. Defaults to `process.cwd()`.
135144 * @property {CompilerOptions } [compilerOptions] Compiler option overrides.
136145 * @property {boolean } [force] Forces creation of the output project.
146+ * @property {string[] } [exclude] Files to exclude (relative to `cwd`)
137147 */
138148function flatten ( projectSpec , flattenedProjectSpec , options = { } ) {
139149 const paths = resolvePathOptions ( options ) ;
@@ -142,15 +152,16 @@ function flatten(projectSpec, flattenedProjectSpec, options = {}) {
142152 const resolvedOutputDirectory = path . dirname ( resolvedOutputSpec ) ;
143153 const resolvedProjectSpec = resolveProjectSpec ( projectSpec , paths , /*referrer*/ undefined ) ;
144154 const projectGraph = getOrCreateProjectGraph ( resolvedProjectSpec , paths ) ;
155+ const skipProjects = /**@type {Set<ProjectGraph> }*/ ( new Set ( ) ) ;
156+ const skipFiles = new Set ( options && options . exclude && options . exclude . map ( file => path . resolve ( paths . cwd , file ) ) ) ;
145157 recur ( projectGraph ) ;
146158
147- const config = {
148- extends : normalizeSlashes ( path . relative ( resolvedOutputDirectory , resolvedProjectSpec ) ) ,
149- compilerOptions : options . compilerOptions || { } ,
150- files
151- } ;
152-
153159 if ( options . force || needsUpdate ( files , resolvedOutputSpec ) ) {
160+ const config = {
161+ extends : normalizeSlashes ( path . relative ( resolvedOutputDirectory , resolvedProjectSpec ) ) ,
162+ compilerOptions : options . compilerOptions || { } ,
163+ files : files . map ( file => normalizeSlashes ( path . relative ( resolvedOutputDirectory , file ) ) )
164+ } ;
154165 mkdirp . sync ( resolvedOutputDirectory ) ;
155166 fs . writeFileSync ( resolvedOutputSpec , JSON . stringify ( config , undefined , 2 ) , "utf8" ) ;
156167 }
@@ -159,11 +170,16 @@ function flatten(projectSpec, flattenedProjectSpec, options = {}) {
159170 * @param {ProjectGraph } projectGraph
160171 */
161172 function recur ( projectGraph ) {
173+ if ( skipProjects . has ( projectGraph ) ) return ;
174+ skipProjects . add ( projectGraph ) ;
162175 for ( const ref of projectGraph . references ) {
163176 recur ( ref . target ) ;
164177 }
165- for ( const file of projectGraph . project . fileNames ) {
166- files . push ( normalizeSlashes ( path . relative ( resolvedOutputDirectory , path . resolve ( projectGraph . projectDirectory , file ) ) ) ) ;
178+ for ( let file of projectGraph . project . fileNames ) {
179+ file = path . resolve ( projectGraph . projectDirectory , file ) ;
180+ if ( skipFiles . has ( file ) ) continue ;
181+ skipFiles . add ( file ) ;
182+ files . push ( file ) ;
167183 }
168184 }
169185}
@@ -257,14 +273,6 @@ function resolveProjectOptions(options = {}) {
257273 } ;
258274}
259275
260- /**
261- * @param {Hook } hook
262- * @returns {NodeJS.ReadWriteStream }
263- */
264- function evaluateHook ( hook ) {
265- return ( typeof hook === "function" ? hook ( ) : hook ) || new PassThrough ( { objectMode : true } ) ;
266- }
267-
268276/**
269277 * @param {ResolvedProjectOptions } left
270278 * @param {ResolvedProjectOptions } right
@@ -448,31 +456,29 @@ function resolveDestPath(projectGraph, paths) {
448456
449457/**
450458 * @param {ProjectGraph } projectGraph
451- * @param {ResolvedProjectOptions } resolvedOptions
459+ * @param {ResolvedProjectOptions } options
452460 */
453- function ensureCompileTask ( projectGraph , resolvedOptions ) {
454- const projectGraphConfig = getOrCreateProjectGraphConfiguration ( projectGraph , resolvedOptions ) ;
455- projectGraphConfig . resolvedOptions = resolvedOptions = mergeProjectOptions ( resolvedOptions , resolvedOptions ) ;
461+ function ensureCompileTask ( projectGraph , options ) {
462+ const projectGraphConfig = getOrCreateProjectGraphConfiguration ( projectGraph , options ) ;
463+ projectGraphConfig . resolvedOptions = options = mergeProjectOptions ( options , options ) ;
456464 if ( ! projectGraphConfig . compileTaskCreated ) {
457- const deps = makeProjectReferenceCompileTasks ( projectGraph , resolvedOptions . typescript , resolvedOptions . paths ) ;
458- compilationGulp . task ( compileTaskName ( projectGraph , resolvedOptions . typescript ) , deps , ( ) => {
459- const destPath = resolveDestPath ( projectGraph , resolvedOptions . paths ) ;
465+ const deps = makeProjectReferenceCompileTasks ( projectGraph , options . typescript , options . paths ) ;
466+ compilationGulp . task ( compileTaskName ( projectGraph , options . typescript ) , deps , ( ) => {
467+ const destPath = resolveDestPath ( projectGraph , options . paths ) ;
460468 const { sourceMap, inlineSourceMap, inlineSources = false , sourceRoot, declarationMap } = projectGraph . project . options ;
461469 const configFilePath = projectGraph . project . options . configFilePath ;
462470 const sourceMapPath = inlineSourceMap ? undefined : "." ;
463471 const sourceMapOptions = { includeContent : inlineSources , sourceRoot, destPath } ;
464- const project = resolvedOptions . inProcess
465- ? tsc . createProject ( configFilePath , { typescript : require ( resolvedOptions . typescript . typescript ) } )
466- : tsc_oop . createProject ( configFilePath , { } , { typescript : resolvedOptions . typescript . typescript } ) ;
472+ const project = options . inProcess
473+ ? tsc . createProject ( configFilePath , { typescript : require ( options . typescript . typescript ) } )
474+ : tsc_oop . createProject ( configFilePath , { } , { typescript : options . typescript . typescript } ) ;
467475 const stream = project . src ( )
468- . pipe ( gulpif ( ! resolvedOptions . force , upToDate ( projectGraph . project , { verbose : resolvedOptions . verbose } ) ) )
476+ . pipe ( gulpif ( ! options . force , upToDate ( projectGraph . project , { verbose : options . verbose } ) ) )
469477 . pipe ( gulpif ( sourceMap || inlineSourceMap , sourcemaps . init ( ) ) )
470478 . pipe ( project ( ) ) ;
471- const js = stream . js
472- . pipe ( evaluateHook ( resolvedOptions . js ) )
479+ const js = ( options . js ? options . js ( stream . js ) : stream . js )
473480 . pipe ( gulpif ( sourceMap || inlineSourceMap , sourcemaps . write ( sourceMapPath , sourceMapOptions ) ) ) ;
474- const dts = stream . dts
475- . pipe ( evaluateHook ( resolvedOptions . dts ) )
481+ const dts = ( options . dts ? options . dts ( stream . dts ) : stream . dts )
476482 . pipe ( gulpif ( declarationMap , sourcemaps . write ( sourceMapPath , sourceMapOptions ) ) ) ;
477483 return merge2 ( [ js , dts ] )
478484 . pipe ( gulp . dest ( destPath ) ) ;
0 commit comments