Skip to content

Commit 78e15ad

Browse files
committed
chore: use _sourceDir everywhere
1 parent 875a743 commit 78e15ad

File tree

2 files changed

+95
-80
lines changed

2 files changed

+95
-80
lines changed

lib/broccoli/angular2-app.js

Lines changed: 48 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
'use strict';
2-
const fs = require('fs');
3-
const glob = require('glob');
42
const path = require('path');
53

64
const isProduction = require('./is-production');
@@ -26,7 +24,7 @@ class Angular2App extends BroccoliPlugin {
2624

2725
this._options = options;
2826
this._sourceDir = options.sourceDir || 'src/client';
29-
this._inputNode = inputNode || this._sourceDir;
27+
this._inputNode = inputNode || this._buildInputTree();
3028
this._destDir = options.destDir || '';
3129

3230
// By default, add all spec files to the tsCompiler.
@@ -65,6 +63,15 @@ class Angular2App extends BroccoliPlugin {
6563
return this._tree.cleanup();
6664
}
6765

66+
_buildInputTree() {
67+
return new BroccoliMergeTrees([
68+
new BroccoliFunnel(this._sourceDir, { destDir: this._sourceDir }),
69+
new BroccoliFunnel('typings', { destDir: 'typings' }),
70+
new BroccoliFunnel('public', { destDir: 'public' }),
71+
this._getConfigTree()
72+
], { overwrite: true });
73+
}
74+
6875
/**
6976
* Create and return the app build system tree that:
7077
* - Get the `assets` tree
@@ -84,27 +91,31 @@ class Angular2App extends BroccoliPlugin {
8491
var tsTree = this._getTsTree();
8592
var indexTree = this._getIndexTree();
8693
var vendorNpmTree = this._getVendorNpmTree();
87-
var excludeDotfilesTree = this._getPublicTree();
8894

8995
var buildTrees = [assetTree, tsTree, indexTree, vendorNpmTree];
9096

91-
if (fs.existsSync('public')) {
92-
buildTrees.push(excludeDotfilesTree);
97+
for (const suffix of ['sass', 'less', 'stylus', 'compass']) {
98+
const plugin = require(`./angular-broccoli-${suffix}`);
99+
const tree = plugin.makeBroccoliTree(this._inputNode, this._options[`${suffix}Compiler`]);
100+
101+
if (!tree) {
102+
continue;
103+
}
104+
105+
buildTrees.push(new BroccoliFunnel(tree, {
106+
include: ['**/*'],
107+
getDestinationPath: (n) => {
108+
if (n.startsWith(this._sourceDir)) {
109+
return n.substr(this._sourceDir.length);
110+
}
111+
return n;
112+
}
113+
}));
93114
}
94115

