UNPKG

5.2 kBMarkdownView Raw
1# gulp-ng-html2js [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]
2
3> A plugin for [gulp](https://github.com/wearefractal/gulp) which generates AngularJS modules, which pre-load your HTML
4code into the [$templateCache](http://docs.angularjs.org/api/ng.$templateCache). This way AngularJS doesn't need to
5request the actual HTML files anymore.
6
7## Usage
8
9First, install `gulp-ng-html2js` as a development dependency:
10
11```shell
12npm install --save-dev gulp-ng-html2js
13```
14
15Then, add it to your `gulpfile.js`:
16
17```javascript
18var ngHtml2Js = require("gulp-ng-html2js");
19
20gulp.src("./partials/*.html")
21 .pipe(ngHtml2Js({
22 moduleName: "MyAwesomePartials",
23 prefix: "/partials"
24 }))
25 .pipe(gulp.dest("./dist/partials"));
26```
27
28The main reason to use this module would be optimization. By pre-loading the HTML files, you can spare requests and
29loading time when the files are actually needed. When you are optimizing, you should do it properly. So, we should add
30the following plugins: `gulp-minify-html`, `gulp-uglify`, and `gulp-concat`:
31
32```javascript
33var ngHtml2Js = require("gulp-ng-html2js");
34var minifyHtml = require("gulp-minify-html");
35var concat = require("gulp-concat");
36var uglify = require("gulp-uglify");
37
38gulp.src("./partials/*.html")
39 .pipe(minifyHtml({
40 empty: true,
41 spare: true,
42 quotes: true
43 }))
44 .pipe(ngHtml2Js({
45 moduleName: "MyAwesomePartials",
46 prefix: "/partials"
47 }))
48 .pipe(concat("partials.min.js"))
49 .pipe(uglify())
50 .pipe(gulp.dest("./dist/partials"));
51```
52
53This way you end up with 1 single, minified Javascript file, which pre-loads all the (minified) HTML templates.
54
55If you have your modules sorted into directories that match the module name, you could do something like this:
56
57```javascript
58// This picks up files like this:
59// partials/date-picker/year.html (as well as month.html, day.html)
60// partials/expanded-combo-box/combobox.html
61// partials/forms/feedback.html (as well as survey.html, contact.html)
62// Returns modules like this:
63// datePicker, expandedComboBox, forms
64gulp.src("./partials/**/*.html")
65 .pipe(ngHtml2Js({
66 moduleName: function (file) {
67 var pathParts = file.path.split('/');
68 var folder = pathParts[pathParts.length - 2];
69 return folder.replace(/-[a-z]/g, function (match) {
70 return match.substr(1).toUpperCase();
71 });
72 }
73 }))
74 .pipe(concat("partials.min.js"))
75 .pipe(gulp.dest('./dist/partials'));
76}
77```
78
79## API
80
81### ngHtml2Js(options)
82
83#### options.moduleName
84Type: `String` or `Function`
85
86The name of the generated AngularJS module. Uses the file url if omitted.
87
88When this is a function, the returned value will be the module name. The function will be passed the vinyl file object so the module name can be determined from the path, content, last access time or any other property. Returning `undefined` will fall back to the file url.
89
90#### options.declareModule
91Type: `Boolean`
92
93Whether to attempt to declare a new module (used with options.moduleName). True if omitted.
94
95Set this to false if options.moduleName is already declared.
96
97#### options.prefix
98Type: `String`
99
100The prefix which should be prepended to the file path to generate the file url.
101
102#### options.stripPrefix
103Type: `String`
104
105The prefix which should be subtracted from the file path to generate the file url.
106
107#### options.rename
108Type: `Function`
109
110A function that allows the generate file url to be manipulated. For example:
111
112``` javascript
113function (templateUrl, templateFile) {
114 return templateUrl.replace('.tpl.html', '.html');
115}
116```
117
118#### options.template
119Type: `String`
120
121A custom Lodash template for generating the Javacript code. The template is called with the following params:
122
123- moduleName: the resulting module name.
124- template
125 * url: the resulting template url.
126 * content: the HTML content of the input file.
127 * escapedContent: the escaped HTML content of the input file. Note: the HTML content is escaped for usage in a single quoted string.
128 * prettyEscapedContent: the readable, escaped HTML content of the input file.
129
130Example
131
132``` javascript
133{
134 template: "$templateCache.put('<%= template.url %>', '<%= template.escapedContent %>');"
135}
136```
137
138#### options.extension
139Type: `String`
140
141The file extension of the generated files. Defaults to .js. Can be used to generate TypeScript files and create a gulp TypeScript - job to convert them. For a working example take a look at [angular-systemjs-typescript-boilerplate](https://github.com/INSPIRATIONlabs/angular-systemjs-typescript-boilerplate)
142
143## License
144
145[MIT License](http://en.wikipedia.org/wiki/MIT_License)
146
147[npm-url]: https://npmjs.org/package/gulp-ng-html2js
148[npm-image]: https://badge.fury.io/js/gulp-ng-html2js.png
149
150[travis-url]: http://travis-ci.org/marklagendijk/gulp-ng-html2js
151[travis-image]: https://secure.travis-ci.org/marklagendijk/gulp-ng-html2js.png?branch=master
152
153[depstat-url]: https://david-dm.org/marklagendijk/gulp-ng-html2js
154[depstat-image]: https://david-dm.org/marklagendijk/gulp-ng-html2js.png