Skip to content

Commit e1c6251

Browse files
committed
Merge pull request #46 from lucastorri/master
Recipes: gzip and inline angularjs templates
2 parents 22e1ac9 + 728ab3b commit e1c6251

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

docs/recipes/adding-gzip-task.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# GZip dist gulp task
2+
3+
1. Install [gulp-gzip](https://www.npmjs.org/package/gulp-gzip) <br>
4+
`npm install gulp-gzip --save-dev`
5+
6+
2. Add the following code to your Gulpfile:
7+
8+
```javascript
9+
var gzip = require('gulp-gzip');
10+
11+
gulp.task('gzip', ['index'], function(cb) {
12+
gulp.src('./dist/**')
13+
.pipe(gzip({
14+
append: true,
15+
level: 9
16+
}))
17+
.pipe(gulp.dest('dist'))
18+
.on('end', cb);
19+
});
20+
```
21+
22+
3. Run the new task <br>
23+
`gulp gzip`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Add AngularJS templates to cache and inline it on page
2+
3+
This recipe assumes that your templates are in `src/templates`. It does a two-step substitution in order to achieve its goal, because it needs the Javascript and HTML to be minified with existing configurations, and then it inlines the generated template cache code. It also assumes that you have a module called `MY_MODULE_NAME`.
4+
5+
6+
1. Install packages [gulp-substituter](https://www.npmjs.org/package/gulp-substituter) and [gulp-angular-templatecache](https://www.npmjs.org/package/gulp-angular-templatecache) package <br>
7+
```
8+
npm install gulp-substituter --save-dev
9+
npm install gulp-angular-templatecache --save-dev
10+
```
11+
12+
2. Add this code to your Gulpfile:
13+
14+
```javascript
15+
var fs = require('fs');
16+
var substituter = require('gulp-substituter');
17+
var templateCache = require('gulp-angular-templatecache');
18+
19+
gulp.task('templates', ['index'], function(cb) {
20+
gulp.src('./dist/templates/**/*.html')
21+
.pipe(templateCache('templates.js', {
22+
root: '/templates',
23+
module: 'MY_MODULE_NAME'
24+
}))
25+
.pipe(gulp.dest('dist/scripts'))
26+
.on('end', function() {
27+
gulp.src('./dist/index.html')
28+
.pipe(substituter({
29+
templates: function() {
30+
var templates = fs.readFileSync('dist/scripts/templates.js').toString()
31+
.replace(/<\/script>/g, '</scr" + "ipt>')
32+
.replace(/\n/g, '');
33+
return '<script type="text/javascript">' + templates + '</script>';
34+
},
35+
__start: '<',
36+
__end: '/>',
37+
__prefix: 'replace'
38+
}))
39+
.pipe(gulp.dest('dist'))
40+
.on('end', cb);
41+
});
42+
});
43+
```
44+
45+
3. Optionally, add the following to your Pho configuration <br>
46+
`defaultDependencies: ['templates']`
47+
48+
4. Add the following to your page, after loading AngularJS and your controllers <br>
49+
`<!-- substitute:templates -->`
50+
51+
5. Run the new task <br>
52+
`gulp templates`

0 commit comments

Comments
 (0)