gulp-include
Advanced tools
Comparing version 2.3.1 to 2.4.0
# Changelog | ||
#### 2.4.0 | ||
* Lots of community fixes graceously assembled and merged by [KenEucker](https://github.com/KenEucker) | ||
#### 2.3.1 | ||
@@ -4,0 +7,0 @@ * Isolated include to solve some scoping issues that happens when running multiple includes in parallel. |
515
index.js
var fs = require('fs'), | ||
path = require('path'), | ||
es = require('event-stream'), | ||
gutil = require('gulp-util'), | ||
glob = require('glob'), | ||
applySourceMap = require('vinyl-sourcemaps-apply'), | ||
stripBom = require('strip-bom'); | ||
path = require('path'), | ||
es = require('event-stream'), | ||
glob = require('glob'), | ||
PluginError = require('plugin-error'), | ||
colors = require('ansi-colors'), | ||
applySourceMap = require('vinyl-sourcemaps-apply'), | ||
stripBom = require('strip-bom'); | ||
module.exports = function (params) { | ||
params = params || {}; | ||
params = params || {}; | ||
var SourceMapGenerator = require('source-map').SourceMapGenerator; | ||
var SourceMapConsumer = require('source-map').SourceMapConsumer; | ||
var SourceMapGenerator = require('source-map').SourceMapGenerator; | ||
var SourceMapConsumer = require('source-map').SourceMapConsumer; | ||
var extensions = null, // The extension to be searched after | ||
includedFiles = [], // Keeping track of what files have been included | ||
includePaths = false, // The paths to be searched | ||
hardFail = false; // Throw error when no match | ||
var extensions = null, // The extension to be searched after | ||
globalIncludedFiles = [], // For track of what files have been included over all files | ||
includePaths = false, // The paths to be searched | ||
hardFail = false, // Throw error when no match | ||
separateInputs = false; // Process each input file separately when using `require` logic. | ||
// Check for includepaths in the params | ||
if (params.includePaths) { | ||
if (typeof params.includePaths == "string") { | ||
// Arrayify the string | ||
includePaths = [params.includePaths]; | ||
}else if (Array.isArray(params.includePaths)) { | ||
// Set this array to the includepaths | ||
includePaths = params.includePaths; | ||
} | ||
} | ||
// Check for includepaths in the params | ||
if (params.includePaths) { | ||
if (typeof params.includePaths == "string") { | ||
// Arrayify the string | ||
includePaths = [params.includePaths]; | ||
} else if (Array.isArray(params.includePaths)) { | ||
// Set this array to the includepaths | ||
includePaths = params.includePaths; | ||
} | ||
} | ||
// Toggle error reporting | ||
if (params.hardFail != undefined) { | ||
hardFail = params.hardFail; | ||
} | ||
if (params.separateInputs) { | ||
separateInputs = true; | ||
} | ||
if (params.extensions) { | ||
extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions; | ||
} | ||
// Toggle error reporting | ||
if (params.hardFail != undefined) { | ||
hardFail = params.hardFail; | ||
} | ||
function include(file, callback) { | ||
if (file.isNull()) { | ||
return callback(null, file); | ||
} | ||
if (params.extensions) { | ||
extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions; | ||
} | ||
if (file.isStream()) { | ||
throw new gutil.PluginError('gulp-include', 'stream not supported'); | ||
} | ||
function include(file, callback) { | ||
var includedFiles = separateInputs ? [] : globalIncludedFiles; | ||
if (file.isBuffer()) { | ||
var result = processInclude(String(file.contents), file.path, file.sourceMap); | ||
file.contents = new Buffer(result.content); | ||
if (file.isNull()) { | ||
return callback(null, file); | ||
} | ||
if (file.sourceMap && result.map) { | ||
if (Object.prototype.toString.call(result.map) === '[object String]') { | ||
result.map = JSON.parse(result.map); | ||
} | ||
if (file.isStream()) { | ||
throw new PluginError('gulp-include', 'stream not supported'); | ||
} | ||
// relative-ize the paths in the map | ||
result.map.file = path.relative(file.base, result.map.file); | ||
result.map.sources.forEach(function (source, q) { | ||
result.map.sources[q] = path.relative(file.base, result.map.sources[q]); | ||
}); | ||
if (file.isBuffer()) { | ||
var result = processInclude(String(file.contents), file.path, file.sourceMap, includedFiles); | ||
file.contents = new Buffer(result.content); | ||
applySourceMap(file, result.map); | ||
} | ||
} | ||
if (file.sourceMap && result.map) { | ||
if (Object.prototype.toString.call(result.map) === '[object String]') { | ||
result.map = JSON.parse(result.map); | ||
} | ||
callback(null, file); | ||
} | ||
// relative-ize the paths in the map | ||
result.map.file = path.relative(file.base, result.map.file); | ||
result.map.sources.forEach(function (source, q) { | ||
result.map.sources[q] = path.relative(file.base, result.map.sources[q]); | ||
}); | ||
function processInclude(content, filePath, sourceMap) { | ||
var matches = content.match(/^(\s+)?(\/\/|\/\*|\#|\<\!\-\-)(\s+)?=(\s+)?(include|require)(.+$)/mg); | ||
var relativeBasePath = path.dirname(filePath); | ||
applySourceMap(file, result.map); | ||
} | ||
} | ||
if (!matches) return {content: content, map: null}; | ||
callback(null, file); | ||
} | ||
// Apply sourcemaps | ||
var map = null, mapSelf, lastMappedLine, currentPos, insertedLines; | ||
if (sourceMap) { | ||
map = new SourceMapGenerator({file: unixStylePath(filePath)}); | ||
lastMappedLine = 1; | ||
currentPos = 0; | ||
insertedLines = 0; | ||
function processInclude(content, filePath, sourceMap, includedFiles) { | ||
var matches = content.match(/^(\s+)?(\/\/|\/\*|\#|\<\!\-\-)(\s+)?=(\s+)?(include|require)(.+$)/mg); | ||
var relativeBasePath = path.dirname(filePath); | ||
mapSelf = function (currentLine) { // maps current file between matches and after all matches | ||
var currentOrigLine = currentLine - insertedLines; | ||
if (!matches) return { | ||
content: content, | ||
map: null | ||
}; | ||
for (var q = (currentLine - lastMappedLine); q > 0; q--) { | ||
map.addMapping({ | ||
generated: { | ||
line: currentLine - q, | ||
column: 0 | ||
}, | ||
original: { | ||
line: currentOrigLine - q, | ||
column: 0 | ||
}, | ||
source: filePath | ||
}); | ||
} | ||
// Apply sourcemaps | ||
var map = null, | ||
mapSelf, lastMappedLine, currentPos, insertedLines; | ||
if (sourceMap) { | ||
map = new SourceMapGenerator({ | ||
file: unixStylePath(filePath) | ||
}); | ||
lastMappedLine = 1; | ||
currentPos = 0; | ||
insertedLines = 0; | ||
lastMappedLine = currentLine; | ||
}; | ||
} | ||
mapSelf = function (currentLine) { // maps current file between matches and after all matches | ||
var currentOrigLine = currentLine - insertedLines; | ||
for (var i = 0; i < matches.length; i++) { | ||
var leadingWhitespaceMatch = matches[i].match(/^\s*/); | ||
var leadingWhitespace = null; | ||
if (leadingWhitespaceMatch) { | ||
leadingWhitespace = leadingWhitespaceMatch[0].replace("\n", ""); | ||
} | ||
for (var q = (currentLine - lastMappedLine); q > 0; q--) { | ||
map.addMapping({ | ||
generated: { | ||
line: currentLine - q, | ||
column: 0 | ||
}, | ||
original: { | ||
line: currentOrigLine - q, | ||
column: 0 | ||
}, | ||
source: filePath | ||
}); | ||
} | ||
// Remove beginnings, endings and trim. | ||
var includeCommand = matches[i] | ||
.replace(/\s+/g, " ") | ||
.replace(/(\/\/|\/\*|\#|<!--)(\s+)?=(\s+)?/g, "") | ||
.replace(/(\*\/|-->)$/g, "") | ||
.replace(/['"]/g, "") | ||
.trim(); | ||
lastMappedLine = currentLine; | ||
}; | ||
} | ||
var split = includeCommand.split(" "); | ||
for (var i = 0; i < matches.length; i++) { | ||
var leadingWhitespaceMatch = matches[i].match(/^\s*/); | ||
var leadingWhitespace = null; | ||
if (leadingWhitespaceMatch) { | ||
leadingWhitespace = leadingWhitespaceMatch[0].replace("\n", ""); | ||
} | ||
var currentLine; | ||
if (sourceMap) { | ||
// get position of current match and get current line number | ||
currentPos = content.indexOf(matches[i], currentPos); | ||
currentLine = currentPos === -1 ? 0 : content.substr(0, currentPos).match(/^/mg).length; | ||
// Remove beginnings, endings and trim. | ||
var includeCommand = matches[i] | ||
.replace(/\s+/g, " ") | ||
.replace(/(\/\/|\/\*|\#|<!--)(\s+)?=(\s+)?/g, "") | ||
.replace(/(\*\/|-->)$/g, "") | ||
.replace(/['"]/g, "") | ||
.trim(); | ||
// sometimes the line matches the leading \n and sometimes it doesn't. wierd. | ||
// in case it does, increment the current line counter | ||
if (leadingWhitespaceMatch[0][0] == '\n') currentLine++; | ||
var split = includeCommand.split(" "); | ||
mapSelf(currentLine); | ||
} | ||
var currentLine; | ||
if (sourceMap) { | ||
// get position of current match and get current line number | ||
currentPos = content.indexOf(matches[i], currentPos); | ||
currentLine = currentPos === -1 ? 0 : content.substr(0, currentPos).match(/^/mg).length; | ||
// SEARCHING STARTS HERE | ||
// Split the directive and the path | ||
var includeType = split[0]; | ||
// sometimes the line matches the leading \n and sometimes it doesn't. wierd. | ||
// in case it does, increment the current line counter | ||
if (leadingWhitespaceMatch[0][0] == '\n') currentLine++; | ||
// Use glob for file searching | ||
var fileMatches = []; | ||
var includePath = ""; | ||
mapSelf(currentLine); | ||
} | ||
if (includePaths != false) { | ||
// If includepaths are set, search in those folders | ||
for (var y = 0; y < includePaths.length; y++) { | ||
includePath = includePaths[y] + "/" + split[1]; | ||
// SEARCHING STARTS HERE | ||
// Split the directive and the path | ||
var includeType = split[0]; | ||
var globResults = glob.sync(includePath, {mark: true}); | ||
fileMatches = fileMatches.concat(globResults); | ||
} | ||
}else{ | ||
// Otherwise search relatively | ||
includePath = relativeBasePath + "/" + split[1]; | ||
var globResults = glob.sync(includePath, {mark: true}); | ||
fileMatches = globResults; | ||
} | ||
// Use glob for file searching | ||
var fileMatches = []; | ||
var includePath = ""; | ||
if (fileMatches.length < 1) fileNotFoundError(includePath); | ||
if (includePaths != false) { | ||
// If includepaths are set, search in those folders | ||
for (var y = 0; y < includePaths.length; y++) { | ||
includePath = includePaths[y] + "/" + split[1]; | ||
var replaceContent = ''; | ||
for (var y = 0; y < fileMatches.length; y++) { | ||
var globbedFilePath = fileMatches[y]; | ||
var globResults = glob.sync(includePath, { | ||
mark: true | ||
}); | ||
fileMatches = fileMatches.concat(globResults); | ||
} | ||
} else { | ||
// Otherwise search relatively | ||
includePath = relativeBasePath + "/" + split[1]; | ||
var globResults = glob.sync(includePath, { | ||
mark: true | ||
}); | ||
fileMatches = globResults; | ||
} | ||
// If directive is of type "require" and file already included, skip to next. | ||
if (includeType == "require" && includedFiles.indexOf(globbedFilePath) > -1) continue; | ||
if (fileMatches.length < 1) fileNotFoundError(includePath); | ||
// If not in extensions, skip this file | ||
if (!inExtensions(globbedFilePath)) continue; | ||
var replaceContent = ''; | ||
for (var y = 0; y < fileMatches.length; y++) { | ||
var globbedFilePath = fileMatches[y]; | ||
// Get file contents and apply recursive include on result | ||
// Unicode byte order marks are stripped from the start of included files | ||
var fileContents = stripBom(fs.readFileSync(globbedFilePath)); | ||
// If directive is of type "require" and file already included, skip to next. | ||
if (includeType == "require" && includedFiles.indexOf(globbedFilePath) > -1) continue; | ||
var result = processInclude(fileContents.toString(), globbedFilePath, sourceMap); | ||
var resultContent = result.content; | ||
// If not in extensions, skip this file | ||
if (!inExtensions(globbedFilePath)) continue; | ||
if (sourceMap) { | ||
var lines = resultContent.match(/^/mg).length; //count lines in result | ||
// Get file contents and apply recursive include on result | ||
// Unicode byte order marks are stripped from the start of included files | ||
var fileContents = stripBom(fs.readFileSync(globbedFilePath)); | ||
if (result.map) { // result had a map, merge mappings | ||
if (Object.prototype.toString.call(result.map) === '[object String]') { | ||
result.map = JSON.parse(result.map); | ||
} | ||
var result = processInclude(fileContents.toString(), globbedFilePath, sourceMap, includedFiles); | ||
var resultContent = result.content; | ||
if (result.map.mappings && result.map.mappings.length > 0) { | ||
var resultMap = new SourceMapConsumer(result.map); | ||
resultMap.eachMapping(function (mapping) { | ||
if (!mapping.source) return; | ||
if (sourceMap) { | ||
var lines = resultContent.match(/^/mg).length; //count lines in result | ||
map.addMapping({ | ||
generated: { | ||
line: mapping.generatedLine + currentLine - 1, | ||
column: mapping.generatedColumn + (leadingWhitespace ? leadingWhitespace.length : 0) | ||
}, | ||
original: { | ||
line: mapping.originalLine, | ||
column: mapping.originalColumn | ||
}, | ||
source: mapping.source, | ||
name: mapping.name | ||
}); | ||
}); | ||
if (result.map) { // result had a map, merge mappings | ||
if (Object.prototype.toString.call(result.map) === '[object String]') { | ||
result.map = JSON.parse(result.map); | ||
} | ||
if (result.map.sourcesContent) { | ||
result.map.sourcesContent.forEach(function(sourceContent, i) { | ||
map.setSourceContent(result.map.sources[i], sourceContent); | ||
}); | ||
} | ||
} | ||
} else { // result was a simple file, map whole file to new location | ||
for (var q = 0; q < lines; q++) { | ||
map.addMapping({ | ||
generated: { | ||
line: currentLine + q, | ||
column: leadingWhitespace ? leadingWhitespace.length : 0 | ||
}, | ||
original: { | ||
line: q + 1, | ||
column: 0 | ||
}, | ||
source: globbedFilePath | ||
}); | ||
} | ||
if (result.map.mappings && result.map.mappings.length > 0) { | ||
var resultMap = new SourceMapConsumer(result.map); | ||
resultMap.eachMapping(function (mapping) { | ||
if (!mapping.source) return; | ||
if (sourceMap.sourcesContent) { | ||
map.setSourceContent(globbedFilePath, resultContent); | ||
} | ||
} | ||
map.addMapping({ | ||
generated: { | ||
line: mapping.generatedLine + currentLine - 1, | ||
column: mapping.generatedColumn + (leadingWhitespace ? leadingWhitespace.length : 0) | ||
}, | ||
original: { | ||
line: mapping.originalLine, | ||
column: mapping.originalColumn | ||
}, | ||
source: mapping.source, | ||
name: mapping.name | ||
}); | ||
}); | ||
// increment/set map line counters | ||
insertedLines += lines; | ||
currentLine += lines; | ||
lastMappedLine = currentLine; | ||
} | ||
if (result.map.sourcesContent) { | ||
result.map.sourcesContent.forEach(function (sourceContent, i) { | ||
map.setSourceContent(result.map.sources[i], sourceContent); | ||
}); | ||
} | ||
} | ||
} else { // result was a simple file, map whole file to new location | ||
for (var q = 0; q < lines; q++) { | ||
map.addMapping({ | ||
generated: { | ||
line: currentLine + q, | ||
column: leadingWhitespace ? leadingWhitespace.length : 0 | ||
}, | ||
original: { | ||
line: q + 1, | ||
column: 0 | ||
}, | ||
source: globbedFilePath | ||
}); | ||
} | ||
if (includedFiles.indexOf(globbedFilePath) == -1) includedFiles.push(globbedFilePath); | ||
if (sourceMap.sourcesContent) { | ||
map.setSourceContent(globbedFilePath, resultContent); | ||
} | ||
} | ||
// If the last file did not have a line break, and it is not the last file in the matched glob, | ||
// add a line break to the end | ||
if (!resultContent.trim().match(/\n$/) && y != fileMatches.length-1) { | ||
resultContent += "\n"; | ||
} | ||
// increment/set map line counters | ||
insertedLines += lines; | ||
currentLine += lines; | ||
lastMappedLine = currentLine; | ||
} | ||
if (leadingWhitespace) resultContent = addLeadingWhitespace(leadingWhitespace, resultContent); | ||
if (includedFiles.indexOf(globbedFilePath) == -1) includedFiles.push(globbedFilePath); | ||
replaceContent += resultContent; | ||
} | ||
// If the last file did not have a line break, and it is not the last file in the matched glob, | ||
// add a line break to the end | ||
if (!resultContent.trim().match(/\n$/) && y != fileMatches.length - 1) { | ||
resultContent += "\n"; | ||
} | ||
// REPLACE | ||
if (replaceContent.length) { | ||
// sometimes the line matches the leading \n and sometimes it doesn't. wierd. | ||
// in case it does, preserve that leading \n | ||
if (leadingWhitespaceMatch[0][0] === '\n') { | ||
replaceContent = '\n' + replaceContent; | ||
} | ||
if (leadingWhitespace) resultContent = addLeadingWhitespace(leadingWhitespace, resultContent); | ||
content = content.replace(matches[i], function() { return replaceContent }); | ||
insertedLines--; // adjust because the original line with comment was removed | ||
} | ||
} | ||
replaceContent += resultContent; | ||
} | ||
if (sourceMap) { | ||
currentLine = content.match(/^/mg).length + 1; | ||
// REPLACE | ||
if (replaceContent.length) { | ||
// sometimes the line matches the leading \n and sometimes it doesn't. wierd. | ||
// in case it does, preserve that leading \n | ||
if (leadingWhitespaceMatch[0][0] === '\n') { | ||
replaceContent = '\n' + replaceContent; | ||
} | ||
mapSelf(currentLine); | ||
} | ||
content = content.replace(matches[i], function () { | ||
return replaceContent | ||
}); | ||
insertedLines--; // adjust because the original line with comment was removed | ||
} | ||
} | ||
return {content: content, map: map ? map.toString() : null}; | ||
} | ||
if (sourceMap) { | ||
currentLine = content.match(/^/mg).length + 1; | ||
function unixStylePath(filePath) { | ||
return filePath.replace(/\\/g, '/'); | ||
} | ||
mapSelf(currentLine); | ||
} | ||
function addLeadingWhitespace(whitespace, string) { | ||
return string.split("\n").map(function(line) { | ||
return whitespace + line; | ||
}).join("\n"); | ||
} | ||
return { | ||
content: content, | ||
map: map ? map.toString() : null | ||
}; | ||
} | ||
function fileNotFoundError(includePath) { | ||
if (hardFail) { | ||
throw new gutil.PluginError('gulp-include', 'No files found matching ' + includePath); | ||
}else{ | ||
console.warn( | ||
gutil.colors.yellow('WARN: ') + | ||
gutil.colors.cyan('gulp-include') + | ||
' - no files found matching ' + includePath | ||
); | ||
} | ||
} | ||
function unixStylePath(filePath) { | ||
return filePath.replace(/\\/g, '/'); | ||
} | ||
function inExtensions(filePath) { | ||
if (!extensions) return true; | ||
for (var i = 0; i < extensions.length; i++) { | ||
var re = extensions[i] + "$"; | ||
if (filePath.match(re)) return true; | ||
} | ||
return false; | ||
} | ||
function addLeadingWhitespace(whitespace, string) { | ||
return string.split("\n").map(function (line) { | ||
return whitespace + line; | ||
}).join("\n"); | ||
} | ||
return es.map(include) | ||
}; | ||
function fileNotFoundError(includePath) { | ||
if (hardFail) { | ||
throw new PluginError('gulp-include', 'No files found matching ' + includePath); | ||
} else { | ||
console.warn( | ||
colors.yellow('WARN: ') + | ||
colors.cyan('gulp-include') + | ||
' - no files found matching ' + includePath | ||
); | ||
} | ||
} | ||
function inExtensions(filePath) { | ||
if (!extensions) return true; | ||
for (var i = 0; i < extensions.length; i++) { | ||
var re = extensions[i] + "$"; | ||
if (filePath.match(re)) return true; | ||
} | ||
return false; | ||
} | ||
return es.map(include) | ||
}; |
{ | ||
"name": "gulp-include", | ||
"version": "2.3.1", | ||
"description": "Makes inclusion of files a breeze. Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.", | ||
"homepage": "http://github.com/wiledal/gulp-include", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/wiledal/gulp-include.git" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"keywords": [ | ||
"gulpplugin" | ||
], | ||
"author": { | ||
"name": "Hugo Wiledal" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"gulp": "~3.3.4", | ||
"mocha": "~1.17.0", | ||
"should": "~2.1.1", | ||
"gulp-sourcemaps": "^1.1.4", | ||
"stream-assert": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"event-stream": "~3.1.0", | ||
"glob": "^5.0.12", | ||
"gulp-util": "~2.2.10", | ||
"source-map": "^0.5.1", | ||
"strip-bom": "^2.0.0", | ||
"vinyl-sourcemaps-apply": "^0.2.0" | ||
} | ||
"name": "gulp-include", | ||
"version": "2.4.0", | ||
"description": "Makes inclusion of files a breeze. Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.", | ||
"homepage": "http://github.com/wiledal/gulp-include", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/wiledal/gulp-include.git" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"keywords": [ | ||
"gulpplugin" | ||
], | ||
"author": { | ||
"name": "Hugo Wiledal" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"gulp": "^4.0.0", | ||
"gulp-sourcemaps": "^2.6.5", | ||
"mocha": "^6.0.2", | ||
"should": "^13.2.3", | ||
"stream-assert": "^2.0.3" | ||
}, | ||
"dependencies": { | ||
"ansi-colors": "^3.2.4", | ||
"event-stream": "^4.0.1", | ||
"glob": "^7.1.3", | ||
"plugin-error": "^1.0.1", | ||
"source-map": "^0.7.3", | ||
"strip-bom": "^2.0.0", | ||
"vinyl": "^2.2.0", | ||
"vinyl-sourcemaps-apply": "^0.2.1" | ||
} | ||
} |
@@ -13,4 +13,2 @@ #gulp-include [![NPM version][npm-image]][npm-url] ![Travis build][travis-image] | ||
*Warning: if you are updating from 1.x.x to 2.x.x, please read this readme to get up to date on the behavior of `gulp-include`* | ||
## Installation | ||
@@ -57,2 +55,8 @@ ```shell | ||
* If set to `false` gulp include will not fail, but display warnings in the console. | ||
- `separateInputs` (optional) | ||
* Boolean, `true` by default | ||
* Set this to `false` if you want to process each input file independent, when executing "require" logic. | ||
So, if file required several times inside one file (or inside required by it files), then dublicates will be ignored. | ||
But when another file will begin processing, all information about required files from previuos file will be discarded. | ||
@@ -59,0 +63,0 @@ #### Example options usage: |
396
test/main.js
@@ -1,10 +0,10 @@ | ||
var gutil = require("gulp-util"), | ||
should = require("should"), | ||
include = require("../index"), | ||
fs = require("fs"), | ||
vm = require("vm"), | ||
assert = require('stream-assert'), | ||
gulp = require('gulp'), | ||
sourcemaps = require('gulp-sourcemaps'), | ||
path = require("path"); | ||
var should = require("should"), | ||
include = require("../index"), | ||
fs = require("fs"), | ||
vm = require("vm"), | ||
Vinyl = require('vinyl'), | ||
assert = require('stream-assert'), | ||
gulp = require('gulp'), | ||
sourcemaps = require('gulp-sourcemaps'), | ||
path = require("path"); | ||
@@ -14,222 +14,222 @@ | ||
describe("gulp-include", function () { | ||
describe("File including", function () { | ||
it("should replace special comments with file contents", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/basic-include.js", | ||
contents: fs.readFileSync("test/fixtures/js/basic-include.js") | ||
}); | ||
describe("File including", function () { | ||
it("should replace special comments with file contents", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/basic-include.js", | ||
contents: fs.readFileSync("test/fixtures/js/basic-include.js") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/basic-include.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/basic-include.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
it("should keep whitespace when including", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/whitespace.js", | ||
contents: fs.readFileSync("test/fixtures/js/whitespace.js") | ||
}); | ||
it("should keep whitespace when including", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/whitespace.js", | ||
contents: fs.readFileSync("test/fixtures/js/whitespace.js") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/whitespace.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/whitespace.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
it("should include complex folder trees", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-trees.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-trees.js") | ||
}); | ||
it("should include complex folder trees", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-trees.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-trees.js") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/include-trees.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
}) | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/include-trees.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
}) | ||
it("should not REQUIRE a file twice", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/big-dummy-project-file.js", | ||
contents: fs.readFileSync("test/fixtures/js/big-dummy-project-file.js") | ||
}); | ||
it("should not REQUIRE a file twice", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/big-dummy-project-file.js", | ||
contents: fs.readFileSync("test/fixtures/js/big-dummy-project-file.js") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/big-dummy-project-file.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/big-dummy-project-file.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
it("should pull files recursively", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/recursive.js", | ||
contents: fs.readFileSync("test/fixtures/js/recursive.js") | ||
}); | ||
it("should pull files recursively", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/recursive.js", | ||
contents: fs.readFileSync("test/fixtures/js/recursive.js") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/recursive.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/recursive.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
it("should only include files with the set extensions, if provided", function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/options-extensions.js", | ||
contents: fs.readFileSync("test/fixtures/js/options-extensions.js") | ||
}); | ||
it("should only include files with the set extensions, if provided", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/options-extensions.js", | ||
contents: fs.readFileSync("test/fixtures/js/options-extensions.js") | ||
}); | ||
testInclude = include({ | ||
extensions: ".txt" | ||
}); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include({ | ||
extensions: ".txt" | ||
}); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/options-extensions.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/options-extensions.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
it("should work with html-comments", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/html/basic-include.html", | ||
contents: fs.readFileSync("test/fixtures/html/basic-include.html") | ||
}); | ||
it("should work with html-comments", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/html/basic-include.html", | ||
contents: fs.readFileSync("test/fixtures/html/basic-include.html") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/html/basic-include-output.html"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/html/basic-include-output.html"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
it('should support source maps', function (done) { | ||
gulp.src('test/fixtures/js/basic-include.js') | ||
.pipe(sourcemaps.init()) | ||
.pipe(include()) | ||
.pipe(assert.length(1)) | ||
.pipe(assert.first(function (d) { | ||
d.sourceMap.sources.should.have.length(3); | ||
d.sourceMap.file.should.eql('basic-include.js'); | ||
d.sourceMap.sources.should.eql(['basic-include.js', 'deep_path/b.js', 'deep_path/deeper_path/c.js']) | ||
})) | ||
.pipe(assert.end(done)); | ||
}); | ||
it('should support source maps', function (done) { | ||
gulp.src('test/fixtures/js/basic-include.js') | ||
.pipe(sourcemaps.init()) | ||
.pipe(include()) | ||
.pipe(assert.length(1)) | ||
.pipe(assert.first(function (d) { | ||
d.sourceMap.sources.should.have.length(3); | ||
d.sourceMap.file.should.eql('basic-include.js'); | ||
d.sourceMap.sources.should.eql(['basic-include.js', 'deep_path/b.js', 'deep_path/deeper_path/c.js']) | ||
})) | ||
.pipe(assert.end(done)); | ||
}); | ||
it('should strip unicode byte order marks from included files', function (done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/html/basic-include-with-unicode-BOM.html", | ||
contents: fs.readFileSync("test/fixtures/html/basic-include-with-unicode-BOM.html") | ||
}); | ||
it('should strip unicode byte order marks from included files', function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/html/basic-include-with-unicode-BOM.html", | ||
contents: fs.readFileSync("test/fixtures/html/basic-include-with-unicode-BOM.html") | ||
}); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include(); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/html/basic-include-output-with-unicode-BOM.html"), "utf8")) | ||
done(); | ||
}); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/html/basic-include-output-with-unicode-BOM.html"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
testInclude.write(file); | ||
}) | ||
it("should include from set includePaths", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-path.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-path.js") | ||
}); | ||
it("should include from set includePaths", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-path.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-path.js") | ||
}); | ||
testInclude = include({ | ||
includePaths: [ | ||
__dirname + "/fixtures/js/include-path", | ||
__dirname + "/fixtures/js/include-path2", | ||
__dirname + "/fixtures/js/include-path2/deeper2", | ||
] | ||
}); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
testInclude = include({ | ||
includePaths: [ | ||
__dirname + "/fixtures/js/include-path", | ||
__dirname + "/fixtures/js/include-path2", | ||
__dirname + "/fixtures/js/include-path2/deeper2", | ||
] | ||
}); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/include-path.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/include-path.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
it("should throw an error if no match is found with hardFail: true", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-fail.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-fail.js") | ||
}); | ||
it("should throw an error if no match is found with hardFail: true", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-fail.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-fail.js") | ||
}); | ||
testInclude = include({ | ||
hardFail: true | ||
}); | ||
testInclude.on("error", function(err) { | ||
if (err) done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
testInclude = include({ | ||
hardFail: true | ||
}); | ||
testInclude.on("error", function (err) { | ||
if (err) done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
it("should not throw an error if no match is found with hardFail: false", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-fail.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-fail.js") | ||
}); | ||
it("should not throw an error if no match is found with hardFail: false", function (done) { | ||
var file = new Vinyl({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-fail.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-fail.js") | ||
}); | ||
testInclude = include({ | ||
hardFail: false | ||
}); | ||
testInclude.on("error", function(err) { | ||
done(err); | ||
}); | ||
testInclude.on("data", function(newFile) { | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
}) | ||
testInclude = include({ | ||
hardFail: false | ||
}); | ||
testInclude.on("error", function (err) { | ||
done(err); | ||
}); | ||
testInclude.on("data", function (newFile) { | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
154014
569
136
8
+ Addedansi-colors@^3.2.4
+ Addedplugin-error@^1.0.1
+ Addedvinyl@^2.2.0
+ Addedansi-colors@1.1.03.2.4(transitive)
+ Addedansi-wrap@0.1.0(transitive)
+ Addedarr-diff@4.0.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedclone@2.1.2(transitive)
+ Addedclone-buffer@1.0.0(transitive)
+ Addedclone-stats@1.0.0(transitive)
+ Addedcloneable-readable@1.1.3(transitive)
+ Addedevent-stream@4.0.1(transitive)
+ Addedextend-shallow@3.0.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedis-extendable@1.0.1(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedmap-stream@0.0.7(transitive)
+ Addedplugin-error@1.0.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedremove-trailing-separator@1.1.0(transitive)
+ Addedreplace-ext@1.0.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsource-map@0.7.4(transitive)
+ Addedsplit@1.0.1(transitive)
+ Addedstream-combiner@0.2.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedvinyl@2.2.1(transitive)
- Removedgulp-util@~2.2.10
- Removedansi-regex@0.2.1(transitive)
- Removedansi-styles@1.1.0(transitive)
- Removedarray-find-index@1.0.2(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedcamelcase-keys@2.1.0(transitive)
- Removedchalk@0.5.1(transitive)
- Removedclone-stats@0.0.1(transitive)
- Removedcurrently-unhandled@0.4.1(transitive)
- Removeddateformat@1.0.12(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removedduplexer2@0.0.2(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedevent-stream@3.1.7(transitive)
- Removedfind-up@1.1.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedglob@5.0.15(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedgulp-util@2.2.20(transitive)
- Removedhas-ansi@0.1.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhosted-git-info@2.8.9(transitive)
- Removedindent-string@2.1.0(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-core-module@2.13.1(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedisarray@0.0.1(transitive)
- Removedload-json-file@1.1.0(transitive)
- Removedlodash._escapehtmlchar@2.4.1(transitive)
- Removedlodash._escapestringchar@2.4.1(transitive)
- Removedlodash._htmlescapes@2.4.1(transitive)
- Removedlodash._isnative@2.4.1(transitive)
- Removedlodash._objecttypes@2.4.1(transitive)
- Removedlodash._reinterpolate@2.4.1(transitive)
- Removedlodash._reunescapedhtml@2.4.1(transitive)
- Removedlodash._shimkeys@2.4.1(transitive)
- Removedlodash.defaults@2.4.1(transitive)
- Removedlodash.escape@2.4.1(transitive)
- Removedlodash.isobject@2.4.1(transitive)
- Removedlodash.keys@2.4.1(transitive)
- Removedlodash.template@2.4.1(transitive)
- Removedlodash.templatesettings@2.4.1(transitive)
- Removedlodash.values@2.4.1(transitive)
- Removedloud-rejection@1.6.0(transitive)
- Removedmap-obj@1.0.1(transitive)
- Removedmap-stream@0.1.0(transitive)
- Removedmeow@3.7.0(transitive)
- Removedminimist@0.2.41.2.8(transitive)
- Removedmultipipe@0.1.2(transitive)
- Removednormalize-package-data@2.5.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpath-exists@2.1.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpath-type@1.1.0(transitive)
- Removedpify@2.3.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedread-pkg@1.1.0(transitive)
- Removedread-pkg-up@1.0.1(transitive)
- Removedreadable-stream@1.0.341.1.14(transitive)
- Removedredent@1.0.0(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsemver@5.7.2(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedspdx-correct@3.2.0(transitive)
- Removedspdx-exceptions@2.5.0(transitive)
- Removedspdx-expression-parse@3.0.1(transitive)
- Removedspdx-license-ids@3.0.18(transitive)
- Removedsplit@0.2.10(transitive)
- Removedstream-combiner@0.0.4(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedstrip-ansi@0.3.0(transitive)
- Removedstrip-indent@1.0.1(transitive)
- Removedsupports-color@0.2.0(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedthrough2@0.5.1(transitive)
- Removedtrim-newlines@1.0.0(transitive)
- Removedvalidate-npm-package-license@3.0.4(transitive)
- Removedvinyl@0.2.3(transitive)
- Removedxtend@3.0.0(transitive)
Updatedevent-stream@^4.0.1
Updatedglob@^7.1.3
Updatedsource-map@^0.7.3