summaryrefslogtreecommitdiff
path: root/tools/ts_library_builder/build_library.ts
blob: 0a18abcbbb27d2596339c26cf233313260c3dc80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import { writeFileSync } from "fs";
import { join } from "path";
import * as prettier from "prettier";
import {
  InterfaceDeclaration,
  ExpressionStatement,
  NamespaceDeclarationKind,
  Project,
  SourceFile,
  ts,
  Type,
  TypeGuards
} from "ts-morph";
import {
  addInterfaceDeclaration,
  addInterfaceProperty,
  addSourceComment,
  addTypeAlias,
  addVariableDeclaration,
  checkDiagnostics,
  flattenNamespace,
  getSourceComment,
  inlineFiles,
  loadDtsFiles,
  loadFiles,
  log,
  logDiagnostics,
  namespaceSourceFile,
  normalizeSlashes,
  setSilent
} from "./ast_util";

export interface BuildLibraryOptions {
  /**
   * The path to the root of the deno repository
   */
  basePath: string;

  /**
   * The path to the current build path
   */
  buildPath: string;

  /**
   * Denotes if the library should be built with debug information (comments
   * that indicate the source of the types)
   */
  debug?: boolean;

  /**
   * An array of files that should be inlined into the library
   */
  inline?: string[];

  /** An array of input files to be provided to the input project, relative to
   * the basePath. */
  inputs?: string[];

  /**
   * Path to globals file to be used I.E. `js/globals.ts`
   */
  additionalGlobals?: string[];

  /**
   * List of global variables to define as let instead of the default const.
   */
  declareAsLet?: string[];

  /**
   * The path to the output library
   */
  outFile: string;

  /**
   * Execute in silent mode or not
   */
  silent?: boolean;
}

const { ModuleKind, ModuleResolutionKind, ScriptTarget } = ts;

/**
 * A preamble which is appended to the start of the library.
 */
const libPreamble = `// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

/// <reference no-default-lib="true" />
/// <reference lib="esnext" />

`;

interface FlattenOptions {
  basePath: string;
  customSources: { [filePath: string]: string };
  filePath: string;
  debug?: boolean;
  declarationProject: Project;
  globalInterfaceName?: string;
  moduleName?: string;
  namespaceName?: string;
  targetSourceFile: SourceFile;
}

/** Flatten a module */
export function flatten({
  basePath,
  customSources,
  filePath,
  debug,
  declarationProject,
  globalInterfaceName,
  moduleName,
  namespaceName,
  targetSourceFile
}: FlattenOptions): void {
  // Flatten the source file into a single set of statements
  const statements = flattenNamespace({
    sourceFile: declarationProject.getSourceFileOrThrow(filePath),
    rootPath: basePath,
    customSources,
    debug
  });

  // If a module name is specified create the module in the target file
  if (moduleName) {
    const namespace = targetSourceFile.addNamespace({
      name: moduleName,
      hasDeclareKeyword: true,
      declarationKind: NamespaceDeclarationKind.Module
    });

    // Add the output of the flattening to the namespace
    namespace.addStatements(statements);
  }

  if (namespaceName) {
    const namespace = targetSourceFile.insertNamespace(0, {
      name: namespaceName,
      hasDeclareKeyword: true,
      declarationKind: NamespaceDeclarationKind.Namespace
    });

    // Add the output of the flattening to the namespace
    namespace.addStatements(statements);

    if (globalInterfaceName) {
      // Retrieve the global interface
      const interfaceDeclaration = targetSourceFile.getInterfaceOrThrow(
        globalInterfaceName
      );

      // Add the namespace to the global interface
      addInterfaceProperty(
        interfaceDeclaration,
        namespaceName,
        `typeof ${namespaceName}`
      );
    }
  }
}

interface PrepareFileForMergeOptions {
  globalVarName: string;
  interfaceName: string;
  targetSourceFile: SourceFile;
}

interface PrepareFileForMergeReturn {
  interfaceDeclaration: InterfaceDeclaration;
}

export function prepareFileForMerge({
  globalVarName,
  interfaceName,
  targetSourceFile
}: PrepareFileForMergeOptions): PrepareFileForMergeReturn {
  // Add the global object interface
  const interfaceDeclaration = targetSourceFile.addInterface({
    name: interfaceName,
    hasDeclareKeyword: true
  });

  // Declare the global variable
  addVariableDeclaration(
    targetSourceFile,
    globalVarName,
    interfaceName,
    true,
    true
  );

  // Add self reference to the global variable
  addInterfaceProperty(interfaceDeclaration, globalVarName, interfaceName);

  return {
    interfaceDeclaration
  };
}

interface MergeGlobalOptions extends PrepareFileForMergeOptions {
  basePath: string;
  debug?: boolean;
  declarationProject: Project;
  filePath: string;
  ignore?: string[];
  inputProject: Project;
  prepareReturn: PrepareFileForMergeReturn;
  declareAsLet?: string[];
}

