-
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.
- Loading branch information
unknown
authored and
unknown
committed
Nov 16, 2015
1 parent
e5ef747
commit 4146a89
Showing
2 changed files
with
82 additions
and
4 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "grunt-template-helper", | ||
"description": "Grunt template processor and wrapper.", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/ciddan/grunt-template-helper", | ||
"author": { | ||
"name": "Mikael Selander", | ||
|
@@ -17,13 +17,13 @@ | |
}, | ||
"main": "Gruntfile.js", | ||
"engines": { | ||
"node": "~0.10.0" | ||
"node": "^4.0.0" | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"dependencies": { | ||
"prettydiff": "1.14.7" | ||
"prettydiff": "1.15.16" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.2", | ||
|
@@ -33,7 +33,7 @@ | |
"chai": "~3.4.0", | ||
"coffee-script": "~1.10.0" | ||
}, | ||
"_id": "[email protected].0", | ||
"_id": "[email protected].1", | ||
"_from": "grunt-template-helper0.1.0", | ||
"keywords": [ | ||
"gruntplugin", | ||
|
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,78 @@ | ||
'use strict' | ||
|
||
module.exports = (grunt) -> | ||
minifier = require 'prettydiff' | ||
|
||
grunt.registerMultiTask 'template', 'Process grunt template files', () -> | ||
options = @options() | ||
|
||
@.files.forEach((f) -> | ||
grunt.log.writeln "src: #{f.src}, dest: #{f.dest}" | ||
|
||
out = f.src.filter((filepath) -> | ||
grunt.file.exists filepath | ||
).map((filepath) -> | ||
compileTemplate filepath, options, f.dest | ||
).join grunt.util.normalizelf grunt.util.linefeed | ||
|
||
if out.length < 1 | ||
grunt.log.warn 'Nothing written because compiled files were empty.' | ||
else | ||
out = minify out, options if options.minify? | ||
grunt.file.write f.dest, out | ||
grunt.log.writeln "File #{f.dest.cyan} created." | ||
) | ||
|
||
minify = (src, options) -> | ||
options.minify.pretty ?= | ||
mode: 'minify' | ||
lang: 'markup' | ||
html: 'html-yes' | ||
|
||
options.minify.pretty.source = src | ||
|
||
try | ||
minified = minifier.api(options.minify.pretty)[0] | ||
catch e | ||
grunt.log.error e | ||
grunt.fail.warn 'Oh noes! Template minification failed :(' | ||
|
||
minified | ||
|
||
|
||
compileTemplate = (filepath, options, dest) -> | ||
src = grunt.file.read filepath | ||
|
||
try | ||
wrap = options.wrap?.banner? and options.wrap?.footer? | ||
inject = wrap and options.wrap.inject?.length >= 1 | ||
|
||
processed = grunt.template.process src, options | ||
|
||
if wrap | ||
processed = "#{options.wrap.banner}#{processed}#{options.wrap.footer}" | ||
|
||
if wrap and inject | ||
for inj, pos in options.wrap.inject | ||
inj.rem ?= '' | ||
inj.repl ?= {} | ||
|
||
switch inj.prop | ||
when 'src' | ||
inject = filepath.replace inj.rem, '' | ||
when 'dest' | ||
inject = dest.replace inj.rem, '' | ||
else | ||
inject = '#{' + pos + '}' | ||
grunt.log.warn( | ||
"Invalid inject property supplied, not | ||
replacing positional marker" | ||
) | ||
|
||
inject = inject.replace k, v for k, v of inj.repl | ||
processed = processed.replace('#{' + pos + '}', inject) | ||
|
||
return processed | ||
catch e | ||
grunt.log.error e | ||
grunt.fail.warn 'Template copilation failed.' |