Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ var fs = require('fs'),
gutil = require('gulp-util');

module.exports = function(options) {
var prefix, basepath, filters;
var prefix, basepath, filters, data;

if (typeof options === 'object') {
prefix = options.prefix || '@@';
basepath = options.basepath || '@file';
filters = options.filters;
data = options.data || {};
} else {
prefix = options || '@@';
basepath = '@file';
Expand All @@ -37,14 +38,14 @@ module.exports = function(options) {
text = stripCommentedIncludes(text);

try {
self.emit('data', include(file, text, includeRegExp, prefix, basepath, filters));
self.emit('data', include(file, text, includeRegExp, prefix, basepath, filters, data));
} catch (e) {
self.emit('error', new gutil.PluginError('gulp-file-include', e.message));
}
}));
} else if (file.isBuffer()) {
try {
self.emit('data', include(file, stripCommentedIncludes(String(file.contents)), includeRegExp, prefix, basepath, filters));
self.emit('data', include(file, stripCommentedIncludes(String(file.contents)), includeRegExp, prefix, basepath, filters, data));
} catch (e) {
self.emit('error', new gutil.PluginError('gulp-file-include', e.message));
}
Expand All @@ -54,9 +55,18 @@ module.exports = function(options) {
return es.through(fileInclude);
};

function include(file, text, includeRegExp, prefix, basepath, filters) {
function include(file, text, includeRegExp, prefix, basepath, filters, data) {
var matches = includeRegExp.exec(text),
data = Object.create(data || {}),
includeData;

var matches = includeRegExp.exec(text);
if (matches[3]) {
includeData = JSON.parse(matches[3]);

for (var attr in includeData) {
data[attr] = includeData[attr];
}
}

switch (basepath) {
case '@file':
Expand Down Expand Up @@ -97,9 +107,8 @@ function include(file, text, includeRegExp, prefix, basepath, filters) {

text = text.replace(match, includeContent);

if (matches[3]) {
if (Object.keys(data).length) {
// replace variables
var data = JSON.parse(matches[3]);
for (var k in data) {
text = text.replace(new RegExp(prefix + k, 'g'), data[k]);
}
Expand Down