forked from angular-app/angular-app
-
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.
refact(grunt) upgrade to grunt 0.4.0
- Loading branch information
1 parent
7c26602
commit d30f275
Showing
11 changed files
with
275 additions
and
214 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
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,35 +1,87 @@ | ||
module.exports = function (grunt) { | ||
/* | ||
* grunt-html2js | ||
* https://github.com/karlgoldstein/grunt-html2js | ||
* | ||
* Copyright (c) 2013 Karl Goldstein | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = function(grunt) { | ||
|
||
// HTML-2-JS Templates | ||
var path = require('path'); | ||
var TPL = 'angular.module("<%= id %>", []).run(["$templateCache", function($templateCache) {\n $templateCache.put("<%= id %>",\n "<%= content %>");\n}]);\n'; | ||
var templateModule = "angular.module('templates', [<%= templates %>]);"; | ||
|
||
var escapeContent = function(content) { | ||
return content.replace(/"/g, '\\"').replace(/\r?\n/g, '" +\n "'); | ||
}; | ||
|
||
// convert Windows file separator URL path separator | ||
var normalizePath = function(p) { | ||
if ( path.sep !== '/' ) { | ||
p = p.replace(/\\/g, '/'); | ||
} | ||
return p; | ||
}; | ||
|
||
grunt.registerTask('html2js', 'Generate js version of html template.', function() { | ||
this.requiresConfig('html2js.src'); | ||
var files = grunt.file.expandFiles(grunt.config.process('html2js.src')); | ||
var base = grunt.config.process('html2js.base') || '.'; | ||
var dest = grunt.config.process('html2js.dest') || '.'; | ||
var templates = []; | ||
files.forEach(function(file) { | ||
var id = normalizePath(path.relative(base, file)); | ||
templates.push("'" + id + "'"); | ||
grunt.file.write(path.resolve(dest, id + '.js'), grunt.template.process(TPL, { | ||
id: id, | ||
content: escapeContent(grunt.file.read(file)) | ||
})); | ||
// Warn on and remove invalid source files (if nonull was set). | ||
var existsFilter = function(filepath) { | ||
|
||
if (!grunt.file.exists(filepath)) { | ||
grunt.log.warn('Source file "' + filepath + '" not found.'); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}; | ||
|
||
// compile a template to an angular module | ||
var compileTemplate = function(moduleName, filepath) { | ||
|
||
var content = escapeContent(grunt.file.read(filepath)); | ||
|
||
var module = 'angular.module("' + moduleName + | ||
'", []).run(["$templateCache", function($templateCache) ' + | ||
'{\n $templateCache.put("' + moduleName + '",\n "' + content + | ||
'");\n}]);\n'; | ||
|
||
return module; | ||
}; | ||
|
||
grunt.registerMultiTask('html2js', 'Compiles Angular-JS templates to JavaScript.', function() { | ||
|
||
var options = this.options({ | ||
base: 'src', | ||
module: 'templates-' + this.target | ||
}); | ||
|
||
// generate a separate module | ||
this.files.forEach(function(f) { | ||
|
||
var moduleNames = []; | ||
|
||
var modules = f.src.filter(existsFilter).map(function(filepath) { | ||
|
||
var moduleName = normalizePath(path.relative(options.base, filepath)); | ||
moduleNames.push("'" + moduleName + "'"); | ||
|
||
return compileTemplate(moduleName, filepath); | ||
|
||
}).join(grunt.util.normalizelf('\n')); | ||
|
||
if (moduleNames.length) { | ||
|
||
var target = f.module || options.module; | ||
|
||
var bundle = "angular.module('" + target + "', [" + | ||
moduleNames.join(', ') + "]);\n\n" + modules; | ||
grunt.file.write(f.dest, bundle); | ||
grunt.log.writeln('File "' + f.dest + '" created.'); | ||
|
||
} else { | ||
|
||
grunt.log.warn('No source templates found, not creating ' + f.dest); | ||
} | ||
}); | ||
grunt.file.write(path.resolve(dest,'templates.js'), grunt.template.process(templateModule, { | ||
templates: templates.join(', ') | ||
})); | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.