Skip to content

Commit 6a18c72

Browse files
committed
add library loader
1 parent 9b8fec1 commit 6a18c72

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
// a webpack loader that bundles all library definitions (d.ts) for the embedded JavaScript engine.
7+
8+
const path = require('path');
9+
const fs = require('fs');
10+
11+
const TYPESCRIPT_LIB_SOURCE = path.join(__dirname, '../../../node_modules/typescript/lib');
12+
const JQUERY_DTS = path.join(__dirname, '../lib/jquery.d.ts');
13+
14+
module.exports = function () {
15+
function getFileName(name) {
16+
return (name === '' ? 'lib.d.ts' : `lib.${name}.d.ts`);
17+
}
18+
function readLibFile(name) {
19+
var srcPath = path.join(TYPESCRIPT_LIB_SOURCE, getFileName(name));
20+
return fs.readFileSync(srcPath).toString();
21+
}
22+
23+
var queue = [];
24+
var in_queue = {};
25+
26+
var enqueue = function (name) {
27+
if (in_queue[name]) {
28+
return;
29+
}
30+
in_queue[name] = true;
31+
queue.push(name);
32+
};
33+
34+
enqueue('es6');
35+
36+
var result = [];
37+
while (queue.length > 0) {
38+
var name = queue.shift();
39+
var contents = readLibFile(name);
40+
var lines = contents.split(/\r\n|\r|\n/);
41+
42+
var outputLines = [];
43+
for (let i = 0; i < lines.length; i++) {
44+
let m = lines[i].match(/\/\/\/\s*<reference\s*lib="([^"]+)"/);
45+
if (m) {
46+
enqueue(m[1]);
47+
}
48+
outputLines.push(lines[i]);
49+
}
50+
51+
result.push({
52+
name: getFileName(name),
53+
output: `"${escapeText(outputLines.join('\n'))}"`
54+
});
55+
}
56+
57+
const jquerySource = fs.readFileSync(JQUERY_DTS).toString();
58+
var lines = jquerySource.split(/\r\n|\r|\n/);
59+
result.push({
60+
name: 'jquery',
61+
output: `"${escapeText(lines.join('\n'))}"`
62+
});
63+
64+
strResult = `\nconst libs : { [name:string]: string; } = {\n`
65+
for (let i = result.length - 1; i >= 0; i--) {
66+
strResult += `"${result[i].name}": ${result[i].output},\n`;
67+
}
68+
strResult += `\n};`
69+
70+
strResult += `export function loadLibrary(name: string) : string {\n return libs[name] || ''; \n}`;
71+
72+
return strResult;
73+
}
74+
75+
/**
76+
* Escape text such that it can be used in a javascript string enclosed by double quotes (")
77+
*/
78+
function escapeText(text) {
79+
// See http://www.javascriptkit.com/jsref/escapesequence.shtml
80+
var _backspace = '\b'.charCodeAt(0);
81+
var _formFeed = '\f'.charCodeAt(0);
82+
var _newLine = '\n'.charCodeAt(0);
83+
var _nullChar = 0;
84+
var _carriageReturn = '\r'.charCodeAt(0);
85+
var _tab = '\t'.charCodeAt(0);
86+
var _verticalTab = '\v'.charCodeAt(0);
87+
var _backslash = '\\'.charCodeAt(0);
88+
var _doubleQuote = '"'.charCodeAt(0);
89+
90+
var startPos = 0, chrCode, replaceWith = null, resultPieces = [];
91+
92+
for (var i = 0, len = text.length; i < len; i++) {
93+
chrCode = text.charCodeAt(i);
94+
switch (chrCode) {
95+
case _backspace:
96+
replaceWith = '\\b';
97+
break;
98+
case _formFeed:
99+
replaceWith = '\\f';
100+
break;
101+
case _newLine:
102+
replaceWith = '\\n';
103+
break;
104+
case _nullChar:
105+
replaceWith = '\\0';
106+
break;
107+
case _carriageReturn:
108+
replaceWith = '\\r';
109+
break;
110+
case _tab:
111+
replaceWith = '\\t';
112+
break;
113+
case _verticalTab:
114+
replaceWith = '\\v';
115+
break;
116+
case _backslash:
117+
replaceWith = '\\\\';
118+
break;
119+
case _doubleQuote:
120+
replaceWith = '\\"';
121+
break;
122+
}
123+
if (replaceWith !== null) {
124+
resultPieces.push(text.substring(startPos, i));
125+
resultPieces.push(replaceWith);
126+
startPos = i + 1;
127+
replaceWith = null;
128+
}
129+
}
130+
resultPieces.push(text.substring(startPos, len));
131+
return resultPieces.join('');
132+
}

0 commit comments

Comments
 (0)