X Tutup
Skip to content

Commit f371c90

Browse files
committed
build(broccoli): Clean-up TypeScript build
The TypeScript parser now only references files that are in broccoli trees. Closes angular#7941
1 parent 85c1927 commit f371c90

File tree

3 files changed

+49
-50
lines changed

3 files changed

+49
-50
lines changed

tools/broccoli/broccoli-typescript.ts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
5858
private genInternalTypings: boolean = false;
5959

6060
static includeExtensions = ['.ts'];
61-
static excludeExtensions = ['.d.ts'];
6261

6362
constructor(public inputPath: string, public cachePath: string, public options) {
6463
if (options.rootFilePaths) {
@@ -83,11 +82,7 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
8382
this.genInternalTypings = false;
8483
}
8584

86-
// TODO: the above turns rootDir set to './' into an empty string - looks like a tsc bug
87-
// check back when we upgrade to 1.7.x
88-
if (this.tsOpts.rootDir === '') {
89-
this.tsOpts.rootDir = './';
90-
}
85+
this.tsOpts.rootDir = inputPath;
9186
this.tsOpts.outDir = this.cachePath;
9287

9388
this.tsServiceHost = new CustomLanguageServiceHost(this.tsOpts, this.rootFilePaths,
@@ -331,54 +326,34 @@ class CustomLanguageServiceHost implements ts.LanguageServiceHost {
331326
}
332327

333328

334-
getScriptFileNames(): string[] { return this.fileNames; }
329+
getScriptFileNames(): string[] {
330+
return this.fileNames.map(f => path.join(this.treeInputPath, f));
331+
}
335332

336333

337334
getScriptVersion(fileName: string): string {
338335
return this.fileRegistry[fileName] && this.fileRegistry[fileName].version.toString();
339336
}
340337

341338

342-
/**
343-
* This method is called quite a bit to lookup 3 kinds of paths:
344-
* 1/ files in the fileRegistry
345-
* - these are the files in our project that we are watching for changes
346-
* - in the future we could add caching for these files and invalidate the cache when
347-
* the file is changed lazily during lookup
348-
* 2/ .d.ts and library files not in the fileRegistry
349-
* - these are not our files, they come from tsd or typescript itself
350-
* - these files change only rarely but since we need them very rarely, it's not worth the
351-
* cache invalidation hassle to cache them
352-
* 3/ bogus paths that typescript compiler tries to lookup during import resolution
353-
* - these paths are tricky to cache since files come and go and paths that was bogus in the
354-
* past might not be bogus later
355-
*
356-
* In the initial experiments the impact of this caching was insignificant (single digit %) and
357-
* not worth the potential issues with stale cache records.
358-
*/
359339
getScriptSnapshot(tsFilePath: string): ts.IScriptSnapshot {
360-
let absoluteTsFilePath;
361-
362-
if (tsFilePath == this.defaultLibFilePath || path.isAbsolute(tsFilePath)) {
363-
absoluteTsFilePath = tsFilePath;
364-
} else if (this.compilerOptions.moduleResolution === ts.ModuleResolutionKind.NodeJs &&
365-
tsFilePath.match(/^node_modules/)) {
366-
absoluteTsFilePath = path.resolve(tsFilePath);
367-
} else if (tsFilePath.match(/^rxjs/)) {
368-
absoluteTsFilePath = path.resolve('node_modules', tsFilePath);
369-
} else if (tsFilePath.match(/^node_modules/)) {
370-
absoluteTsFilePath = path.resolve('node_modules/../', tsFilePath);
371-
} else {
372-
absoluteTsFilePath = path.join(this.treeInputPath, tsFilePath);
373-
}
340+
// TypeScript seems to request lots of bogus paths during import path lookup and resolution,
341+
// so we we just return undefined when the path is not correct.
374342

343+
// Ensure it is in the input tree or a lib.d.ts file.
344+
if (!startsWith(tsFilePath, this.treeInputPath) && !tsFilePath.match(/\/lib(\..*)*.d\.ts$/)) {
345+
if (fs.existsSync(tsFilePath)) {
346+
console.log('Rejecting', tsFilePath, '. File is not in the input tree.');
347+
}
348+
return undefined;
349+
}
375350

376-
if (!fs.existsSync(absoluteTsFilePath)) {
377-
// TypeScript seems to request lots of bogus paths during import path lookup and resolution,
378-
// so we we just return undefined when the path is not correct.
351+
// Ensure it exists
352+
if (!fs.existsSync(tsFilePath)) {
379353
return undefined;
380354
}
381-
return ts.ScriptSnapshot.fromString(fs.readFileSync(absoluteTsFilePath, FS_OPTS));
355+
356+
return ts.ScriptSnapshot.fromString(fs.readFileSync(tsFilePath, FS_OPTS));
382357
}
383358

384359

@@ -402,6 +377,10 @@ function clone<T>(object: T): T {
402377
return <T>result;
403378
}
404379

380+
function startsWith(str: string, substring: string): boolean {
381+
return str.substring(0, substring.length) === substring;
382+
}
383+
405384
function endsWith(str: string, substring: string): boolean {
406385
return str.indexOf(substring, str.length - substring.length) !== -1;
407386
}

tools/broccoli/trees/browser_tree.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,18 @@ module.exports = function makeBrowserTree(options, destinationPath) {
113113
{include: ['**/**'], exclude: ['e2e_test/**'], destDir: '/benchpress/'});
114114
}
115115

116+
let externalTypings =
117+
new Funnel('node_modules', {include: ['rxjs/**/*.d.ts', 'zone.js/**/*.d.ts']});
118+
119+
116120
var modulesTree = mergeTrees([
117121
angular2Tree,
118122
benchmarksTree,
119123
benchmarksExternalTree,
120124
payloadTestsTree,
121125
playgroundTree,
122-
benchpressTree
126+
benchpressTree,
127+
externalTypings,
123128
]);
124129

125130
var es6PolyfillTypings =

tools/broccoli/trees/node_tree.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,28 @@ module.exports = function makeNodeTree(projects, destinationPath) {
3030
'angular2/src/upgrade/**',
3131
'angular2/upgrade.ts',
3232
'angular2/platform/testing/**',
33+
'angular2/manual_typings/**',
34+
'angular2/typings/**'
3335
]
3436
});
3537

