gulp-caveman
Advanced tools
Comparing version 1.0.0 to 2.0.0
75
index.js
'use strict'; | ||
var fs = require('fs'), | ||
path = require('path'); | ||
var path = require('path'); | ||
var Caveman = require('caveman'), | ||
glob = require('glob'), | ||
Concat = require('concat-with-sourcemaps'), | ||
gutil = require('gulp-util'), | ||
through = require('through2'); | ||
module.exports = function (templates) { | ||
var File = gutil.File; | ||
module.exports = function () { | ||
var concatenated, lastFile, latestMod; | ||
return through.obj(function (file, enc, cb) { | ||
@@ -23,5 +26,15 @@ if (file.isNull()) { | ||
if (!latestMod || file.stat && file.stat.mtime > latestMod) { | ||
lastFile = file; | ||
latestMod = file.stat && file.stat.mtime; | ||
} | ||
var filename = path.basename(file.path, '.html'); | ||
if (!concatenated) { | ||
concatenated = new Concat(false, filename, gutil.linefeed); | ||
} | ||
try { | ||
file.contents = compileTemplates(file.contents, templates, cb); | ||
this.push(file); | ||
concatenated.add(file.relative, compileTemplate(filename, file.contents.toString(), cb)); | ||
} catch (err) { | ||
@@ -32,40 +45,28 @@ this.emit('error', new gutil.PluginError('gulp-caveman', err, {fileName: file.path})); | ||
cb(); | ||
}); | ||
}; | ||
}, function (cb) { | ||
if (!lastFile || !concatenated) { | ||
cb(); | ||
return; | ||
} | ||
function compileTemplates (contents, templates, cb) { | ||
if (!templates) { | ||
return contents; | ||
} | ||
var file = new File(lastFile); | ||
if (typeof templates === 'string') { | ||
templates = [templates]; | ||
} | ||
file.contents = concatenated.content; | ||
var templateCount = 0; | ||
var compiled = templates.map(function (pattern) { | ||
var templateFiles = glob.sync(pattern); | ||
templateCount = templateCount + templateFiles.length; | ||
this.push(file); | ||
cb(); | ||
}); | ||
}; | ||
return templateFiles.reduce(function (previous, current) { | ||
var name = path.basename(current, path.extname(current)), | ||
template = ''; | ||
function compileTemplate (fileName, fileContents, cb) { | ||
var template = ''; | ||
try { | ||
let compiled = Caveman.compile(fs.readFileSync(current).toString()); | ||
template = `Caveman.register("${name}", function(Caveman, d) { ${compiled} });\n`; | ||
} catch (err) { | ||
cb(new gutil.PluginError('gulp-caveman', 'Error compiling Caveman template ' + name)); | ||
} | ||
return previous + template; | ||
}, ''); | ||
}); | ||
if (templateCount) { | ||
let templateMsg = templateCount === 1 ? 'template saved.' : 'templates saved.' | ||
gutil.log(templateCount, templateMsg); | ||
try { | ||
let compiled = Caveman.compile(fileContents); | ||
template = `Caveman.register("${fileName}", function(Caveman, d) { ${compiled} });`; | ||
} catch (err) { | ||
cb(new gutil.PluginError('gulp-caveman', 'Error compiling Caveman template ' + fileName)); | ||
} | ||
return new Buffer(compiled.join('') + contents.toString()); | ||
return new Buffer(template); | ||
} |
{ | ||
"name": "gulp-caveman", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Compile Caveman templates on the server-side using Gulp.", | ||
@@ -40,3 +40,3 @@ "license": "MIT", | ||
"caveman": "^0.1.4", | ||
"glob": "^6.0.4", | ||
"concat-with-sourcemaps": "^1.0.4", | ||
"gulp-util": "^3.0.7", | ||
@@ -43,0 +43,0 @@ "through2": "^2.0.0" |
@@ -20,4 +20,4 @@ # gulp-caveman [![Build Status](https://travis-ci.org/webfella/gulp-caveman.svg?branch=master)](https://travis-ci.org/webfella/gulp-caveman) | ||
gulp.task('default', function () { | ||
return gulp.src('src/app.js') | ||
.pipe(gulpCaveman('src/templates/*.html')) | ||
return gulp.src('src/templates/*.html') | ||
.pipe(gulpCaveman()) | ||
.pipe(gulp.dest('public')); | ||
@@ -27,18 +27,4 @@ }); | ||
## API | ||
### gulpCaveman(templates) | ||
#### templates | ||
Type: `String` or `Array` | ||
Default: `''` | ||
An array or string to the locations of your templates | ||
```js | ||
['path/to/first/template.html', 'path/to/second/*.html'] | ||
``` | ||
## 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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
4380
53
29
+ Addedconcat-with-sourcemaps@1.1.0(transitive)
+ Addedsource-map@0.6.1(transitive)
- Removedglob@^6.0.4
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedglob@6.0.4(transitive)
- Removedinflight@1.0.6(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedwrappy@1.0.2(transitive)