Browse Source

Use a more compact format for dts to avoid duplication

Alex Dima 7 năm trước cách đây
mục cha
commit
be47b28d96
5 tập tin đã thay đổi với 103 bổ sung51 xóa
  1. 93 37
      scripts/importTypescript.js
  2. 0 5
      src/lib/lib-es6-ts.ts
  3. 0 5
      src/lib/lib-ts.ts
  4. 7 0
      src/lib/lib.ts
  5. 3 4
      src/tsWorker.ts

+ 93 - 37
scripts/importTypescript.js

@@ -9,14 +9,13 @@ const fs = require('fs');
 const TYPESCRIPT_LIB_SOURCE = path.join(__dirname, '../node_modules/typescript/lib');
 const TYPESCRIPT_LIB_DESTINATION = path.join(__dirname, '../src/lib');
 
-(function() {
+(function () {
 	try {
 		fs.statSync(TYPESCRIPT_LIB_DESTINATION);
 	} catch (err) {
 		fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
 	}
-	importLibDeclarationFile('lib.d.ts');
-	importLibDeclarationFile('lib.es6.d.ts');
+	importLibs();
 
 	var tsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js')).toString();
 
@@ -27,7 +26,7 @@ const TYPESCRIPT_LIB_DESTINATION = path.join(__dirname, '../src/lib');
 	);
 
 	var tsServices_amd = tsServices +
-	`
+		`
 // MONACOCHANGE
 // Defining the entire module name because r.js has an issue and cannot bundle this file
 // correctly with an anonymous define call
@@ -37,7 +36,7 @@ define("vs/language/typescript/lib/typescriptServices", [], function() { return
 	fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'), tsServices_amd);
 
 	var tsServices_esm = tsServices +
-	`
+		`
 // MONACOCHANGE
 export const createClassifier = ts.createClassifier;
 export const createLanguageService = ts.createLanguageService;
@@ -54,7 +53,7 @@ export const TokenClass = ts.TokenClass;
 
 	var dtsServices = fs.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts')).toString();
 	dtsServices +=
-	`
+		`
 // MONACOCHANGE
 export = ts;
 // END MONACOCHANGE
@@ -62,46 +61,103 @@ export = ts;
 	fs.writeFileSync(path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'), dtsServices);
 })();
 
-/**
- * Import a lib*.d.ts file from TypeScript's dist
- */
-function importLibDeclarationFile(name) {
-	var dstName = name.replace(/\.d\.ts$/, '').replace(/\./g, '-') + '-ts';
+function importLibs() {
+	function getFileName(name) {
+		return (name === '' ? 'lib.d.ts' : `lib.${name}.d.ts`);
+	}
+	function getVariableName(name) {
+		return (name === '' ? 'lib_dts' : `lib_${name.replace(/\./g, '_')}_dts`);
+	}
+	function readLibFile(name) {
+		var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, getFileName(name));
+		return fs.readFileSync(srcPath).toString();
+	}
 
-	var contents = resolveLibFile(null, name);
+	var queue = [];
+	var in_queue = {};
 
-	var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, dstName + '.ts');
-	fs.writeFileSync(dstPath,
-`/*---------------------------------------------------------------------------------------------
- *  Copyright (c) Microsoft Corporation. All rights reserved.
- *  Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
+	var enqueue = function (name) {
+		if (in_queue[name]) {
+			return;
+		}
+		in_queue[name] = true;
+		queue.push(name);
+	};
 
-export const contents = "${escapeText(contents)}";
-`);
-}
+	enqueue('');
+	enqueue('es6');
 
-function resolveLibFile(name, filename) {
-	var srcPath;
-	if (filename) {
-		srcPath = path.join(TYPESCRIPT_LIB_SOURCE, filename);
-	} else {
-		srcPath = path.join(TYPESCRIPT_LIB_SOURCE, `lib.${name}.d.ts`);
+	var result = [];
+	while (queue.length > 0) {
+		var name = queue.shift();
+		var contents = readLibFile(name);
+		var lines = contents.split(/\r\n|\r|\n/);
+
+		var output = '';
+		var writeOutput = function (text) {
+			if (output.length === 0) {
+				output = text;
+			} else {
+				output += ` + ${text}`;
+			}
+		};
+		var outputLines = [];
+		var flushOutputLines = function () {
+			writeOutput(`"${escapeText(outputLines.join('\n'))}"`);
+			outputLines = [];
+		};
+		var deps = [];
+		for (let i = 0; i < lines.length; i++) {
+			let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
+			if (m) {
+				flushOutputLines();
+				writeOutput(getVariableName(m[1]));
+				deps.push(getVariableName(m[1]));
+				enqueue(m[1]);
+				continue;
+			}
+			outputLines.push(lines[i]);
+		}
+		flushOutputLines();
+
+		result.push({
+			name: getVariableName(name),
+			deps: deps,
+			output: output
+		});
 	}
 
-	var contents = fs.readFileSync(srcPath).toString();
-	var lines = contents.split(/\r\n|\r|\n/);
-	var result = [];
-	for (let i = 0; i < lines.length; i++) {
-		let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
-		if (m) {
-			result.push('\n' + resolveLibFile(m[1], null) + '\n');
-			continue;
+	var strResult = `/*---------------------------------------------------------------------------------------------
+ *  Copyright (c) Microsoft Corporation. All rights reserved.
+ *  Licensed under the MIT License. See License.txt in the project root for license information.
+ *--------------------------------------------------------------------------------------------*/
+`;
+	// Do a topological sort
+	while (result.length > 0) {
+		for (let i = result.length - 1; i >= 0; i--) {
+			if (result[i].deps.length === 0) {
+				// emit this node
+				strResult += `\nexport const ${result[i].name} = ${result[i].output};\n`;
+
+				// mark dep as resolved
+				for (let j = 0; j < result.length; j++) {
+					for (let k = 0; k < result[j].deps.length; k++) {
+						if (result[j].deps[k] === result[i].name) {
+							result[j].deps.splice(k, 1);
+							break;
+						}
+					}
+				}
+
+				// remove from result
+				result.splice(i, 1);
+				break;
+			}
 		}
-		result.push(lines[i]);
 	}
 
-	return result.join('\n');
+	var dstPath = path.join(TYPESCRIPT_LIB_DESTINATION, 'lib.ts');
+	fs.writeFileSync(dstPath, strResult);
 }
 
 /**

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 5
src/lib/lib-es6-ts.ts


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 5
src/lib/lib-ts.ts


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 7 - 0
src/lib/lib.ts


+ 3 - 4
src/tsWorker.ts

@@ -5,20 +5,19 @@
 'use strict';
 
 import * as ts from './lib/typescriptServices';
-import { contents as libdts } from './lib/lib-ts';
-import { contents as libes6ts } from './lib/lib-es6-ts';
+import { lib_dts, lib_es6_dts } from './lib/lib';
 
 import Promise = monaco.Promise;
 import IWorkerContext = monaco.worker.IWorkerContext;
 
 const DEFAULT_LIB = {
 	NAME: 'defaultLib:lib.d.ts',
-	CONTENTS: libdts
+	CONTENTS: lib_dts
 };
 
 const ES6_LIB = {
 	NAME: 'defaultLib:lib.es6.d.ts',
-	CONTENTS: libes6ts
+	CONTENTS: lib_es6_dts
 };
 
 export class TypeScriptWorker implements ts.LanguageServiceHost {

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác