Skip to content

chore: use _sourceDir everywhere #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 48 additions & 75 deletions lib/broccoli/angular2-app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';
const fs = require('fs');
const glob = require('glob');
const path = require('path');

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

this._options = options;
this._sourceDir = options.sourceDir || 'src/client';
this._inputNode = inputNode || this._sourceDir;
this._inputNode = inputNode || this._buildInputTree();
this._destDir = options.destDir || '';

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

_buildInputTree() {
return new BroccoliMergeTrees([
new BroccoliFunnel(this._sourceDir, { destDir: this._sourceDir }),
new BroccoliFunnel('typings', { destDir: 'typings' }),
new BroccoliFunnel('public', { destDir: 'public' }),
this._getConfigTree()
], { overwrite: true });
}

/**
* Create and return the app build system tree that:
* - Get the `assets` tree
Expand All @@ -84,27 +91,31 @@ class Angular2App extends BroccoliPlugin {
var tsTree = this._getTsTree();
var indexTree = this._getIndexTree();
var vendorNpmTree = this._getVendorNpmTree();
var excludeDotfilesTree = this._getPublicTree();

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

if (fs.existsSync('public')) {
buildTrees.push(excludeDotfilesTree);
for (const suffix of ['sass', 'less', 'stylus', 'compass']) {
const plugin = require(`./angular-broccoli-${suffix}`);
const tree = plugin.makeBroccoliTree(this._inputNode, this._options[`${suffix}Compiler`]);

if (!tree) {
continue;
}

buildTrees.push(new BroccoliFunnel(tree, {
include: ['**/*'],
getDestinationPath: (n) => {
if (n.startsWith(this._sourceDir)) {
return n.substr(this._sourceDir.length);
}
return n;
}
}));
}

buildTrees = buildTrees.concat(
require('./angular-broccoli-sass').makeBroccoliTree(this._inputNode,
this._options.sassCompiler),
require('./angular-broccoli-less').makeBroccoliTree(this._inputNode,
this._options.lessCompiler),
require('./angular-broccoli-stylus').makeBroccoliTree(this._inputNode,
this._options.stylusCompiler),
require('./angular-broccoli-compass').makeBroccoliTree(this._inputNode,
this._options.compassCompiler)
).filter(x => !!x);

