@@ -13,36 +13,41 @@ const del = require("del");
13
13
const needsUpdate = require ( "./needsUpdate" ) ;
14
14
const mkdirp = require ( "./mkdirp" ) ;
15
15
const { reportDiagnostics } = require ( "./diagnostics" ) ;
16
- const { PassThrough } = require ( "stream" ) ;
17
16
18
17
class CompilationGulp extends gulp . Gulp {
19
18
/**
20
- * @param {import("gulp-help").GulpHelp | import("gulp").Gulp } gulp
19
+ * @param {boolean } [verbose]
21
20
*/
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 ;
31
31
}
32
+ }
32
33
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 ;
36
41
}
37
-
38
- // Do not reset tasks when `gulp.start()` is called
42
+
43
+ // Do not reset tasks
39
44
_resetAllTasks ( ) { }
40
45
_resetSpecificTasks ( ) { }
41
46
_resetTask ( ) { }
42
47
}
43
48
44
49
// internal `Gulp` instance for compilation artifacts.
45
- const compilationGulp = new CompilationGulp ( gulp ) ;
50
+ const compilationGulp = new CompilationGulp ( ) ;
46
51
47
52
/** @type {Map<ResolvedProjectSpec, ProjectGraph> } */
48
53
const projectGraphCache = new Map ( ) ;
@@ -60,7 +65,9 @@ function createCompiler(projectSpec, options) {
60
65
const resolvedOptions = resolveProjectOptions ( options ) ;
61
66
const resolvedProjectSpec = resolveProjectSpec ( projectSpec , resolvedOptions . paths , /*referrer*/ undefined ) ;
62
67
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 ( ) ) ) ;
64
71
}
65
72
exports . createCompiler = createCompiler ;
66
73
@@ -74,13 +81,13 @@ exports.createCompiler = createCompiler;
74
81
* @property {string } [cwd] The path to use for the current working directory. Defaults to `process.cwd()`.
75
82
* @property {string } [base] The path to use as the base for relative paths. Defaults to `cwd`.
76
83
* @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.
79
86
* @property {boolean } [verbose] Indicates whether verbose logging is enabled.
80
87
* @property {boolean } [force] Force recompilation (no up-to-date check).
81
88
* @property {boolean } [inProcess] Indicates whether to run gulp-typescript in-process or out-of-process (default).
82
89
*
83
- * @typedef {NodeJS.ReadWriteStream | (( ) => NodeJS.ReadWriteStream) } Hook
90
+ * @typedef {(stream: NodeJS.ReadableStream ) => NodeJS.ReadWriteStream } Hook
84
91
*/
85
92
function compile ( projectSpec , options ) {
86
93
const compiler = createCompiler ( projectSpec , options ) ;
@@ -97,7 +104,9 @@ function createCleaner(projectSpec, options) {
97
104
const paths = resolvePathOptions ( options ) ;
98
105
const resolvedProjectSpec = resolveProjectSpec ( projectSpec , paths , /*referrer*/ undefined ) ;
99
106
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 ( ) ) ) ;
101
110
}
102
111
exports . createCleaner = createCleaner ;
103
112
@@ -134,6 +143,7 @@ exports.addTypeScript = addTypeScript;
134
143
* @property {string } [cwd] The path to use for the current working directory. Defaults to `process.cwd()`.
135
144
* @property {CompilerOptions } [compilerOptions] Compiler option overrides.
136
145
* @property {boolean } [force] Forces creation of the output project.
146
+ * @property {string[] } [exclude] Files to exclude (relative to `cwd`)
137
147
*/
138
148
function flatten ( projectSpec , flattenedProjectSpec , options = { } ) {
139
149
const paths = resolvePathOptions ( options ) ;
@@ -142,15 +152,16 @@ function flatten(projectSpec, flattenedProjectSpec, options = {}) {
142
152
const resolvedOutputDirectory = path . dirname ( resolvedOutputSpec ) ;
143
153
const resolvedProjectSpec = resolveProjectSpec ( projectSpec , paths , /*referrer*/ undefined ) ;
144
154
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 ) ) ) ;
145
157
recur ( projectGraph ) ;
146
158
147
- const config = {
148
- extends : normalizeSlashes ( path . relative ( resolvedOutputDirectory , resolvedProjectSpec ) ) ,
149
- compilerOptions : options . compilerOptions || { } ,
150
- files
151
- } ;
152
-
153
159
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
+ } ;
154
165
mkdirp . sync ( resolvedOutputDirectory ) ;
155
166
fs . writeFileSync ( resolvedOutputSpec , JSON . stringify ( config , undefined , 2 ) , "utf8" ) ;
156
167
}
@@ -159,11 +170,16 @@ function flatten(projectSpec, flattenedProjectSpec, options = {}) {
159
170
* @param {ProjectGraph } projectGraph
160
171
*/
161
172
function recur ( projectGraph ) {
173
+ if ( skipProjects . has ( projectGraph ) ) return ;
174
+ skipProjects . add ( projectGraph ) ;
162
175
for ( const ref of projectGraph . references ) {
163
176
recur ( ref . target ) ;
164
177
}
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 ) ;
167
183
}
168
184
}
169
185
}
@@ -257,14 +273,6 @@ function resolveProjectOptions(options = {}) {
257
273
} ;
258
274
}
259
275
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
-
268
276
/**
269
277
* @param {ResolvedProjectOptions } left
270
278
* @param {ResolvedProjectOptions } right
@@ -448,31 +456,29 @@ function resolveDestPath(projectGraph, paths) {
448
456
449
457
/**
450
458
* @param {ProjectGraph } projectGraph
451
- * @param {ResolvedProjectOptions } resolvedOptions
459
+ * @param {ResolvedProjectOptions } options
452
460
*/
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 ) ;
456
464
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 ) ;
460
468
const { sourceMap, inlineSourceMap, inlineSources = false , sourceRoot, declarationMap } = projectGraph . project . options ;
461
469
const configFilePath = projectGraph . project . options . configFilePath ;
462
470
const sourceMapPath = inlineSourceMap ? undefined : "." ;
463
471
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 } ) ;
467
475
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 } ) ) )
469
477
. pipe ( gulpif ( sourceMap || inlineSourceMap , sourcemaps . init ( ) ) )
470
478
. pipe ( project ( ) ) ;
471
- const js = stream . js
472
- . pipe ( evaluateHook ( resolvedOptions . js ) )
479
+ const js = ( options . js ? options . js ( stream . js ) : stream . js )
473
480
. 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 )
476
482
. pipe ( gulpif ( declarationMap , sourcemaps . write ( sourceMapPath , sourceMapOptions ) ) ) ;
477
483
return merge2 ( [ js , dts ] )
478
484
. pipe ( gulp . dest ( destPath ) ) ;
0 commit comments