Skip to content
This repository was archived by the owner on Jul 4, 2023. It is now read-only.

Commit 70cc688

Browse files
author
Jesse Zhang
committed
docs
1 parent 2049572 commit 70cc688

File tree

6 files changed

+94
-42
lines changed

6 files changed

+94
-42
lines changed

README.md

+18-21
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,23 @@ Dependency Tree Building:
5151
Additional Files:
5252
* **requiredLibs** (Array<sup>1</sup> | String | RegExp): Files included but *not* parsed for dependencies. Use this in conjunction with *parseExclude*.
5353
* **optionalLibs** (Array<sup>1</sup> | String | RegExp): Optional library files that, if depended on, will also include all files matched by the *optionalLibsInclude*.
54-
* **optionalLibsInclude** (String): Glob of files relative to the optional library file to be included if the optional library file is depended on.
54+
* **optionalLibsInclude** (Array<sup>1</sup> | String | RegExp): Optional library files to include if matching optional library defined by *optionalLib* is included
5555

5656
Modules and Dependencies:
5757
* **appModule** (String): Name of main Angular module. This will be used to create an `init.js` file in the stream which will contain `angular.module(appModule, [all required modules]);`.
5858
* **globalModules** (Array | String): Modules to include in the `init.js` modules list that will not be found via the dependency tree building (e.g. modules in files from both *requiredLibs* and *parseExclude*).
5959
* **globalDependencies** (Array | String): Dependencies that will be defined globally but will not be found via the dependency tree building (e.g. dependencies from modules in *globalModules*). Unfound dependencies will throw a gulp error otherwise.
60-
* **filesWithResolvedDeps** (Array<sup>1</sup> | String | RegExp): Files that contain resolved dependencies (e.g. ui-bootstrap modal controllers).
6160

6261
Misc:
63-
* **ignoredTemplates** (Array<sup>1</sup> | String | RegExp): Template strings to ignore looking for.
6462
* **filePriority** (Array | String): Files sorted to the top of the stream.
6563

6664
<sup>1</sup> Array of Strings or RegExps. Files are selected by partial matches.
6765

6866
---
6967
## Notes
68+
#### Watch mode
69+
TODO
70+
7071
#### Automatically Required Files
7172
TODO
7273

@@ -77,17 +78,18 @@ TODO
7778
* Directives need to return an object, not a reference to an object
7879
* `return { templateUrl: "...", link: "..." };`
7980
* Not `var d = { templateUrl: "...", link: "..." }; return d;`
81+
* Resolved injections into controllers uses the "resolve" property for the object defining the resolve (see ui-router or ui-bootstrap modals)
8082

8183
#### Misc:
8284
* All dependencies beginning with $ will be ignored.
8385
* Inline controllers within directives will be parsed if the controller function block is defined in the return object (i.e. not a reference to the controller).
8486
* Anything that matches the pattern `"controller": "SomeCtrl"` will consider `SomeCtrl` to be a dependency. (Quotes can be single or double; object key does not require quotes.)
8587
* All strings found in the file ending in *.html* or *.json* will be considered a template of that file (unless ignored via the *ignoredTemplates* option).
88+
* Supports $inject property
8689

8790
---
8891
## Limitations
8992
* Dynamically built template URLs will not be included (in both html partials and js files)
90-
* No support (yet?) for $inject property
9193

9294
---
9395
## Example
@@ -96,35 +98,31 @@ TODO
9698
```
9799
module.exports = {
98100
seed: [
99-
"./index.html.ejs",
100-
101-
"./shared/app/app.js",
102-
"./shared/app/routes.js",
103-
104-
"./local/app/app.js",
105-
"./local/app/routes.js"
101+
"./index.html.ejs"
106102
],
107103
options: {
108104
parseExclude: [
109105
"/libs/",
110106
/\/libs-optional\/[^\/]+\/includes\//i
111107
],
112108
requiredFiles: [],
113-
ignoredTemplates: [
114-
/assets/
115-
],
116109
requiredLibs: [
117110
"/libs/"
118111
],
119112
filePriority: [
120-
"jquery.js",
121-
"lodash.js",
122-
"angular.js"
113+
"/libs/jquery/jquery.js",
114+
"/libs/lodash/lodash.js",
115+
"/libs/angular/angular.js",
116+
"/libs/jquery/",
117+
"/libs/lodash/",
118+
"/libs/angular/"
123119
],
124120
optionalLibs: [
125121
"/libs-optional/"
126122
],
127-
optionalLibsInclude: "includes/*.js",
123+
optionalLibsInclude: [
124+
/\/libs-optional\/[^\/]+\/includes\/[^\/]+\.js/i
125+
],
128126
globalDependencies: [
129127
"Restangular"
130128
],
@@ -135,9 +133,8 @@ module.exports = {
135133
"ui.bootstrap",
136134
"restangular"
137135
],
138-
filesWithResolvedDeps: [
139-
/modal/i
140-
]
136+
verbose: true,
137+
debug: false
141138
}
142139
}
143140
```

