From 78e15ad729c164e8be0913244c35f26d086ba619 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Tue, 26 Apr 2016 18:01:29 -0700 Subject: [PATCH] chore: use _sourceDir everywhere --- lib/broccoli/angular2-app.js | 123 +++++++++++----------------- lib/broccoli/broccoli-typescript.js | 52 ++++++++++-- 2 files changed, 95 insertions(+), 80 deletions(-) diff --git a/lib/broccoli/angular2-app.js b/lib/broccoli/angular2-app.js index ea647f3a23e4..0839f6c5652d 100644 --- a/lib/broccoli/angular2-app.js +++ b/lib/broccoli/angular2-app.js @@ -1,6 +1,4 @@ 'use strict'; -const fs = require('fs'); -const glob = require('glob'); const path = require('path'); const isProduction = require('./is-production'); @@ -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. @@ -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 @@ -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 }); } @@ -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() }); } @@ -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.*'; @@ -362,6 +342,7 @@ class Angular2App extends BroccoliPlugin { */ _getAssetsTree() { return new BroccoliFunnel(this._inputNode, { + include: [path.join(this._sourceDir, '**/*')], exclude: [ '**/*.ts', '**/*.js', @@ -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 }); } diff --git a/lib/broccoli/broccoli-typescript.js b/lib/broccoli/broccoli-typescript.js index 214ed522e506..2fa75c02354d 100644 --- a/lib/broccoli/broccoli-typescript.js +++ b/lib/broccoli/broccoli-typescript.js @@ -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' @@ -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); @@ -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() { @@ -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. @@ -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 = '';