95-
buildTrees = buildTrees.concat(
96-
require('./angular-broccoli-sass').makeBroccoliTree(this._inputNode,
97-
this._options.sassCompiler),
98-
require('./angular-broccoli-less').makeBroccoliTree(this._inputNode,
99-
this._options.lessCompiler),
100-
require('./angular-broccoli-stylus').makeBroccoliTree(this._inputNode,
101-
this._options.stylusCompiler),
102-
require('./angular-broccoli-compass').makeBroccoliTree(this._inputNode,
103-
this._options.compassCompiler)
104-
).filter(x => !!x);
105-
106-
var merged = BroccoliMergeTrees(buildTrees, { overwrite: true });
107-
return new BroccoliFunnel(BroccoliMergeTrees([merged, new BroccoliSwManifest([merged])]), {
116+
var merged = new BroccoliMergeTrees(buildTrees, { overwrite: true });
117+
merged = new BroccoliMergeTrees([merged, new BroccoliSwManifest([merged])]);
118+
return new BroccoliFunnel(merged, {
108119
destDir: this._destDir
109120
});
110121
}
@@ -215,19 +226,22 @@ class Angular2App extends BroccoliPlugin {
215226
* @return {Tree} Tree for app/index.html.
216227
*/
217228
_getIndexTree() {
218-
var htmlName = 'index.html';
219229
var files = [
220230
'index.html'
221231
];
222232

223-
var index = new BroccoliFunnel(this._inputNode, {
224-
files: files,
225-
description: 'BroccoliFunnel: index.html'
233+
let indexTree = new BroccoliFunnel(this._inputNode, {
234+
include: files.map(name => path.join(this._sourceDir, name)),
235+
getDestinationPath: (n) => {
236+
if (n.startsWith(this._sourceDir)) {
237+
return n.substr(this._sourceDir.length);
238+
}
239+
return n;
240+
}
226241
});
227242

228-
229-
return BroccoliConfigReplace(index, {
230-
files: [htmlName],
243+
return BroccoliConfigReplace(indexTree, {
244+
files: files,
231245
patterns: this._getReplacePatterns()
232246
});
233247
}
@@ -268,42 +282,8 @@ class Angular2App extends BroccoliPlugin {
268282
* @return {Tree} Tree for TypeScript files.
269283
*/
270284
_getTsTree() {
271-
var typingsTree = this._getTypingsTree();
272-
var sourceTree = this._getSourceTree();
273-
var configTree = this._getConfigTree();
274-
275285
var tsConfigPath = path.join(this._sourceDir, 'tsconfig.json');
276-
var tsconfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));
277-
278-
// Add all glob files to files. In some cases we don't want to specify
279-
let globFiles = this._tsCompiler.additionalFiles;
280-
if (globFiles) {
281-
if (typeof globFiles == 'string') {
282-
globFiles = [globFiles];
283-
}
284-
285-
for (const g of globFiles) {
286-
const files = glob(g, { sync: true, cwd: this._sourceDir, root: this._sourceDir });
287-
tsconfig.files = tsconfig.files.concat(files);
288-
}
289-
}
290-
291-
// Remove dupes in tsconfig.files.
292-
const fileNameMap = {};
293-
tsconfig.files = tsconfig.files.filter(fileName => {
294-
if (fileNameMap[fileName]) {
295-
return false;
296-
}
297-
fileNameMap[fileName] = true;
298-
return true;
299-
});
300-
301-
// Because the tsconfig does not include the source directory, add this as the first path
302-
// element.
303-
tsconfig.files = tsconfig.files.map(name => path.join(this._sourceDir, name));
304-
305-
var mergedTree = BroccoliMergeTrees([sourceTree, typingsTree, configTree], { overwrite: true });
306-
var tsTree = new BroccoliTypescript(mergedTree, tsconfig);
286+
var tsTree = new BroccoliTypescript(this._inputNode, tsConfigPath, this._tsCompiler);
307287

308288
var tsTreeExcludes = ['*.d.ts', 'tsconfig.json'];
309289
var excludeSpecFiles = '**/*.spec.*';
@@ -362,6 +342,7 @@ class Angular2App extends BroccoliPlugin {
362342
*/
363343
_getAssetsTree() {
364344
return new BroccoliFunnel(this._inputNode, {
345+
include: [path.join(this._sourceDir, '**/*')],
365346
exclude: [
366347
'**/*.ts',
367348
'**/*.js',
@@ -370,20 +351,12 @@ class Angular2App extends BroccoliPlugin {
370351
'**/*.less',
371352
'**/*.styl'
372353
],
373-
allowEmpty: true
374-
});
375-
}
376-
377-
/**
378-
* Returns the `excludeDotfiles` tree.
379-
*
380-
* @private
381-
* @method _getPublicTree
382-
* @return {Tree} The dotfiles exclusion tree.
383-
*/
384-
_getPublicTree() {
385-
return new BroccoliFunnel('public', {
386-
exclude: ['**/.*'],
354+
getDestinationPath: (n) => {
355+
if (n.startsWith(this._sourceDir)) {
356+
return n.substr(this._sourceDir.length);
357+
}
358+
return n;
359+
},
387360
allowEmpty: true
388361
});
389362
}

lib/broccoli/broccoli-typescript.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fs = require('fs');
55
const fse = require('fs-extra');
66
const path = require('path');
77
const ts = require('typescript');
8+
const glob = require('glob');
89

910
const FS_OPTS = {
1011
encoding: 'utf-8'
@@ -22,7 +23,7 @@ const FS_OPTS = {
2223
* requires global emit, which can affect many files.
2324
*/
2425
class BroccoliTypeScriptCompiler extends Plugin {
25-
constructor(inputPath, options) {
26+
constructor(inputPath, tsConfigPath, options) {
2627
super([inputPath], {});
2728

2829
this._fileRegistry = Object.create(null);
@@ -31,10 +32,8 @@ class BroccoliTypeScriptCompiler extends Plugin {
3132
this._tsServiceHost = null;
3233
this._tsService = null;
3334

35+
this._tsConfigPath = tsConfigPath;
3436
this._options = options;
35-
if (options.files) {
36-
this._rootFilePaths = options.files.splice(0);
37-
}
3837
}
3938

4039
build() {
@@ -56,6 +55,10 @@ class BroccoliTypeScriptCompiler extends Plugin {
5655
if (!tsFilePath.match(/\.ts$/) || !fs.existsSync(tsFilePath)) {
5756
return;
5857
}
58+
// Remove d.ts files that aren't part of the tsconfig files.
59+
if (tsFilePath.match(/\.d\.ts$/) && this._tsConfigFiles.indexOf(tsFilePath) == -1) {
60+
return;
61+
}
5962

6063
if (!this._fileRegistry[tsFilePath]) {
6164
// Not in the registry? Add it.
@@ -115,10 +118,49 @@ class BroccoliTypeScriptCompiler extends Plugin {
115118
}
116119
}
117120

121+
_loadTsConfig() {
122+
var tsConfigPath = path.join(this.inputPaths[0], this._tsConfigPath);
123+
var tsconfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));
124+
125+
tsconfig.files = tsconfig.files.map(name => path.join(path.dirname(this._tsConfigPath), name));
126+
127+
// Add all glob files to files. In some cases we don't want to specify
128+
let globFiles = this._options.additionalFiles;
129+
if (globFiles) {
130+
if (typeof globFiles == 'string') {
131+
globFiles = [globFiles];
132+
}
133+
134+
for (const g of globFiles) {
135+
const files = glob(g, { sync: true, cwd: this.inputPaths[0], root: this.inputPaths[0] });
136+
tsconfig.files = tsconfig.files.concat(files);
137+
}
138+
}
139+
140+
// Remove dupes in tsconfig.files.
141+
const fileNameMap = {};
142+
tsconfig.files = tsconfig.files.filter(fileName => {
143+
if (fileNameMap[fileName]) {
144+
return false;
145+
}
146+
fileNameMap[fileName] = true;
147+
return true;
148+
});
149+
150+
// Because the tsconfig does not include the source directory, add this as the first path
151+
// element.
152+
tsconfig.files = tsconfig.files.map(name => path.join(this.inputPaths[0], name));
153+
return tsconfig;
154+
}
155+
118156
_createServiceHost() {
157+
var tsconfig = this._loadTsConfig();
158+
159+
this._tsConfigFiles = tsconfig.files.splice(0);
160+
119161
// the conversion is a bit awkward, see https://github.com/Microsoft/TypeScript/issues/5276
120162
// in 1.8 use convertCompilerOptionsFromJson
121-
this._tsOpts = ts.parseJsonConfigFileContent(this._options, null, null).options;
163+
this._tsOpts = ts.parseJsonConfigFileContent(tsconfig, null, null).options;
122164
this._tsOpts.rootDir = '';
123165
this._tsOpts.outDir = '';
124166

0 commit comments

Comments
 (0)