index.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var through = require("through2"),
22
gutil = require("gulp-util"),
33
Graph = require("./lib/Graph"),
4-
filters = require("./consts/angular-filters");
4+
filters = require("./consts/angular-filters"),
5+
PluginError = require("./lib/errors").PluginError;
56

67
module.exports = function (seeds, options) {
78
options = parseOptions(options);
@@ -73,10 +74,34 @@ module.exports.watch = function (seeds, options) {
7374
};
7475

7576
function parseOptions(options) {
76-
// TODO: add defaults
77+
if (!options.appModule) {
78+
throw new PluginError("Missing options.appModule in configuration.");
79+
}
80+
81+
options.parseExclude = parseOption(options.parseExclude);
82+
options.requiredFiles = parseOption(options.requiredFiles);
83+
options.requiredLibs = parseOption(options.requiredLibs);
84+
options.filePriority = parseOption(options.filePriority);
85+
options.optionalLibs = parseOption(options.optionalLibs);
86+
options.optionalLibsInclude = parseOption(options.optionalLibsInclude);
87+
options.globalDependencies = parseOption(options.globalDependencies);
88+
options.globalModules = parseOption(options.globalModules);
89+
90+
options.verbose = options.verbose || false;
91+
options.debug = options.debug || false;
7792

7893
filters.forEach(function (filter) {
7994
options.globalDependencies.push("filter:" + filter);
8095
});
8196
return options;
97+
}
98+
99+
function parseOption(option) {
100+
if (option instanceof Array) {
101+
return option;
102+
}
103+
if (option == null) {
104+
return [];
105+
}
106+
return [option];
82107
}

lib/MetaData.js

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var MetaData = function (path) {
3535
this.resolves = {};
3636

3737
this.type = {
38+
app: false,
3839
config: false,
3940
run: false,
4041
required: false
@@ -114,6 +115,10 @@ MetaData.prototype.addItem = function (type, item) {
114115
}
115116
};
116117

118+
MetaData.prototype.isApp = function () {
119+
this.type.app = true;
120+
this.isRequired();
121+
};
117122
MetaData.prototype.isConfig = function () {
118123
this.type.config = true;
119124
this.isRequired();
@@ -141,13 +146,21 @@ MetaData.prototype.merge = function (src) {
141146
});
142147
});
143148

149+
if (src.type.app) {
150+
this.isApp();
151+
}
152+
144153
if (src.type.config) {
145154
this.isConfig();
146155
}
147156

148157
if (src.type.run) {
149158
this.isRun();
150159
}
160+
161+
if (src.type.required) {
162+
this.isRequired();
163+
}
151164
};
152165

153166
module.exports = MetaData;

lib/errors.js

+7
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ module.exports.GraphError = function (reason, component, path) {
3939
"\nComponent: " + component +
4040
"\nReason: " + reason
4141
);
42+
};
43+
44+
module.exports.PluginError = function (reason) {
45+
return new gutil.PluginError(
46+
"gulp-angular-builder",
47+
reason
48+
);
4249
};

lib/parsers/html-parser.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ var htmlparser = require("htmlparser2"),
33

44
module.exports = function (contents, metadata) {
55
metadata = metadata || new MetaData();
6-
7-
var parsed = parseContent(contents);
8-
Object.keys(parsed).forEach(function (type) {
9-
parsed[type].forEach(function (dep) {
10-
metadata.addDependency(type, dep);
11-
});
12-
});
13-
6+
parseContent(contents, metadata);
147
return metadata;
158
};
169

17-
function parseContent(contents) {
18-
var component = [],
10+
function parseContent(contents, metadata) {
11+
var isApp = false,
12+
component = [],
1913
template = [],
2014
directiveToken = [],
2115
animationToken = [],
@@ -48,7 +42,9 @@ function parseContent(contents) {
4842
}
4943

5044
Object.keys(attributes).forEach(function (attr) {
51-
if (attr == "ng-include") {
45+
if (attr == "ng-app") {
46+
isApp = true;
47+
} else if (attr == "ng-include") {
5248
// Add template
5349
template.push(attributes[attr]);
5450
} else if (attr == "ng-controller") {
@@ -88,11 +84,25 @@ function parseContent(contents) {
8884
return _;
8985
});
9086

91-
return {
92-
component: component,
93-
filter: filter,
94-
template: template,
95-
directiveToken: directiveToken,
96-
animationToken: animationToken
97-
};
87+
component.forEach(function (dep) {
88+
metadata.addDependency("component", dep);
89+
});
90+
filter.forEach(function (dep) {
91+
metadata.addDependency("filter", dep);
92+
});
93+
template.forEach(function (dep) {
94+
metadata.addDependency("template", dep);
95+
});
96+
directiveToken.forEach(function (dep) {
97+
metadata.addDependency("directiveToken", dep);
98+
});
99+
animationToken.forEach(function (dep) {
100+
metadata.addDependency("animationToken", dep);
101+
});
102+
103+
if (isApp) {
104+
metadata.isApp();
105+
}
106+
107+
return metadata;
98108
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-angular-builder",
3-
"version": "0.3.0-beta",
3+
"version": "0.3.0",
44
"description": "Gulp plugin to filter and include only necessary AngularJS files.",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)