gulp-file-include
Advanced tools
Comparing version 0.2.1 to 0.3.0
35
index.js
'use strict'; | ||
var fs = require('fs'), | ||
path = require('path'), | ||
concat = require('concat-stream'), | ||
es = require('event-stream'); | ||
module.exports = function(prefix) { | ||
prefix = prefix || '@@'; | ||
module.exports = function(options) { | ||
var prefix, basepath; | ||
if (typeof options === 'object') { | ||
prefix = options.prefix || '@@'; | ||
basepath = options.basepath || '@file'; | ||
} else { | ||
prefix = options || '@@'; | ||
basepath = '@file'; | ||
} | ||
var includeRegExp = new RegExp(prefix + 'include\\(\\s*["\'](.*?)["\'](,\\s*({[\\s\\S]*?})){0,1}\\s*\\)'); | ||
@@ -19,6 +29,6 @@ | ||
var text = String(data); | ||
self.emit('data', include(file, text, includeRegExp, prefix)); | ||
self.emit('data', include(file, text, includeRegExp, prefix, basepath)); | ||
})); | ||
} else if (file.isBuffer()) { | ||
self.emit('data', include(file, String(file.contents), includeRegExp, prefix)); | ||
self.emit('data', include(file, String(file.contents), includeRegExp, prefix, basepath)); | ||
} | ||
@@ -30,10 +40,21 @@ } | ||
function include(file, text, includeRegExp, prefix) { | ||
prefix = prefix || ''; | ||
function include(file, text, includeRegExp, prefix, basepath) { | ||
var matches = includeRegExp.exec(text); | ||
switch (basepath) { | ||
case '@file': | ||
basepath = path.dirname(file.path); | ||
break; | ||
case '@root': | ||
basepath = ''; | ||
break; | ||
default: | ||
break; | ||
} | ||
basepath = path.resolve(__dirname, basepath); | ||
while (matches) { | ||
var match = matches[0], | ||
includePath = matches[1], | ||
includeContent = fs.readFileSync(includePath); | ||
includeContent = fs.readFileSync(path.resolve(basepath, includePath)); | ||
@@ -40,0 +61,0 @@ text = text.replace(match, includeContent); |
{ | ||
"name": "gulp-file-include", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "a gulp plugin for file include", | ||
@@ -33,4 +33,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"concat-stream": "^1.4.3", | ||
"event-stream": "^3.1.0" | ||
"concat-stream": "^1.4.5", | ||
"event-stream": "^3.1.2" | ||
}, | ||
@@ -37,0 +37,0 @@ "directories": { |
@@ -0,1 +1,3 @@ | ||
[![NPM](https://nodei.co/npm/gulp-file-include.png?downloads=true)](https://nodei.co/npm/gulp-file-include/) | ||
### gulp-file-include | ||
@@ -9,4 +11,35 @@ a plugin of gulp for file include | ||
### how to use | ||
### options | ||
* options - type: `string`, just as prefix, default `@@`, and basepath is default `@file` | ||
```js | ||
fileinclude('@@') | ||
``` | ||
* options - type: `object` | ||
- prefix: `string`, default `@@` | ||
- basepath: `string`, default `@file`, it could be `@root`, `@file`, `your-basepath` | ||
* options.basepath - type: `string`, it could be | ||
- '@root', include file relative to the dir where `gulp` running in | ||
- '@file', include file relative to the dir where `file` in | ||
- 'your-basepath' include file relative to the basepath you give | ||
```js | ||
fileinclude({ | ||
prefix: '@@', | ||
basepath: '@file' | ||
}) | ||
``` | ||
```js | ||
fileinclude({ | ||
prefix: '@@', | ||
basepath: '/home/' | ||
}) | ||
``` | ||
### example | ||
index.html | ||
@@ -18,3 +51,6 @@ ```html | ||
@@include('./view.html') | ||
@@include('./var.html', {"name": "haoxin", "age": 12345}) | ||
@@include('./var.html', { | ||
"name": "haoxin", | ||
"age": 12345 | ||
}) | ||
</body> | ||
@@ -42,9 +78,8 @@ </html> | ||
gulp.src(['index.html']) | ||
.pipe(fileinclude()) | ||
.pipe(gulp.dest('./result/')); | ||
.pipe(fileinclude({ | ||
prefix: '@@', | ||
basepath: '@file' | ||
})) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
gulp.task('default', function() { | ||
gulp.run('fileinclude'); | ||
}); | ||
``` | ||
@@ -64,3 +99,14 @@ | ||
### migrate from 0.2.x | ||
* juse use the options below | ||
```js | ||
{ | ||
prefix: '@@', // or something else | ||
basepath: '@root' | ||
} | ||
``` | ||
### License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4398
59
109
Updatedconcat-stream@^1.4.5
Updatedevent-stream@^3.1.2