/** Take a module and merge it into the global scope */
export function mergeGlobals({
  basePath,
  debug,
  declarationProject,
  filePath,
  globalVarName,
  ignore,
  inputProject,
  targetSourceFile,
  declareAsLet,
  prepareReturn: { interfaceDeclaration }
}: MergeGlobalOptions): void {
  // Retrieve source file from the input project
  const sourceFile = inputProject.getSourceFileOrThrow(filePath);

  // we are going to create a map of variables
  const globalVariables = new Map<
    string,
    {
      type: Type;
      node: ExpressionStatement;
    }
  >();
  const globalInterfaces: InterfaceDeclaration[] = [];

  // For every augmentation of the global variable in source file, we want
  // to extract the type and add it to the global variable map
  sourceFile.forEachChild(node => {
    if (TypeGuards.isExpressionStatement(node)) {
      const firstChild = node.getFirstChild();
      if (!firstChild) {
        return;
      }
      if (TypeGuards.isBinaryExpression(firstChild)) {
        const leftExpression = firstChild.getLeft();
        if (
          TypeGuards.isPropertyAccessExpression(leftExpression) &&
          leftExpression.getExpression().getText() === globalVarName
        ) {
          const globalVarProperty = leftExpression.getName();
          if (globalVarProperty !== globalVarName) {
            globalVariables.set(globalVarProperty, {
              type: firstChild.getType(),
              node
            });
          }
        }
      }
    } else if (TypeGuards.isInterfaceDeclaration(node) && node.isExported()) {
      globalInterfaces.push(node);
    }
  });

  // A set of source files that the types we are using are dependent on us
  // importing
  const dependentSourceFiles = new Set<SourceFile>();

  // Create a global variable and add the property to the `Window` interface
  // for each mutation of the `window` variable we observed in `globals.ts`
  for (const [property, info] of globalVariables) {
    if (!(ignore && ignore.includes(property))) {
      const type = info.type.getText(info.node);
      const typeSymbol = info.type.getSymbol();
      if (typeSymbol) {
        const valueDeclaration = typeSymbol.getValueDeclaration();
        if (valueDeclaration) {
          dependentSourceFiles.add(valueDeclaration.getSourceFile());
        }
      }
      const isConst = !(declareAsLet && declareAsLet.includes(property));
      addVariableDeclaration(targetSourceFile, property, type, isConst, true);
      addInterfaceProperty(interfaceDeclaration, property, type);
    }
  }

  // We need to copy over any type aliases
  for (const typeAlias of sourceFile.getTypeAliases()) {
    addTypeAlias(
      targetSourceFile,
      typeAlias.getName(),
      typeAlias.getType().getText(sourceFile),
      true
    );
  }

  // We need to copy over any interfaces
  for (const interfaceDeclaration of globalInterfaces) {
    addInterfaceDeclaration(targetSourceFile, interfaceDeclaration);
  }

  // We need to ensure that we only namespace each source file once, so we
  // will use this map for tracking that.
  const sourceFileMap = new Map<SourceFile, string>();

  // For each import declaration in source file we will want to convert the
  // declaration source file into a namespace that exists within the merged
  // namespace
  const importDeclarations = sourceFile.getImportDeclarations();
  const namespaces = new Set<string>();
  for (const declaration of importDeclarations) {
    const namespaceImport = declaration.getNamespaceImport();
    if (namespaceImport) {
      const declarationSourceFile = declaration.getModuleSpecifierSourceFile();
      if (
        declarationSourceFile &&
        dependentSourceFiles.has(declarationSourceFile)
      ) {
        // the source file will resolve to the original `.ts` file, but the
        // information we really want is in the emitted `.d.ts` file, so we will
        // resolve to that file
        const dtsFilePath = declarationSourceFile
          .getFilePath()
          .replace(/\.ts$/, ".d.ts");
        const dtsSourceFile = declarationProject.getSourceFileOrThrow(
          dtsFilePath
        );
        targetSourceFile.addStatements(
          namespaceSourceFile(dtsSourceFile, {
            debug,
            namespace: namespaceImport.getText(),
            namespaces,
            rootPath: basePath,
            sourceFileMap
          })
        );
      }
    }
  }

  if (debug) {
    addSourceComment(targetSourceFile, sourceFile, basePath);
  }
}

/**
 * Generate the runtime library for Deno and write it to the supplied out file
 * name.
 */
