Skip to content

Commit 27bc032

Browse files
committed
nuke special lib options and use compiler option's lib instead
1 parent 8fac413 commit 27bc032

3 files changed

Lines changed: 55 additions & 32 deletions

File tree

build/gulpfile.editor.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,6 @@ const extractEditorSrcTask = task.define('extract-editor-src', () => {
7272
apiusages,
7373
extrausages
7474
],
75-
libs: [
76-
`lib.es5.d.ts`,
77-
`lib.es2015.core.d.ts`,
78-
`lib.es2015.collection.d.ts`,
79-
`lib.es2015.generator.d.ts`,
80-
`lib.es2015.promise.d.ts`,
81-
`lib.es2015.iterable.d.ts`,
82-
`lib.es2015.proxy.d.ts`,
83-
`lib.es2015.reflect.d.ts`,
84-
`lib.es2015.symbol.d.ts`,
85-
`lib.es2015.symbol.wellknown.d.ts`,
86-
`lib.dom.d.ts`,
87-
`lib.dom.iterable.d.ts`,
88-
`lib.webworker.importscripts.d.ts`
89-
],
9075
shakeLevel: 2, // 0-Files, 1-InnerFile, 2-ClassMembers
9176
importIgnorePattern: /(^vs\/css!)|(promise-polyfill\/polyfill)/,
9277
destRoot: path.join(root, 'out-editor-src'),

build/lib/treeshaking.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ function createTypeScriptLanguageService(options) {
7676
FILES[typing] = fs.readFileSync(filePath).toString();
7777
});
7878
// Resolve libs
79-
const RESOLVED_LIBS = {};
80-
options.libs.forEach((filename) => {
81-
const filepath = path.join(TYPESCRIPT_LIB_FOLDER, filename);
82-
RESOLVED_LIBS[`defaultLib:${filename}`] = fs.readFileSync(filepath).toString();
83-
});
79+
const RESOLVED_LIBS = processLibFiles(options);
8480
const compilerOptions = ts.convertCompilerOptionsFromJson(options.compilerOptions, options.sourcesRoot).options;
8581
const host = new TypeScriptLanguageServiceHost(RESOLVED_LIBS, FILES, compilerOptions);
8682
return ts.createLanguageService(host);
@@ -138,6 +134,29 @@ function discoverAndReadFiles(options) {
138134
}
139135
return FILES;
140136
}
137+
/**
138+
* Read lib files and follow lib references
139+
*/
140+
function processLibFiles(options) {
141+
const stack = [...options.compilerOptions.lib];
142+
const result = {};
143+
while (stack.length > 0) {
144+
const filename = `lib.${stack.shift().toLowerCase()}.d.ts`;
145+
const key = `defaultLib:${filename}`;
146+
if (!result[key]) {
147+
// add this file
148+
const filepath = path.join(TYPESCRIPT_LIB_FOLDER, filename);
149+
const sourceText = fs.readFileSync(filepath).toString();
150+
result[key] = sourceText;
151+
// precess dependencies and "recurse"
152+
const info = ts.preProcessFile(sourceText);
153+
for (let ref of info.libReferenceDirectives) {
154+
stack.push(ref.fileName);
155+
}
156+
}
157+
}
158+
return result;
159+
}
141160
/**
142161
* A TypeScript language service host
143162
*/

build/lib/treeshaking.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const enum ShakeLevel {
1818
}
1919

2020
export function toStringShakeLevel(shakeLevel: ShakeLevel): string {
21-
switch(shakeLevel) {
21+
switch (shakeLevel) {
2222
case ShakeLevel.Files:
2323
return 'Files (0)';
2424
case ShakeLevel.InnerFile:
@@ -42,11 +42,6 @@ export interface ITreeShakingOptions {
4242
* Inline usages.
4343
*/
4444
inlineEntryPoints: string[];
45-
/**
46-
* TypeScript libs.
47-
* e.g. `lib.d.ts`, `lib.es2015.collection.d.ts`
48-
*/
49-
libs: string[];
5045
/**
5146
* Other .d.ts files
5247
*/
@@ -130,11 +125,7 @@ function createTypeScriptLanguageService(options: ITreeShakingOptions): ts.Langu
130125
});
131126

132127
// Resolve libs
133-
const RESOLVED_LIBS: ILibMap = {};
134-
options.libs.forEach((filename) => {
135-
const filepath = path.join(TYPESCRIPT_LIB_FOLDER, filename);
136-
RESOLVED_LIBS[`defaultLib:${filename}`] = fs.readFileSync(filepath).toString();
137-
});
128+
const RESOLVED_LIBS = processLibFiles(options);
138129

139130
const compilerOptions = ts.convertCompilerOptionsFromJson(options.compilerOptions, options.sourcesRoot).options;
140131

@@ -205,6 +196,34 @@ function discoverAndReadFiles(options: ITreeShakingOptions): IFileMap {
205196
return FILES;
206197
}
207198

199+
/**
200+
* Read lib files and follow lib references
201+
*/
202+
function processLibFiles(options: ITreeShakingOptions): ILibMap {
203+
204+
const stack: string[] = [...options.compilerOptions.lib];
205+
const result: ILibMap = {};
206+
207+
while (stack.length > 0) {
208+
const filename = `lib.${stack.shift()!.toLowerCase()}.d.ts`;
209+
const key = `defaultLib:${filename}`;
210+
if (!result[key]) {
211+
// add this file
212+
const filepath = path.join(TYPESCRIPT_LIB_FOLDER, filename);
213+
const sourceText = fs.readFileSync(filepath).toString();
214+
result[key] = sourceText;
215+
216+
// precess dependencies and "recurse"
217+
const info = ts.preProcessFile(sourceText);
218+
for (let ref of info.libReferenceDirectives) {
219+
stack.push(ref.fileName);
220+
}
221+
}
222+
}
223+
224+
return result;
225+
}
226+
208227
interface ILibMap { [libName: string]: string; }
209228
interface IFileMap { [fileName: string]: string; }
210229

@@ -475,7 +494,7 @@ function markNodes(languageService: ts.LanguageService, options: ITreeShakingOpt
475494
}
476495

477496
if (black_queue.length === 0) {
478-
for (let i = 0; i< gray_queue.length; i++) {
497+
for (let i = 0; i < gray_queue.length; i++) {
479498
const node = gray_queue[i];
480499
const nodeParent = node.parent;
481500
if ((ts.isClassDeclaration(nodeParent) || ts.isInterfaceDeclaration(nodeParent)) && nodeOrChildIsBlack(nodeParent)) {

0 commit comments

Comments
 (0)