UNPKG

3.99 kBMarkdownView Raw
1# gulp-wrap
2
3[![NPM version](https://img.shields.io/npm/v/gulp-wrap.svg?style=flat)](https://www.npmjs.com/package/gulp-wrap)
4[![Build Status](https://secure.travis-ci.org/adamayres/gulp-wrap.svg?branch=master)](http://travis-ci.org/adamayres/gulp-wrap)
5[![Coverage Status](https://img.shields.io/coveralls/adamayres/gulp-wrap.svg?style=flat)](https://coveralls.io/r/adamayres/gulp-wrap)
6[![Dependency Status](https://img.shields.io/david/adamayres/gulp-wrap.svg?style=flat&label=deps)](https://david-dm.org/adamayres/gulp-wrap)
7[![devDependency Status](https://img.shields.io/david/dev/adamayres/gulp-wrap.svg?style=flat&label=devDeps)](https://david-dm.org/adamayres/gulp-wrap#info=devDependencies)
8
9> A [gulp](https://github.com/gulpjs/gulp) plugin to wrap the stream contents with a [lodash](http://lodash.com/docs#template) template.
10
11## Usage
12
13First, install `gulp-wrap` as a development dependency:
14
15```shell
16npm install --save-dev gulp-wrap
17```
18
19Then, add it to your `gulpfile.js`:
20
21**Wrap the contents with an inline template:**
22
23```javascript
24var wrap = require("gulp-wrap");
25
26gulp.src("./src/*.json")
27 .pipe(wrap('angular.module(\'text\', []).value(<%= contents %>);'))
28 .pipe(gulp.dest("./dist"));
29```
30
31**Wrap the contents with a template from file:**
32
33```javascript
34var wrap = require("gulp-wrap");
35
36gulp.src("./src/*.json")
37 .pipe(wrap({ src: 'path/to/template.txt'}))
38 .pipe(gulp.dest("./dist"));
39```
40
41**Use parsed contents within a template (supports JSON and YAML):**
42
43```javascript
44var wrap = require("gulp-wrap");
45
46gulp.src("./src/*.json")
47 .pipe(wrap('Hello, <%= contents.title %>, have a <%= contents.adjective %> day.'))
48 .pipe(gulp.dest("./dist"));
49```
50
51**Provide additional data and options for template processing:**
52
53```javascript
54var wrap = require("gulp-wrap");
55
56gulp.src("./src/*.json")
57 .pipe(wrap('BEFORE <%= data.contents %> <%= data.someVar %> AFTER', { someVar: 'someVal'}, { variable: 'data' }))
58 .pipe(gulp.dest("./dist"));
59```
60
61This gulp plugin wraps the stream contents in a template. If you want the stream contents to be the templates use the [gulp-template](https://github.com/sindresorhus/gulp-template) plugin.
62
63## Template
64
65The stream contents will be available in the template using the `contents` key. If the file extension is `json`, `yaml`, or `yml` then the contents will be parsed before being passed to the template. Properties from the vinyl file will be available in the template under the `file` object and are local to that stream. User supplied `data` values will always take precedence over namespace clashes with the file properties.
66
67## API
68
69### wrap(template\[,data\]\[,options\])
70
71#### template
72Type: `String` or `Object` or `Function`
73
74The template to used. When a `String` then it will be used as the template. When an `Object` then the template will be loaded from file. When a `Function` then the function will be called and should return the template content. This function get the `data` object as first parameter.
75
76#### template.src
77Type: `String`
78
79The file location of the template.
80
81#### data
82Type: `Object` or `Function`
83
84The data object that is passed on to the [lodash](http://lodash.com/docs#template) template call. When a `Function` then the function will be called and should return the `Object` data used in the template.
85
86#### options
87Type: `Object` or `Function`
88
89The options object that is passed on to the [lodash](http://lodash.com/docs#template) template call. When a `Function` then the function will be called and should return the `Object` used as the options.
90
91#### options.parse
92Type: `Boolean`
93
94Set to explicit `false` value to disable automatic JSON and YAML parsing.
95
96#### options.engine
97Type: `String`
98
99Set the [consolidate template engine](https://www.npmjs.com/package/consolidate) to use. (default to `lodash`).
100Using another engine that `lodash` may require installation of additional node package.
101
102## License
103
104[MIT License](http://en.wikipedia.org/wiki/MIT_License)