From 6a18c72bbf3f2f02805e673c27f1340543ba52db Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Tue, 23 Jun 2020 22:58:54 +0200 Subject: [PATCH] add library loader --- .../server/build/javaScriptLibraryLoader.js | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 extensions/html-language-features/server/build/javaScriptLibraryLoader.js diff --git a/extensions/html-language-features/server/build/javaScriptLibraryLoader.js b/extensions/html-language-features/server/build/javaScriptLibraryLoader.js new file mode 100644 index 00000000000..85792138ba4 --- /dev/null +++ b/extensions/html-language-features/server/build/javaScriptLibraryLoader.js @@ -0,0 +1,132 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +// a webpack loader that bundles all library definitions (d.ts) for the embedded JavaScript engine. + +const path = require('path'); +const fs = require('fs'); + +const TYPESCRIPT_LIB_SOURCE = path.join(__dirname, '../../../node_modules/typescript/lib'); +const JQUERY_DTS = path.join(__dirname, '../lib/jquery.d.ts'); + +module.exports = function () { + function getFileName(name) { + return (name === '' ? 'lib.d.ts' : `lib.${name}.d.ts`); + } + function readLibFile(name) { + var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, getFileName(name)); + return fs.readFileSync(srcPath).toString(); + } + + var queue = []; + var in_queue = {}; + + var enqueue = function (name) { + if (in_queue[name]) { + return; + } + in_queue[name] = true; + queue.push(name); + }; + + enqueue('es6'); + + var result = []; + while (queue.length > 0) { + var name = queue.shift(); + var contents = readLibFile(name); + var lines = contents.split(/\r\n|\r|\n/); + + var outputLines = []; + for (let i = 0; i < lines.length; i++) { + let m = lines[i].match(/\/\/\/\s*= 0; i--) { + strResult += `"${result[i].name}": ${result[i].output},\n`; + } + strResult += `\n};` + + strResult += `export function loadLibrary(name: string) : string {\n return libs[name] || ''; \n}`; + + return strResult; +} + +/** + * Escape text such that it can be used in a javascript string enclosed by double quotes (") + */ +function escapeText(text) { + // See http://www.javascriptkit.com/jsref/escapesequence.shtml + var _backspace = '\b'.charCodeAt(0); + var _formFeed = '\f'.charCodeAt(0); + var _newLine = '\n'.charCodeAt(0); + var _nullChar = 0; + var _carriageReturn = '\r'.charCodeAt(0); + var _tab = '\t'.charCodeAt(0); + var _verticalTab = '\v'.charCodeAt(0); + var _backslash = '\\'.charCodeAt(0); + var _doubleQuote = '"'.charCodeAt(0); + + var startPos = 0, chrCode, replaceWith = null, resultPieces = []; + + for (var i = 0, len = text.length; i < len; i++) { + chrCode = text.charCodeAt(i); + switch (chrCode) { + case _backspace: + replaceWith = '\\b'; + break; + case _formFeed: + replaceWith = '\\f'; + break; + case _newLine: + replaceWith = '\\n'; + break; + case _nullChar: + replaceWith = '\\0'; + break; + case _carriageReturn: + replaceWith = '\\r'; + break; + case _tab: + replaceWith = '\\t'; + break; + case _verticalTab: + replaceWith = '\\v'; + break; + case _backslash: + replaceWith = '\\\\'; + break; + case _doubleQuote: + replaceWith = '\\"'; + break; + } + if (replaceWith !== null) { + resultPieces.push(text.substring(startPos, i)); + resultPieces.push(replaceWith); + startPos = i + 1; + replaceWith = null; + } + } + resultPieces.push(text.substring(startPos, len)); + return resultPieces.join(''); +}