forked from bruderstein/gulp-html-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- <script>contents are emitted as Files
- Loading branch information
0 parents
commit 337a991
Showing
4 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
var File = require('vinyl'), | ||
cheerio = require('cheerio'), | ||
through = require('through2'), | ||
extend = require('extend'), | ||
fs = require('fs'), | ||
q = require('q'); | ||
|
||
module.exports = function(options) { | ||
var defaults = { | ||
presets: 'script', | ||
includeHtmlInOutput: false, | ||
createReadStream : fs.createReadStream | ||
}; | ||
|
||
var presets = { | ||
script : { | ||
selector: 'script:not([data-ignore=true], [data-remove=true])', | ||
getFileName: function(node) { return node.attr('src'); } | ||
}, | ||
} | ||
|
||
var selectedPresets = (options && options.presets && presets[options.presets]) || | ||
presets[defaults.presets]; | ||
|
||
|
||
options = extend({}, defaults, selectedPresets, options); | ||
|
||
|
||
var streamToBuffer = function(stream) { | ||
var buffers = []; | ||
var deferred = q.defer(); | ||
var totalLength = 0; | ||
stream.on('readable', function() { | ||
data = stream.read(); | ||
if (data !== null) { | ||
buffers.push(data); | ||
totalLength += data.length; | ||
} | ||
}); | ||
|
||
stream.on('error', function(err) { | ||
deferred.reject(err); | ||
}); | ||
|
||
stream.on('end', function() { | ||
deferred.resolve(Buffer.concat(buffers, totalLength)); | ||
}); | ||
|
||
return deferred.promise; | ||
} | ||
|
||
// Calls the callback for each matching in the contents, with an error object | ||
// and the filename. callback(err, fileName). | ||
// fileName === null signals the end of the matches | ||
var transformFile = function(contents, callback) { | ||
var $ = cheerio.load(contents.toString()); | ||
$(options.selector).each(function() { | ||
var element = $(this); | ||
var fileName = options.getFileName(element); | ||
callback(null, fileName); | ||
}); | ||
|
||
callback(null, null); | ||
} | ||
|
||
|
||
var transform = function(file, enc, callback) { | ||
var stream = this; | ||
var bufferReadPromises = []; | ||
|
||
if (file.isNull()) { | ||
// No contents - do nothing | ||
stream.push(file); | ||
callback(); | ||
} | ||
|
||
if (file.isStream()) { | ||
streamToBuffer(file.contents) | ||
.then(function(contents) { | ||
|
||
transformFile(contents, function(err, fileName) { | ||
if (fileName) { | ||
stream.push(new File({ | ||
cwd: file.cwd, | ||
base: file.base, | ||
path: file.base + fileName, | ||
contents: options.createReadStream(file.base + fileName) | ||
})); | ||
} else { | ||
if (options.includeHtmlInOutput) { | ||
stream.push(file); | ||
} | ||
callback(); | ||
} | ||
|
||
}); | ||
|
||
}, function(err) { | ||
stream.emit('error', err); | ||
}); | ||
} | ||
|
||
if (file.isBuffer()) { | ||
|
||
transformFile(file.contents, function(err, fileName) { | ||
var createdStream; | ||
if (fileName) { | ||
try { | ||
var readPromise = streamToBuffer(options.createReadStream(file.base + fileName)) | ||
.then(function(contents) { | ||
stream.push(new File({ | ||
cwd: file.cwd, | ||
base: file.base, | ||
path: file.base + fileName, | ||
contents: contents | ||
})); | ||
}, function(err) { | ||
stream.emit('error', err); | ||
}); | ||
bufferReadPromises.push(readPromise); | ||
} | ||
catch(err) { | ||
stream.emit('error', err); | ||
} | ||
|
||
} else { | ||
q.all(bufferReadPromises) | ||
.then(function() { | ||
// end of contents, no further matches for this file | ||
if (options.includeHtmlInOutput) { | ||
stream.push(file); | ||
} | ||
callback(); | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
return through.obj(transform); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "gulp-html-src", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"author": "Dave Brotherstone", | ||
"license": "MIT", | ||
"dependencies": { | ||
"vinyl": "~0.2.3", | ||
"through2": "~0.4.1", | ||
"cheerio": "~0.13.1", | ||
"extend": "~1.2.1", | ||
"q": "~1.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.18.2", | ||
"chai": "~1.9.1", | ||
"gulp-util": "~2.2.14" | ||
} | ||
} |
Oops, something went wrong.