Skip to content

Commit

Permalink
Merge pull request #9 from alex-klock/develop
Browse files Browse the repository at this point in the history
0.5.1
  • Loading branch information
alex-klock authored Oct 5, 2017
2 parents ecd81f4 + 8fbaabf commit 5f15d11
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
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.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
Expand Down
67 changes: 34 additions & 33 deletions src/bundling/plugins/ng2-router-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -67,10 +68,11 @@ export class Ng2RouterPluginClass implements Plugin {
* @param {Ng2LazyPluginOptions} [options]
*/
constructor(options?: Ng2RouterPluginOptions) {
this.options = Object.assign({
this.options = Object.assign(<Ng2RouterPluginOptions> {
appPath: 'app',
aotAppPath: 'aot/app',
autoSplitBundle: 'app',
lazyModulePattern: '**/+*/!(*-route*|*-routing*).module.?(ngfactory.){ts,js}',
loadChildrenPattern: /loadChildren[\s]*:[\s]*['|"](.*?)['|"]/gm,
vendorBundle: 'vendors'
}, options);
Expand All @@ -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}`);
}
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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$'.
Expand Down Expand Up @@ -359,4 +359,5 @@ export interface LazyModuleInfo {
* @type {string}
*/
moduleName?: string;
}
}
// 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); });

0 comments on commit 5f15d11

Please sign in to comment.