gulp-file-include
Advanced tools
Comparing version 2.2.2 to 2.3.0
@@ -35,2 +35,3 @@ 'use strict' | ||
var customWebRoot = !!opts.context.webRoot | ||
var includeOnceFiles = {}; | ||
@@ -76,3 +77,3 @@ function fileInclude(file, enc, cb) { | ||
function include(file, text, data) { | ||
function include(file, text, data, sourceFile = '') { | ||
var filebase = opts.basepath === '@file' ? path.dirname(file.path) : opts.basepath | ||
@@ -89,3 +90,4 @@ var currentFilename = path.resolve(file.base, file.path) | ||
name: 'if', | ||
handler: conditionalHandler | ||
handler: conditionalHandler, | ||
sourceFile: sourceFile | ||
}) | ||
@@ -96,3 +98,4 @@ text = replaceOperator(text, { | ||
name: 'for', | ||
handler: forHandler | ||
handler: forHandler, | ||
sourceFile: sourceFile | ||
}) | ||
@@ -103,4 +106,12 @@ text = replaceVariable(text, data, opts) | ||
suffix: opts.suffix, | ||
name: 'include_once', | ||
handler: includeOnceHandler, | ||
sourceFile: sourceFile | ||
}) | ||
text = replaceFunction(text, { | ||
prefix: opts.prefix, | ||
suffix: opts.suffix, | ||
name: 'include', | ||
handler: includeHandler | ||
handler: includeHandler, | ||
sourceFile: sourceFile | ||
}) | ||
@@ -111,3 +122,4 @@ text = replaceFunction(text, { | ||
name: 'loop', | ||
handler: loopHandler | ||
handler: loopHandler, | ||
sourceFile: sourceFile | ||
}) | ||
@@ -137,2 +149,17 @@ | ||
function includeOnceHandler(inst) { | ||
var args = /[^)"']*["']([^"']*)["'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args) | ||
if (args) { | ||
if (typeof includeOnceFiles[inst.sourceFile] === 'undefined') { | ||
includeOnceFiles[inst.sourceFile] = []; | ||
} | ||
if (includeOnceFiles[inst.sourceFile].indexOf(args[1]) === -1) { | ||
includeOnceFiles[inst.sourceFile].push(args[1]); | ||
return includeHandler(inst) | ||
} else { | ||
return ''; | ||
} | ||
} | ||
} | ||
function includeHandler(inst) { | ||
@@ -169,3 +196,3 @@ var args = /[^)"']*["']([^"']*)["'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args) | ||
recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3]) : {}) | ||
recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3]) : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename) | ||
@@ -233,3 +260,3 @@ return String(recFile.contents) | ||
var context = arr[i] | ||
recFile = include(recFile, includeContent, args[3] ? context : {}) | ||
recFile = include(recFile, includeContent, args[3] ? context : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename) | ||
// why handler dont reconize underscore? | ||
@@ -236,0 +263,0 @@ // if (typeof context == 'object' && typeof context['_key'] == 'undefined') { |
@@ -32,3 +32,4 @@ 'use strict' | ||
before: before, | ||
args: matchArg.body | ||
args: matchArg.body, | ||
sourceFile: opts.sourceFile | ||
}) | ||
@@ -35,0 +36,0 @@ |
{ | ||
"name": "gulp-file-include", | ||
"version": "2.2.2", | ||
"description": "a gulp plugin for file include", | ||
"version": "2.3.0", | ||
"description": "A gulp plugin for file include", | ||
"main": "lib/index.js", | ||
@@ -37,4 +37,4 @@ "scripts": { | ||
"plugin-error": "^1.0.1", | ||
"through2": "^3.0.1", | ||
"vinyl": "^2.2.0" | ||
"through2": "^4.0.2", | ||
"vinyl": "^2.2.1" | ||
}, | ||
@@ -46,3 +46,3 @@ "devDependencies": { | ||
"markdown": "^0.5.0", | ||
"mocha": "^7.1.1", | ||
"mocha": "^8.2.1", | ||
"should": "^13.2.3" | ||
@@ -49,0 +49,0 @@ }, |
@@ -139,2 +139,74 @@ [![NPM version][npm-img]][npm-url] | ||
### @@include_once options - type: `JSON` | ||
index.html | ||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
@@include_once('./view.html') | ||
@@include_once('./var.html', { | ||
"name": "haoxin", | ||
"age": 12345, | ||
"socials": { | ||
"fb": "facebook.com/include", | ||
"tw": "twitter.com/include" | ||
} | ||
}) | ||
@@include_once('./var.html', { | ||
"name": "haoxin", | ||
"age": 12345, | ||
"socials": { | ||
"fb": "facebook.com/include", | ||
"tw": "twitter.com/include" | ||
} | ||
}) | ||
</body> | ||
</html> | ||
``` | ||
view.html | ||
```html | ||
<h1>view</h1> | ||
``` | ||
var.html | ||
```html | ||
<label>@@name</label> | ||
<label>@@age</label> | ||
<strong>@@socials.fb</strong> | ||
<strong>@@socials.tw</strong> | ||
``` | ||
gulpfile.js | ||
```js | ||
const fileinclude = require('gulp-file-include'); | ||
const gulp = require('gulp'); | ||
gulp.task('fileinclude', function() { | ||
gulp.src(['index.html']) | ||
.pipe(fileinclude({ | ||
prefix: '@@', | ||
basepath: '@file' | ||
})) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
``` | ||
result: | ||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>view</h1> | ||
<label>haoxin</label> | ||
<label>12345</label> | ||
<strong>facebook.com/include</strong> | ||
<strong>twitter.com/include</strong> | ||
</body> | ||
</html> | ||
``` | ||
### filters | ||
@@ -141,0 +213,0 @@ |
20847
370
379
+ Addedthrough2@4.0.2(transitive)
- Removedthrough2@3.0.2(transitive)
Updatedthrough2@^4.0.2
Updatedvinyl@^2.2.1