angular-tpl2js
Advanced tools
Comparing version 0.0.10 to 1.0.0
270
index.js
@@ -10,145 +10,139 @@ var async = require('async'); | ||
var self = this; | ||
var _config = {}; | ||
var self = this; | ||
var _config = {}; | ||
function readAsync(file, callback) { | ||
if (file) | ||
fs.readFile(file, 'utf8', callback); | ||
} | ||
function readAsync(file, callback) { | ||
if (file) | ||
fs.readFile(file, 'utf8', callback); | ||
} | ||
function merge(obj1, obj2) { | ||
var obj3 = {}; | ||
for (var a1 in obj1) { | ||
obj3[a1] = obj1[a1]; | ||
} | ||
for (var a2 in obj2) { | ||
obj3[a2] = obj2[a2]; | ||
} | ||
return obj3; | ||
function merge(obj1, obj2) { | ||
var obj3 = {}; | ||
for (var a1 in obj1) { | ||
obj3[a1] = obj1[a1]; | ||
} | ||
for (var a2 in obj2) { | ||
obj3[a2] = obj2[a2]; | ||
} | ||
return obj3; | ||
} | ||
function embedIncludes(template, source) { | ||
function embedIncludes(template, source) { | ||
var $ = cheerio.load(template, {decodeEntities: false}); | ||
var $ = cheerio.load(template, {decodeEntities: false}); | ||
function recurse() { | ||
$('[ng-include]').each(function (i, ele) { | ||
var src = path.dirname(source) + '/' + ($(ele).attr('ng-include') || $(ele).attr('src')).replace(/"/g, '').replace(/'/g, '').trim(); | ||
var include = fs.readFileSync(src, 'utf8'); | ||
function recurse() { | ||
$('[ng-include]').each(function (i, ele) { | ||
var src = path.dirname(source) + '/' + ($(ele).attr('ng-include') || $(ele).attr('src')).replace(/"/g, '').replace(/'/g, '').trim(); | ||
var include = fs.readFileSync(src, 'utf8'); | ||
$(ele).append(include); | ||
$(this).removeAttr('ng-include') | ||
$(ele).append(include); | ||
$(this).removeAttr('ng-include'); | ||
if ($(ele).find('[ng-include]').length > 0) { | ||
recurse(); | ||
} | ||
}); | ||
if ($(ele).find('[ng-include]').length > 0) { | ||
recurse(); | ||
} | ||
}); | ||
} | ||
recurse(); | ||
recurse(); | ||
return $.html(); | ||
} | ||
return $.html(); | ||
} | ||
self.config = { | ||
get: function () { | ||
return _config; | ||
}, | ||
set: function (config) { | ||
self.config = { | ||
get: function () { | ||
return _config; | ||
}, | ||
set: function (config) { | ||
var HTMLMinifier = { | ||
collapseWhitespace: true, | ||
removeComments: true | ||
} | ||
var HTMLMinifier = { | ||
collapseWhitespace: true, | ||
removeComments: true | ||
}; | ||
_config = config || {}; | ||
_config.HTMLMinifier = merge(HTMLMinifier, (config.HTMLMinifier || {})); | ||
_config.include = config.include || false; | ||
} | ||
_config = config || {}; | ||
_config.HTMLMinifier = merge(HTMLMinifier, (config.HTMLMinifier || {})); | ||
_config.include = config.include || false; | ||
} | ||
} | ||
self.source = { | ||
hash: function (raw, base) { | ||
var source = {contents: raw, templates: []}; | ||
var parts = source.contents.split(/(?=templateUrl)/g); | ||
self.source = { | ||
hash: function (raw, base) { | ||
var source = {contents: raw, templates: []}; | ||
var parts = source.contents.split(/(?=templateUrl)/g); | ||
parts.forEach(function (element, index) { | ||
parts.forEach(function (element, index) { | ||
var match = (element.match(/(?!,)templateUrl(.*)$/gm)); | ||
var match = (element.match(/(?!,)templateUrl(.*)$/gm)); | ||
if (match) { | ||
var templateUrl = match[0].split(':')[1].split(',')[0].replace(/"/g, '').replace(/'/g, '').trim() | ||
var relative = path.resolve(__dirname + base, '../' + templateUrl); | ||
source.templates.push(relative); | ||
} | ||
}); | ||
if (match) { | ||
var templateUrl = match[0].split(':')[1].split(',')[0].replace(/"/g, '').replace(/'/g, '').trim() | ||
var relative = path.resolve(__dirname + base, '../' + templateUrl); | ||
source.templates.push(relative); | ||
} | ||
}); | ||
return source; | ||
}, | ||
read: function (target) { | ||
var deferred = new Promise(function (resolve, reject) { | ||
fs.readFile(__dirname + target, 'utf8', function (err, data) { | ||
resolve(data); | ||
}); | ||
}); | ||
return deferred; | ||
} | ||
return source; | ||
}, | ||
read: function (target) { | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(__dirname + target, 'utf8', function (err, data) { | ||
resolve(data); | ||
}); | ||
}); | ||
} | ||
}; | ||
self.templates = { | ||
get: function (source) { | ||
self.templates = { | ||
get: function (source) { | ||
var deferred = new Promise(function (resolve, reject) { | ||
async.map(source.templates, readAsync, function (err, results) { | ||
return new Promise(function (resolve, reject) { | ||
async.map(source.templates, readAsync, function (err, results) { | ||
if (err) | ||
reject('template not found: ' + err.path); | ||
else { | ||
results.forEach(function (element, index, arr) { | ||
if (err) | ||
reject('template not found: ' + err.path); | ||
else { | ||
results.forEach(function (element, index, arr) { | ||
var t, $ = cheerio.load(element, {decodeEntities: false}); | ||
var t, $ = cheerio.load(element, {decodeEntities: false}); | ||
if ($($.html()).find('[ng-include]').length > 0 && _config.include) { | ||
t = embedIncludes($.html(), source.templates[index]) | ||
} | ||
if ($($.html()).find('[ng-include]').length > 0 && _config.include) { | ||
t = embedIncludes($.html(), source.templates[index]) | ||
} | ||
var template = minify((t || $.html()), _config.HTMLMinifier) // minify the markup | ||
source.templates[index] = template | ||
resolve(source); | ||
}); | ||
resolve(source); | ||
} | ||
}); | ||
var template = minify((t || $.html()), _config.HTMLMinifier) // minify the markup | ||
source.templates[index] = template; | ||
resolve(source); | ||
}); | ||
return deferred; | ||
}, | ||
set: function (transformed) { | ||
var deferred = new Promise(function (resolve, reject) { | ||
resolve(source); | ||
} | ||
}); | ||
}); | ||
}, | ||
set: function (transformed) { | ||
return new Promise(function (resolve, reject) { | ||
try { | ||
var parts = transformed.contents.split(/(?=templateUrl)(?!,)/g); | ||
try { | ||
var parts = transformed.contents.split(/(?=templateUrl)(?!,)/g); | ||
if (parts.length === 1) | ||
throw 'unable to set template: no templateUrl clause'; | ||
if (parts.length === 1) | ||
throw 'unable to set template: no templateUrl clause'; | ||
parts.forEach(function (element, index, arr) { | ||
parts.forEach(function (element, index, arr) { | ||
var match = (element.match(/(?!,)templateUrl(.*)$/gm)); | ||
var match = (element.match(/(?!,)templateUrl(.*)$/gm)); | ||
if (match) | ||
arr[index] = arr[index].replace(/(?!,)templateUrl(.*)(?!,)$/gm, 'template: \'' + transformed.templates.shift().replace(/'/g, "\\'") + '\',') | ||
}); | ||
if (match) | ||
arr[index] = arr[index].replace(/(?!,)templateUrl(.*)(?!,)$/gm, 'template: \'' + transformed.templates.shift().replace(/'/g, "\\'") + '\',') | ||
}); | ||
resolve(parts.join('')); | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}); | ||
return deferred; | ||
resolve(parts.join('')); | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
@@ -158,41 +152,41 @@ | ||
var self = this; | ||
var self = this; | ||
self.inline = function (input, config, done) { // -- in | ||
self.inline = function (input, config, done) { // -- in | ||
engine = new TemplateEngine(); | ||
engine = new TemplateEngine(); | ||
// shift optional config argument | ||
if (arguments.length === 2 && Object.prototype.toString.call(arguments[1]) === '[object Function]') { | ||
done = config; | ||
} else { | ||
engine.config.set(config); | ||
} | ||
// shift optional config argument | ||
if (arguments.length === 2 && Object.prototype.toString.call(arguments[1]) === '[object Function]') { | ||
done = config; | ||
} else { | ||
engine.config.set(config); | ||
} | ||
var base, js; | ||
var base, js; | ||
function run(js) { | ||
engine.templates.get(js).then(function (transformed) { | ||
engine.templates.set(transformed).then(function (output) { | ||
done(null, output); // -- out | ||
}, function (err) { | ||
done(err) // -- templates.set promise error | ||
}); | ||
}, function (err) { | ||
done(err); // -- templates.get promise error | ||
}); | ||
} | ||
function run(js) { | ||
engine.templates.get(js).then(function (transformed) { | ||
engine.templates.set(transformed).then(function (output) { | ||
done(null, output); // -- out | ||
}, function (err) { | ||
done(err) // -- templates.set promise error | ||
}); | ||
}, function (err) { | ||
done(err); // -- templates.get promise error | ||
}); | ||
} | ||
if (input instanceof Buffer) { | ||
base = '/' + path.dirname(path.relative(__dirname, config.target)); | ||
js = engine.source.hash(input.toString(), base); | ||
run(js); | ||
} else { | ||
engine.source.read(input).then(function (data) { | ||
base = path.dirname(input); | ||
js = engine.source.hash(data, base); | ||
run(js); | ||
}); | ||
} | ||
if (input instanceof Buffer) { | ||
base = '/' + path.dirname(path.relative(__dirname, config.target)); | ||
js = engine.source.hash(input.toString(), base); | ||
run(js); | ||
} else { | ||
engine.source.read(input).then(function (data) { | ||
base = path.dirname(input); | ||
js = engine.source.hash(data, base); | ||
run(js); | ||
}); | ||
} | ||
} | ||
} | ||
@@ -199,0 +193,0 @@ |
{ | ||
"name": "angular-tpl2js", | ||
"version": "0.0.10", | ||
"version": "1.0.0", | ||
"description": "Convert Angular templates to inline JavaScript", | ||
"files": [ | ||
"index.js" | ||
], | ||
"main": "index.js", | ||
"repository": { | ||
@@ -15,19 +13,25 @@ "type": "git", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.9", | ||
"gulp": "^3.9.1", | ||
"gulp-istanbul": "^0.10.4", | ||
"gulp-mocha": "^2.2.0", | ||
"gulp-util": "^3.0.7", | ||
"mocha": "^2.4.5", | ||
"vinyl": "^1.1.1" | ||
"chai": "4.0.2", | ||
"coveralls": "2.13.1", | ||
"mocha": "3.4.2", | ||
"nyc": "11.0.3", | ||
"vinyl": "2.0.2" | ||
}, | ||
"dependencies": { | ||
"async": "^2.0.0-rc.3", | ||
"cheerio": "^0.20.0", | ||
"html-minifier": "^2.1.0" | ||
"async": "2.5.0", | ||
"cheerio": "0.22.0", | ||
"html-minifier": "3.5.2" | ||
}, | ||
"scripts": { | ||
"test": "gulp test" | ||
"test": "./node_modules/.bin/nyc mocha test/test.js --reporter dot" | ||
}, | ||
"nyc": { | ||
"report-dir": "./.coverage", | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
], | ||
"sourceMap": false, | ||
"tempDirectory": "./.coverage/.temp" | ||
} | ||
} |
@@ -23,7 +23,7 @@ # angular-tpl2js | ||
```javascript | ||
var tpl2js = require('angular-tpl2js'); | ||
let tpl2js = require('angular-tpl2js'); | ||
tpl2js.inline('/js/directive.js', function (err, result) { | ||
// directive.js with inline template | ||
// directive.js with inline template | ||
}); | ||
@@ -38,8 +38,8 @@ ``` | ||
// defaults | ||
var config = { | ||
include: false, | ||
HTMLMinifier: { | ||
collapseWhitespace: true, | ||
removeComments: true | ||
} | ||
let config = { | ||
include: false, | ||
HTMLMinifier: { | ||
collapseWhitespace: true, | ||
removeComments: true | ||
} | ||
} | ||
@@ -51,11 +51,10 @@ ``` | ||
```javascript | ||
tpl2js.inline('/js/directive.js', {include: true}, function (err, result) { | ||
// directive.js with inline template: ng-include parsed | ||
// directive.js with inline template: ng-include parsed | ||
}); | ||
``` | ||
## Use With Gulp | ||
## Usage With Gulp | ||
Please use the [gulp-angular-tpl2js plugin](https://github.com/scniro/gulp-angular-tpl2js) for use with gulp. |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5
1
9142
151
57
+ Addedasync@2.5.0(transitive)
+ Addedcheerio@0.22.0(transitive)
+ Addedclean-css@4.1.11(transitive)
+ Addedcommander@2.11.0(transitive)
+ Addeddomhandler@2.4.2(transitive)
+ Addedhtml-minifier@3.5.2(transitive)
+ Addedhtmlparser2@3.10.1(transitive)
+ Addedlodash.assignin@4.2.0(transitive)
+ Addedlodash.bind@4.2.1(transitive)
+ Addedlodash.defaults@4.2.0(transitive)
+ Addedlodash.filter@4.6.0(transitive)
+ Addedlodash.flatten@4.4.0(transitive)
+ Addedlodash.foreach@4.5.0(transitive)
+ Addedlodash.map@4.6.0(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedlodash.pick@4.4.0(transitive)
+ Addedlodash.reduce@4.6.0(transitive)
+ Addedlodash.reject@4.6.0(transitive)
+ Addedlodash.some@4.6.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addeduglify-js@3.0.28(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedabab@1.0.4(transitive)
- Removedacorn@2.7.0(transitive)
- Removedacorn-globals@1.0.9(transitive)
- Removedajv@6.12.6(transitive)
- Removedalign-text@0.1.4(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasync@0.2.102.6.4(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcamelcase@1.2.1(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcenter-align@0.1.3(transitive)
- Removedchange-case@3.0.2(transitive)
- Removedcheerio@0.20.0(transitive)
- Removedclean-css@3.4.28(transitive)
- Removedcliui@2.1.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcommander@2.8.1(transitive)
- Removedconstant-case@2.0.0(transitive)
- Removedcore-util-is@1.0.21.0.3(transitive)
- Removedcssom@0.3.8(transitive)
- Removedcssstyle@0.2.37(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removeddomhandler@2.3.0(transitive)
- Removeddot-case@2.1.1(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedentities@1.0.0(transitive)
- Removedescodegen@1.14.3(transitive)
- Removedesprima@4.0.1(transitive)
- Removedestraverse@4.3.0(transitive)
- Removedesutils@2.0.3(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedheader-case@1.0.1(transitive)
- Removedhtml-minifier@2.1.7(transitive)
- Removedhtmlparser2@3.8.3(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-lower-case@1.1.3(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedis-upper-case@1.1.2(transitive)
- Removedisarray@0.0.1(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjsdom@7.2.2(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@1.0.4(transitive)
- Removedlevn@0.3.0(transitive)
- Removedlongest@1.0.1(transitive)
- Removedlower-case-first@1.0.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removednwmatcher@1.4.4(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedoptionator@0.8.3(transitive)
- Removedparse5@1.5.1(transitive)
- Removedpascal-case@2.0.1(transitive)
- Removedpath-case@2.1.1(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedprelude-ls@1.1.2(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedreadable-stream@1.1.14(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedrequest@2.88.2(transitive)
- Removedright-align@0.1.3(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsax@1.4.1(transitive)
- Removedsentence-case@2.1.1(transitive)
- Removedsnake-case@2.1.0(transitive)
- Removedsource-map@0.4.40.6.1(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedswap-case@1.1.2(transitive)
- Removedsymbol-tree@3.2.4(transitive)
- Removedtitle-case@2.1.1(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedtype-check@0.3.2(transitive)
- Removeduglify-js@2.6.4(transitive)
- Removeduglify-to-browserify@1.0.2(transitive)
- Removedupper-case-first@1.1.2(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
- Removedwebidl-conversions@2.0.1(transitive)
- Removedwhatwg-url-compat@0.6.5(transitive)
- Removedwindow-size@0.1.0(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedwordwrap@0.0.2(transitive)
- Removedxml-name-validator@2.0.1(transitive)
- Removedyargs@3.10.0(transitive)
Updatedasync@2.5.0
Updatedcheerio@0.22.0
Updatedhtml-minifier@3.5.2