var merged = BroccoliMergeTrees(buildTrees, { overwrite: true });
return new BroccoliFunnel(BroccoliMergeTrees([merged, new BroccoliSwManifest([merged])]), {
var merged = new BroccoliMergeTrees(buildTrees, { overwrite: true });
merged = new BroccoliMergeTrees([merged, new BroccoliSwManifest([merged])]);
return new BroccoliFunnel(merged, {
destDir: this._destDir
});
}
Expand Down Expand Up @@ -215,19 +226,22 @@ class Angular2App extends BroccoliPlugin {
* @return {Tree} Tree for app/index.html.
*/
_getIndexTree() {
var htmlName = 'index.html';
var files = [
'index.html'
];

var index = new BroccoliFunnel(this._inputNode, {
files: files,
description: 'BroccoliFunnel: index.html'
let indexTree = new BroccoliFunnel(this._inputNode, {
include: files.map(name => path.join(this._sourceDir, name)),
getDestinationPath: (n) => {
if (n.startsWith(this._sourceDir)) {
return n.substr(this._sourceDir.length);
}
return n;
}
});


return BroccoliConfigReplace(index, {
files: [htmlName],
return BroccoliConfigReplace(indexTree, {
files: files,
patterns: this._getReplacePatterns()
});
}
Expand Down Expand Up @@ -268,42 +282,8 @@ class Angular2App extends BroccoliPlugin {
* @return {Tree} Tree for TypeScript files.
*/
_getTsTree() {
var typingsTree = this._getTypingsTree();
var sourceTree = this._getSourceTree();
var configTree = this._getConfigTree();

var tsConfigPath = path.join(this._sourceDir, 'tsconfig.json');
var tsconfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));

// Add all glob files to files. In some cases we don't want to specify
let globFiles = this._tsCompiler.additionalFiles;
if (globFiles) {
if (typeof globFiles == 'string') {
globFiles = [globFiles];
}

for (const g of globFiles) {
const files = glob(g, { sync: true, cwd: this._sourceDir, root: this._sourceDir });
tsconfig.files = tsconfig.files.concat(files);
}
}

// Remove dupes in tsconfig.files.
const fileNameMap = {};
tsconfig.files = tsconfig.files.filter(fileName => {
if (fileNameMap[fileName]) {
return false;
}
fileNameMap[fileName] = true;
return true;
});

// Because the tsconfig does not include the source directory, add this as the first path
// element.
tsconfig.files = tsconfig.files.map(name => path.join(this._sourceDir, name));

var mergedTree = BroccoliMergeTrees([sourceTree, typingsTree, configTree], { overwrite: true });
var tsTree = new BroccoliTypescript(mergedTree, tsconfig);
var tsTree = new BroccoliTypescript(this._inputNode, tsConfigPath, this._tsCompiler);

var tsTreeExcludes = ['*.d.ts', 'tsconfig.json'];
var excludeSpecFiles = '**/*.spec.*';
Expand Down Expand Up @@ -362,6 +342,7 @@ class Angular2App extends BroccoliPlugin {
*/
_getAssetsTree() {
return new BroccoliFunnel(this._inputNode, {
include: [path.join(this._sourceDir, '**/*')],
exclude: [
'**/*.ts',
'**/*.js',
Expand All @@ -370,20 +351,12 @@ class Angular2App extends BroccoliPlugin {
'**/*.less',
'**/*.styl'
],
allowEmpty: true
});
}

/**
* Returns the `excludeDotfiles` tree.
*
* @private
* @method _getPublicTree
* @return {Tree} The dotfiles exclusion tree.
*/
_getPublicTree() {
return new BroccoliFunnel('public', {
exclude: ['**/.*'],
getDestinationPath: (n) => {
if (n.startsWith(this._sourceDir)) {
return n.substr(this._sourceDir.length);
}
return n;
},
allowEmpty: true
});
}
Expand Down
52 changes: 47 additions & 5 deletions lib/broccoli/broccoli-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const ts = require('typescript');
const glob = require('glob');

const FS_OPTS = {
encoding: 'utf-8'
Expand All @@ -22,7 +23,7 @@ const FS_OPTS = {
* requires global emit, which can affect many files.
*/
class BroccoliTypeScriptCompiler extends Plugin {
constructor(inputPath, options) {
constructor(inputPath, tsConfigPath, options) {
super([inputPath], {});

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

this._tsConfigPath = tsConfigPath;
this._options = options;
if (options.files) {
this._rootFilePaths = options.files.splice(0);
}
}

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

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

_loadTsConfig() {
var tsConfigPath = path.join(this.inputPaths[0], this._tsConfigPath);
var tsconfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));

tsconfig.files = tsconfig.files.map(name => path.join(path.dirname(this._tsConfigPath), name));

// Add all glob files to files. In some cases we don't want to specify
let globFiles = this._options.additionalFiles;
if (globFiles) {
if (typeof globFiles == 'string') {
globFiles = [globFiles];
}

for (const g of globFiles) {
const files = glob(g, { sync: true, cwd: this.inputPaths[0], root: this.inputPaths[0] });
tsconfig.files = tsconfig.files.concat(files);
}
}

// Remove dupes in tsconfig.files.
const fileNameMap = {};
tsconfig.files = tsconfig.files.filter(fileName => {
if (fileNameMap[fileName]) {
return false;
}
fileNameMap[fileName] = true;
return true;
});

// Because the tsconfig does not include the source directory, add this as the first path
// element.
tsconfig.files = tsconfig.files.map(name => path.join(this.inputPaths[0], name));
return tsconfig;
}

_createServiceHost() {
var tsconfig = this._loadTsConfig();

this._tsConfigFiles = tsconfig.files.splice(0);

// the conversion is a bit awkward, see https://github.com/Microsoft/TypeScript/issues/5276
// in 1.8 use convertCompilerOptionsFromJson
this._tsOpts = ts.parseJsonConfigFileContent(this._options, null, null).options;
this._tsOpts = ts.parseJsonConfigFileContent(tsconfig, null, null).options;
this._tsOpts.rootDir = '';
this._tsOpts.outDir = '';

Expand Down