@@ -9,6 +9,9 @@ const fold = require("travis-fold");
9
9
const ts = require ( "./lib/typescript" ) ;
10
10
const del = require ( "del" ) ;
11
11
const getDirSize = require ( "./scripts/build/getDirSize" ) ;
12
+ const { base64VLQFormatEncode } = require ( "./scripts/build/sourcemaps" ) ;
13
+ const needsUpdate = require ( "./scripts/build/needsUpdate" ) ;
14
+ const { flatten } = require ( "./scripts/build/project" ) ;
12
15
13
16
// add node_modules to path so we don't need global modules, prefer the modules by adding them first
14
17
var nodeModulesPathPrefix = path . resolve ( "./node_modules/.bin/" ) + path . delimiter ;
@@ -64,9 +67,14 @@ Paths.typesMapOutput = "built/local/typesMap.json";
64
67
Paths . typescriptFile = "built/local/typescript.js" ;
65
68
Paths . servicesFile = "built/local/typescriptServices.js" ;
66
69
Paths . servicesDefinitionFile = "built/local/typescriptServices.d.ts" ;
70
+ Paths . servicesOutFile = "built/local/typescriptServices.out.js" ;
71
+ Paths . servicesDefinitionOutFile = "built/local/typescriptServices.out.d.ts" ;
67
72
Paths . typescriptDefinitionFile = "built/local/typescript.d.ts" ;
68
73
Paths . typescriptStandaloneDefinitionFile = "built/local/typescript_standalone.d.ts" ;
74
+ Paths . tsserverLibraryFile = "built/local/tsserverlibrary.js" ;
69
75
Paths . tsserverLibraryDefinitionFile = "built/local/tsserverlibrary.d.ts" ;
76
+ Paths . tsserverLibraryOutFile = "built/local/tsserverlibrary.out.js" ;
77
+ Paths . tsserverLibraryDefinitionOutFile = "built/local/tsserverlibrary.out.d.ts" ;
70
78
Paths . baselines = { } ;
71
79
Paths . baselines . local = "tests/baselines/local" ;
72
80
Paths . baselines . localTest262 = "tests/baselines/test262/local" ;
@@ -101,7 +109,9 @@ const ConfigFileFor = {
101
109
runjs : "src/testRunner" ,
102
110
lint : "scripts/tslint" ,
103
111
scripts : "scripts" ,
104
- all : "src"
112
+ all : "src" ,
113
+ typescriptServices : "built/local/typescriptServices.tsconfig.json" ,
114
+ tsserverLibrary : "built/local/tsserverlibrary.tsconfig.json" ,
105
115
} ;
106
116
107
117
const ExpectedLKGFiles = [
@@ -124,13 +134,18 @@ desc("Builds the full compiler and services");
124
134
task ( TaskNames . local , [
125
135
TaskNames . buildFoldStart ,
126
136
TaskNames . coreBuild ,
137
+ Paths . servicesDefinitionFile ,
138
+ Paths . typescriptFile ,
139
+ Paths . typescriptDefinitionFile ,
140
+ Paths . typescriptStandaloneDefinitionFile ,
141
+ Paths . tsserverLibraryDefinitionFile ,
127
142
TaskNames . localize ,
128
143
TaskNames . buildFoldEnd
129
144
] ) ;
130
145
131
146
task ( "default" , [ TaskNames . local ] ) ;
132
147
133
- const RunTestsPrereqs = [ TaskNames . lib , Paths . servicesDefinitionFile , Paths . tsserverLibraryDefinitionFile ] ;
148
+ const RunTestsPrereqs = [ TaskNames . lib , Paths . servicesDefinitionFile , Paths . typescriptDefinitionFile , Paths . tsserverLibraryDefinitionFile ] ;
134
149
desc ( "Runs all the tests in parallel using the built run.js file. Optional arguments are: t[ests]=category1|category2|... d[ebug]=true." ) ;
135
150
task ( TaskNames . runtestsParallel , RunTestsPrereqs , function ( ) {
136
151
tsbuild ( [ ConfigFileFor . runjs ] , true , ( ) => {
@@ -172,6 +187,9 @@ task(TaskNames.lkg, [
172
187
TaskNames . release ,
173
188
TaskNames . local ,
174
189
Paths . servicesDefinitionFile ,
190
+ Paths . typescriptFile ,
191
+ Paths . typescriptDefinitionFile ,
192
+ Paths . typescriptStandaloneDefinitionFile ,
175
193
Paths . tsserverLibraryDefinitionFile ,
176
194
Paths . releaseCompiler ,
177
195
...libraryTargets
@@ -333,64 +351,146 @@ file(Paths.diagnosticInformationMap, [Paths.diagnosticMessagesJson], function ()
333
351
} ) ;
334
352
} , { async : true } ) ;
335
353
336
- // tsserverlibrary.d.ts
337
- file ( Paths . tsserverLibraryDefinitionFile , [ TaskNames . coreBuild ] , function ( ) {
338
- const sources = [ "compiler.d.ts" , "jsTyping.d.ts" , "services.d.ts" , "server.d.ts" ] . map ( f => path . join ( Paths . builtLocal , f ) ) ;
339
- let output = "" ;
340
- for ( const f of sources ) {
341
- output = output + "\n" + removeConstModifierFromEnumDeclarations ( readFileSync ( f ) ) ;
342
- }
343
- output = output + "\nexport = ts;\nexport as namespace ts;" ;
344
- fs . writeFileSync ( Paths . tsserverLibraryDefinitionFile , output , { encoding : "utf-8" } ) ;
354
+ file ( ConfigFileFor . tsserverLibrary , [ ] , function ( ) {
355
+ flatten ( "src/tsserver/tsconfig.json" , ConfigFileFor . tsserverLibrary , {
356
+ exclude : [ "src/tsserver/server.ts" ] ,
357
+ compilerOptions : {
358
+ "removeComments" : false ,
359
+ "stripInternal" : true ,
360
+ "declarationMap" : false ,
361
+ "outFile" : "tsserverlibrary.out.js"
362
+ }
363
+ } )
345
364
} ) ;
346
365
347
- // typescriptservices.d.ts
348
- file ( Paths . servicesDefinitionFile , [ TaskNames . coreBuild ] , function ( ) {
349
- // Generate a config file
350
- const files = [ ] ;
351
- recur ( `src/services/tsconfig.json` ) ;
366
+ // tsserverlibrary.js
367
+ // tsserverlibrary.d.ts
368
+ file ( Paths . tsserverLibraryFile , [ TaskNames . coreBuild , ConfigFileFor . tsserverLibrary ] , function ( ) {
369
+ tsbuild ( ConfigFileFor . tsserverLibrary , false , ( ) => {
370
+ if ( needsUpdate ( [ Paths . tsserverLibraryOutFile , Paths . tsserverLibraryDefinitionOutFile ] , [ Paths . tsserverLibraryFile , Paths . tsserverLibraryDefinitionFile ] ) ) {
371
+ const copyright = readFileSync ( Paths . copyright ) ;
372
+
373
+ let libraryDefinitionContent = readFileSync ( Paths . tsserverLibraryDefinitionOutFile ) ;
374
+ libraryDefinitionContent = copyright + removeConstModifierFromEnumDeclarations ( libraryDefinitionContent ) ;
375
+ libraryDefinitionContent += "\nexport = ts;\nexport as namespace ts;" ;
376
+ fs . writeFileSync ( Paths . tsserverLibraryDefinitionFile , libraryDefinitionContent , "utf8" ) ;
377
+
378
+ let libraryContent = readFileSync ( Paths . tsserverLibraryOutFile ) ;
379
+ libraryContent = copyright + libraryContent ;
380
+ fs . writeFileSync ( Paths . tsserverLibraryFile , libraryContent , "utf8" ) ;
381
+
382
+ // adjust source map for tsserverlibrary.js
383
+ let libraryMapContent = readFileSync ( Paths . tsserverLibraryOutFile + ".map" ) ;
384
+ const map = JSON . parse ( libraryMapContent ) ;
385
+ const lineStarts = /**@type {* }*/ ( ts ) . computeLineStarts ( copyright ) ;
386
+ let prependMappings = "" ;
387
+ for ( let i = 1 ; i < lineStarts . length ; i ++ ) {
388
+ prependMappings += ";" ;
389
+ }
390
+
391
+ const offset = copyright . length - lineStarts [ lineStarts . length - 1 ] ;
392
+ if ( offset > 0 ) {
393
+ prependMappings += base64VLQFormatEncode ( offset ) + "," ;
394
+ }
395
+
396
+ const outputMap = {
397
+ version : map . version ,
398
+ file : map . file ,
399
+ sources : map . sources ,
400
+ sourceRoot : map . sourceRoot ,
401
+ mappings : prependMappings + map . mappings ,
402
+ names : map . names ,
403
+ sourcesContent : map . sourcesContent
404
+ } ;
405
+
406
+ libraryMapContent = JSON . stringify ( outputMap ) ;
407
+ fs . writeFileSync ( Paths . tsserverLibraryFile + ".map" , libraryMapContent ) ;
408
+ }
409
+ complete ( ) ;
410
+ } ) ;
411
+ } , { async : true } ) ;
412
+ task ( Paths . tsserverLibraryDefinitionFile , [ Paths . tsserverLibraryFile ] ) ;
352
413
353
- const config = {
354
- extends : "../../ src/tsconfig-base" ,
414
+ file ( ConfigFileFor . typescriptServices , [ ] , function ( ) {
415
+ flatten ( " src/services/ tsconfig.json" , ConfigFileFor . typescriptServices , {
355
416
compilerOptions : {
417
+ "removeComments" : false ,
356
418
"stripInternal" : true ,
357
- "outFile" : "typescriptServices.js"
358
- } ,
359
- files
360
- } ;
361
-
362
- const configFilePath = `built/local/typescriptServices.tsconfig.json` ;
363
- fs . writeFileSync ( configFilePath , JSON . stringify ( config , undefined , 2 ) ) ;
364
- tsbuild ( configFilePath , false , ( ) => {
365
- const servicesContent = readFileSync ( Paths . servicesDefinitionFile ) ;
366
- const servicesContentWithoutConstEnums = removeConstModifierFromEnumDeclarations ( servicesContent ) ;
367
- fs . writeFileSync ( Paths . servicesDefinitionFile , servicesContentWithoutConstEnums ) ;
368
-
369
- // Also build typescript.js, typescript.js.map, and typescript.d.ts
370
- jake . cpR ( Paths . servicesFile , Paths . typescriptFile ) ;
371
- if ( fs . existsSync ( Paths . servicesFile + ".map" ) ) {
372
- jake . cpR ( Paths . servicesFile + ".map" , Paths . typescriptFile + ".map" ) ;
419
+ "declarationMap" : false ,
420
+ "outFile" : "typescriptServices.out.js"
373
421
}
422
+ } ) ;
423
+ } ) ;
424
+
425
+ // typescriptServices.js
426
+ // typescriptServices.d.ts
427
+ file ( Paths . servicesFile , [ TaskNames . coreBuild , ConfigFileFor . typescriptServices ] , function ( ) {
428
+ tsbuild ( ConfigFileFor . typescriptServices , false , ( ) => {
429
+ if ( needsUpdate ( [ Paths . servicesOutFile , Paths . servicesDefinitionOutFile ] , [ Paths . servicesFile , Paths . servicesDefinitionFile ] ) ) {
430
+ const copyright = readFileSync ( Paths . copyright ) ;
431
+
432
+ let servicesDefinitionContent = readFileSync ( Paths . servicesDefinitionOutFile ) ;
433
+ servicesDefinitionContent = copyright + removeConstModifierFromEnumDeclarations ( servicesDefinitionContent ) ;
434
+ fs . writeFileSync ( Paths . servicesDefinitionFile , servicesDefinitionContent , "utf8" ) ;
435
+
436
+ let servicesContent = readFileSync ( Paths . servicesOutFile ) ;
437
+ servicesContent = copyright + servicesContent ;
438
+ fs . writeFileSync ( Paths . servicesFile , servicesContent , "utf8" ) ;
439
+
440
+ // adjust source map for typescriptServices.js
441
+ let servicesMapContent = readFileSync ( Paths . servicesOutFile + ".map" ) ;
442
+ const map = JSON . parse ( servicesMapContent ) ;
443
+ const lineStarts = /**@type {* }*/ ( ts ) . computeLineStarts ( copyright ) ;
444
+ let prependMappings = "" ;
445
+ for ( let i = 1 ; i < lineStarts . length ; i ++ ) {
446
+ prependMappings += ";" ;
447
+ }
374
448
375
- fs . writeFileSync ( Paths . typescriptDefinitionFile , servicesContentWithoutConstEnums + "\r\nexport = ts" , { encoding : "utf-8" } ) ;
376
- // And typescript_standalone.d.ts
377
- fs . writeFileSync ( Paths . typescriptStandaloneDefinitionFile , servicesContentWithoutConstEnums . replace ( / d e c l a r e ( n a m e s p a c e | m o d u l e ) t s ( \. .+ ) ? \{ / g, 'declare module "typescript" {' ) , { encoding : "utf-8" } ) ;
449
+ const offset = copyright . length - lineStarts [ lineStarts . length - 1 ] ;
450
+ if ( offset > 0 ) {
451
+ prependMappings += base64VLQFormatEncode ( offset ) + "," ;
452
+ }
453
+
454
+ const outputMap = {
455
+ version : map . version ,
456
+ file : map . file ,
457
+ sources : map . sources ,
458
+ sourceRoot : map . sourceRoot ,
459
+ mappings : prependMappings + map . mappings ,
460
+ names : map . names ,
461
+ sourcesContent : map . sourcesContent
462
+ } ;
463
+
464
+ servicesMapContent = JSON . stringify ( outputMap ) ;
465
+ fs . writeFileSync ( Paths . servicesFile + ".map" , servicesMapContent ) ;
466
+ }
378
467
379
468
complete ( ) ;
380
469
} ) ;
470
+ } , { async : true } ) ;
471
+ task ( Paths . servicesDefinitionFile , [ Paths . servicesFile ] ) ;
381
472
382
- function recur ( configPath ) {
383
- const cfgFile = readJson ( configPath ) ;
384
- if ( cfgFile . references ) {
385
- for ( const ref of cfgFile . references ) {
386
- recur ( path . join ( path . dirname ( configPath ) , ref . path , "tsconfig.json" ) ) ;
387
- }
388
- }
389
- for ( const file of cfgFile . files ) {
390
- files . push ( path . join ( `../../` , path . dirname ( configPath ) , file ) ) ;
473
+ // typescript.js
474
+ // typescript.d.ts
475
+ file ( Paths . typescriptFile , [ Paths . servicesFile ] , function ( ) {
476
+ if ( needsUpdate ( [ Paths . servicesFile , Paths . servicesDefinitionFile ] , [ Paths . typescriptFile , Paths . typescriptDefinitionFile ] ) ) {
477
+ jake . cpR ( Paths . servicesFile , Paths . typescriptFile ) ;
478
+ if ( fs . existsSync ( Paths . servicesFile + ".map" ) ) {
479
+ jake . cpR ( Paths . servicesFile + ".map" , Paths . typescriptFile + ".map" ) ;
391
480
}
481
+ const content = readFileSync ( Paths . servicesDefinitionFile ) ;
482
+ fs . writeFileSync ( Paths . typescriptDefinitionFile , content + "\r\nexport = ts;" , { encoding : "utf-8" } ) ;
392
483
}
393
- } , { async : true } ) ;
484
+ } ) ;
485
+ task ( Paths . typescriptDefinitionFile , [ Paths . typescriptFile ] ) ;
486
+
487
+ // typescript_standalone.d.ts
488
+ file ( Paths . typescriptStandaloneDefinitionFile , [ Paths . servicesDefinitionFile ] , function ( ) {
489
+ if ( needsUpdate ( Paths . servicesDefinitionFile , Paths . typescriptStandaloneDefinitionFile ) ) {
490
+ const content = readFileSync ( Paths . servicesDefinitionFile ) ;
491
+ fs . writeFileSync ( Paths . typescriptStandaloneDefinitionFile , content . replace ( / d e c l a r e ( n a m e s p a c e | m o d u l e ) t s ( \. .+ ) ? \{ / g, 'declare module "typescript" {' ) , { encoding : "utf-8" } ) ;
492
+ }
493
+ } ) ;
394
494
395
495
function getLibraryTargets ( ) {
396
496
/** @type {{ libs: string[], paths?: Record<string, string>, sources?: Record<string, string[]> } } */
@@ -765,4 +865,4 @@ function getDiffTool() {
765
865
*/
766
866
function removeConstModifierFromEnumDeclarations ( text ) {
767
867
return text . replace ( / ^ ( \s * ) ( e x p o r t ) ? c o n s t e n u m ( \S + ) { ( \s * ) $ / gm, '$1$2enum $3 {$4' ) ;
768
- }
868
+ }
0 commit comments