36-
let ambientTypings = [
38+
let externalTypings = [
3739
'angular2/typings/hammerjs/hammerjs.d.ts',
3840
'angular2/typings/node/node.d.ts',
39-
'node_modules/zone.js/dist/zone.js.d.ts',
4041
'angular2/manual_typings/globals.d.ts',
4142
'angular2/typings/es6-collections/es6-collections.d.ts',
4243
'angular2/typings/es6-promise/es6-promise.d.ts'
4344
];
4445

46+
let externalTypingsTree = new Funnel('modules', {files: externalTypings});
47+
48+
let packageTypings =
49+
new Funnel('node_modules', {include: ['rxjs/**/*.d.ts', 'zone.js/**/*.d.ts']});
50+
51+
let compileSrcContext = mergeTrees([srcTree, externalTypingsTree, packageTypings]);
52+
4553
// Compile the sources and generate the @internal .d.ts
46-
let compiledSrcTreeWithInternals = compileTree(srcTree, true, ambientTypings);
54+
let compiledSrcTreeWithInternals = compileTree(compileSrcContext, true, []);
4755

4856
var testTree = new Funnel('modules', {
4957
include: [
@@ -71,6 +79,8 @@ module.exports = function makeNodeTree(projects, destinationPath) {
7179
'angular2/test/platform/xhr_impl_spec.ts',
7280
'angular2/test/platform/browser/**/*.ts',
7381
'angular2/test/common/forms/**',
82+
'angular2/manual_typings/**',
83+
'angular2/typings/**',
7484

7585
// we call browser's bootstrap
7686
'angular2/test/router/route_config/route_config_spec.ts',
@@ -92,12 +102,17 @@ module.exports = function makeNodeTree(projects, destinationPath) {
92102
let srcPrivateDeclarations =
93103
new Funnel(compiledSrcTreeWithInternals, {srcDir: INTERNAL_TYPINGS_PATH});
94104

95-
testTree = mergeTrees([testTree, srcPrivateDeclarations]);
96-
97-
let compiledTestTree = compileTree(testTree, false, ambientTypings.concat([
105+
let testAmbients = [
98106
'angular2/typings/jasmine/jasmine.d.ts',
99107
'angular2/typings/angular-protractor/angular-protractor.d.ts',
100-
]));
108+
'angular2/typings/selenium-webdriver/selenium-webdriver.d.ts'
109+
];
110+
let testAmbientsTree = new Funnel('modules', {files: testAmbients});
111+
112+
testTree = mergeTrees(
113+
[testTree, srcPrivateDeclarations, testAmbientsTree, externalTypingsTree, packageTypings]);
114+
115+
let compiledTestTree = compileTree(testTree, false, []);
101116

102117
// Merge the compiled sources and tests
103118
let compiledSrcTree =

0 commit comments

Comments
 (0)
X Tutup