export function main({
  basePath,
  buildPath,
  inline,
  inputs,
  additionalGlobals,
  declareAsLet,
  debug,
  outFile,
  silent
}: BuildLibraryOptions): void {
  setSilent(silent);
  log("-----");
  log("build_lib");
  log();
  log(`basePath: "${basePath}"`);
  log(`buildPath: "${buildPath}"`);
  if (inline && inline.length) {
    log("inline:");
    for (const filename of inline) {
      log(`  "${filename}"`);
    }
  }
  if (inputs && inputs.length) {
    log("inputs:");
    for (const input of inputs) {
      log(`  "${input}"`);
    }
  }
  log(`debug: ${!!debug}`);
  log(`outFile: "${outFile}"`);
  log();

  // the inputProject will take in the TypeScript files that are internal
  // to Deno to be used to generate the library
  const inputProject = new Project({
    compilerOptions: {
      baseUrl: basePath,
      declaration: true,
      emitDeclarationOnly: true,
      lib: ["esnext"],
      module: ModuleKind.ESNext,
      moduleResolution: ModuleResolutionKind.NodeJs,
      paths: {
        "*": ["*", `${buildPath}/*`]
      },
      preserveConstEnums: true,
      strict: true,
      stripInternal: true,
      target: ScriptTarget.ESNext
    }
  });

  // Add the input files we will need to generate the declarations, `globals`
  // plus any modules that are importable in the runtime need to be added here
  // plus the `lib.esnext` which is used as the base library
  if (inputs) {
    inputProject.addExistingSourceFiles(
      inputs.map(input => join(basePath, input))
    );
  }

  // emit the project, which will be only the declaration files
  const inputEmitResult = inputProject.emitToMemory();

  log("Emitted input project.");

  const inputDiagnostics = inputEmitResult
    .getDiagnostics()
    .map(d => d.compilerObject);
  logDiagnostics(inputDiagnostics);
  if (inputDiagnostics.length) {
    console.error("\nDiagnostics present during input project emit.\n");
    process.exit(1);
  }

  // the declaration project will be the target for the emitted files from
  // the input project, these will be used to transfer information over to
  // the final library file
  const declarationProject = new Project({
    compilerOptions: {
      baseUrl: basePath,
      lib: ["esnext"],
      moduleResolution: ModuleResolutionKind.NodeJs,
      paths: {
        "*": ["*", `${buildPath}/*`]
      },
      strict: true,
      target: ScriptTarget.ESNext
    },
    useVirtualFileSystem: true
  });

  // we don't want to add to the declaration project any of the original
  // `.ts` source files, so we need to filter those out
  const jsPath = normalizeSlashes(`${basePath}/js`);
  const inputProjectFiles = inputProject
    .getSourceFiles()
    .map(sourceFile => sourceFile.getFilePath())
    .filter(filePath => !filePath.startsWith(jsPath));
  loadFiles(declarationProject, inputProjectFiles);

  // now we add the emitted declaration files from the input project
  for (const { filePath, text } of inputEmitResult.getFiles()) {
    declarationProject.createSourceFile(filePath, text);
  }

  // the outputProject will contain the final library file we are looking to
  // build
  const outputProjectCompilerOptions: ts.CompilerOptions = {
    baseUrl: buildPath,
    lib: ["esnext"],
    moduleResolution: ModuleResolutionKind.NodeJs,
    strict: true,
    target: ScriptTarget.ESNext
  };

  const outputProject = new Project({
    compilerOptions: outputProjectCompilerOptions,
    useVirtualFileSystem: true
  });

  // There are files we need to load into memory, so that the project "compiles"
  loadDtsFiles(outputProject, outputProjectCompilerOptions);

  // libDts is the final output file we are looking to build and we are not
  // actually creating it, only in memory at this stage.
  const libDTs = outputProject.createSourceFile(outFile);

  // Deal with `js/deno.ts`

  // Generate a object hash of substitutions of modules to use when flattening
  const customSources = {};

  const prepareForMergeOpts: PrepareFileForMergeOptions = {
    globalVarName: "window",
    interfaceName: "Window",
    targetSourceFile: libDTs
  };

  const prepareReturn = prepareFileForMerge(prepareForMergeOpts);

  mergeGlobals({
    basePath,
    debug,
    declarationProject,
    filePath: `${basePath}/js/globals.ts`,
    inputProject,
    ignore: ["Deno"],
    declareAsLet,
    ...prepareForMergeOpts,
    prepareReturn
  });

  log(`Merged "globals" into global scope.`);

  if (additionalGlobals) {
    for (const additionalGlobal of additionalGlobals) {
      mergeGlobals({
        basePath,
        debug,
        declarationProject,
        filePath: `${basePath}/${additionalGlobal}`,
        inputProject,
        ignore: ["Deno"],
        declareAsLet,
        ...prepareForMergeOpts,
        prepareReturn
      });
    }

    log(`Added additional "globals" into global scope.`);
  }

  flatten({
    basePath,
    customSources,
    debug,
    declarationProject,
    filePath: `${basePath}/js/deno.d.ts`,
    globalInterfaceName: "Window",
    namespaceName: "Deno",
    targetSourceFile: libDTs
  });

  log(`Created module "deno" and namespace Deno.`);

  // Inline any files that were passed in, to be used to add additional libs
  // which are not part of TypeScript.
  if (inline && inline.length) {
    inlineFiles({
      basePath,
      debug,
      inline,
      targetSourceFile: libDTs
    });
  }

  // Add the preamble
  libDTs.insertStatements(0, libPreamble);

  // Check diagnostics
  checkDiagnostics(outputProject);

  // Output the final library file
  libDTs.saveSync();
  const libDTsText = prettier.format(
    outputProject.getFileSystem().readFileSync(outFile, "utf8"),
    { parser: "typescript" }
  );
  if (!silent) {
    console.log(`Outputting library to: "${outFile}"`);
    console.log(`  Length: ${libDTsText.length}`);
  }
  writeFileSync(outFile, libDTsText, { encoding: "utf8" });
  if (!silent) {
    console.log("-----");
    console.log();
  }
}