From a1ff310ed7c0d6a281c98e0b291577649e967c87 Mon Sep 17 00:00:00 2001 From: alex-klock Date: Wed, 4 Oct 2017 22:51:55 -0400 Subject: [PATCH 1/2] Ng2RouterPlugin's auto split feature now handles nested lazy modules. Updated fuse-box to 2.3.2. --- package.json | 4 +- src/bundling/plugins/ng2-router-plugin.ts | 67 ++++++++++++----------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index a463685..87aa975 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ng2-fused", - "version": "0.5.0", + "version": "0.5.1", "description": "FuseBox plugins and utilities for building Angular2 applications.", "author": "Alexander Klock", "repository": { @@ -18,7 +18,7 @@ "devDependencies": { "@types/jasmine": "^2.5.47", "@types/node": "^7.0.12", - "fuse-box": "^2.2.31", + "fuse-box": "^2.3.2", "jasmine": "^2.5.3", "rimraf": "^2.6.1", "typescript": "^2.4.2" diff --git a/src/bundling/plugins/ng2-router-plugin.ts b/src/bundling/plugins/ng2-router-plugin.ts index 28ad555..62df2b1 100644 --- a/src/bundling/plugins/ng2-router-plugin.ts +++ b/src/bundling/plugins/ng2-router-plugin.ts @@ -2,6 +2,7 @@ import { Plugin, WorkFlowContext } from 'fuse-box'; import * as fs from 'fs'; import * as path from 'path'; import * as escodegen from 'escodegen'; +import * as glob from 'glob'; import { NgContext } from '../../analysis/core/ng-context'; /** @@ -67,10 +68,11 @@ export class Ng2RouterPluginClass implements Plugin { * @param {Ng2LazyPluginOptions} [options] */ constructor(options?: Ng2RouterPluginOptions) { - this.options = Object.assign({ + this.options = Object.assign( { appPath: 'app', aotAppPath: 'aot/app', autoSplitBundle: 'app', + lazyModulePattern: '**/+*/!(*-route*|*-routing*).module.?(ngfactory.){ts,js}', loadChildrenPattern: /loadChildren[\s]*:[\s]*['|"](.*?)['|"]/gm, vendorBundle: 'vendors' }, options); @@ -92,39 +94,19 @@ export class Ng2RouterPluginClass implements Plugin { return; } + let homeDir = 'build/workspace'; let fusePath = this.options.aot ? this.options.aotAppPath : this.options.appPath; - let modulesRoot = path.join(context.homeDir, fusePath); - - let files = fs.readdirSync(modulesRoot); - for (let file of files) { - let featurePath = path.join(modulesRoot, file); - let stat = fs.statSync(featurePath); - if (stat.isDirectory() && file.indexOf('+') === 0) { - let featureName = file.substring(1); - let featureFiles = fs.readdirSync(featurePath); - let entryFile = featureFiles.find(f => { - return f.indexOf(featureName + '.module.') !== -1 && - f.indexOf('.spec.') === -1; - }); - if (!entryFile) { - entryFile = featureFiles.find(f => { - return f.indexOf('.module.') !== -1 && - f.indexOf('-routing.') === -1 && - f.indexOf('.spec.') === -1; - }); - } - if (!entryFile) { - entryFile = featureFiles.indexOf('index.ts') !== -1 ? 'index.ts' : null; - } + let modulePattern = path.posix.join(fusePath, this.options.lazyModulePattern); - if (entryFile) { - let entryPath = path.posix.join(fusePath, file, entryFile); - - context.bundle.split(`**/${file}/**`, `${featureName} > ${entryPath}`); - } else { - console.warn(`Unable to find entry file for feature folder at '${featurePath}. No split bundle added.`); - } - } + let files: string[] = glob.sync(modulePattern, { cwd: context.homeDir }); + files.sort((a, b) => { + let a1 = a.split('/').length; + let b1 = b.split('/').length; + return a1 > b1 ? 1 : a1 < b1 ? -1 : 0; + }); + for (let file of files) { + let feature = this.getFeatureInfoFromPath(file); + context.bundle.split(`${feature.path}/**`, `${feature.name} > ${file}`); } } @@ -241,6 +223,18 @@ export class Ng2RouterPluginClass implements Plugin { return null; } + public getFeatureInfoFromPath(filePath: string) { + let lastIndex = filePath.lastIndexOf('+') + 1; + let feature = filePath.substr(lastIndex).split('/')[0]; + if (!feature) { + throw new Error('Unable to determine feature name from \'' + filePath + '\'.'); + } + return { + name: feature, + path: filePath.substr(0, lastIndex) + `${feature}` + }; + } + /** * Parses the loadChildren string for module information. * @@ -318,6 +312,12 @@ export interface Ng2RouterPluginOptions { */ loadChildrenPattern?: RegExp; + /** + * Glob pattern for matching lazy module folders. + * Note that the appPath (or aotAppPath) is prefix to the pattern. + */ + lazyModulePattern?: string; + /** * The test property for FuseBox plugins. Can be a regular expression or a string for a simplified regexp. * Defaults to '*.ts$|*.js$'. @@ -359,4 +359,5 @@ export interface LazyModuleInfo { * @type {string} */ moduleName?: string; -} \ No newline at end of file +} +// glob('src/app/**/+*/!(*-route*|*-routing*).module.{ts,js}', (err, files) => { console.log(files); });> glob('src/app/**/+*/!(*-route*|*-routing*).module.{ts,js}', (err, files) => { console.log(files); }); \ No newline at end of file From 8fbaabf0176342d46479d3c28de36a1ebf76b7fb Mon Sep 17 00:00:00 2001 From: alex-klock Date: Wed, 4 Oct 2017 22:58:03 -0400 Subject: [PATCH 2/2] Added usebox-angular-universal-starter project link to README.md. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0477978..d3510f0 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ npm install ng2-fused --save-dev [![NPM](https://nodei.co/npm/ng2-fused.png?downloads=true)](https://nodei.co/npm/ng2-fused/) -Check out the [ng2-fused-seed](https://github.com/alex-klock/ng2-fused-seed) project for a working starter project utilizing the following plugins. +Check out the [ng2-fused-seed](https://github.com/alex-klock/ng2-fused-seed) project for a working starter project utilizing the following plugins. Or [fusebox-angular-universal-starter](https://github.com/patrickmichalina/fusebox-angular-universal-starter) for a fully featured angular2 seed. ## Ng2TemplatePlugin @@ -165,4 +165,4 @@ By default this plugin tests for the file `/spec-bundle\.(ts|js)$/`, if you wish * More unit tests. * More samples. -For a seed project utilizing FuseBox and Ng2Fused, check out https://github.com/alex-klock/ng2-fused-seed. \ No newline at end of file +For a seed project utilizing FuseBox and Ng2Fused, check out https://github.com/alex-klock/ng2-fused-seed or https://github.com/patrickmichalina/fusebox-angular-universal-starter. \ No newline at end of file