Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-tpl2js

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-tpl2js - npm Package Compare versions

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.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc