Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.*.swp
*~
26 changes: 26 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"bitwise": true,
"curly": true,
"eqeqeq": true,
"esnext": true,
"forin": true,
"freeze": true,
"immed": true,
"indent": 2,
"latedef": true,
"maxlen": 120,
"newcap": true,
"noarg": true,
"noempty": true,
"nonbsp": true,
"nonew": true,
"plusplus": true,
"quotmark": "single",
"undef": true,
"unused": true,
"strict": true,
"browser": true,
"node": true,
"phantom": true,
"jasmine": true
}
67 changes: 4 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# npm-html2js [![Build Status](https://travis-ci.org/arnauddri/npm-html2js.svg?branch=master)](https://travis-ci.org/arnauddri/npm-html2js)
# npm-html2js

Use npm as a build tool to load all your jade/html templates into your $templateCache.

```javascript
$ npm-html2js -i 'src/**/*.jade' -o 'dist/template.js'
```shell
$ npm-html2js -i 'src/**/*.jade'

angular.module('template.js', []);
.run(['$templateCache', function($templateCache) {
.$templateCache.put('files/file1.tpl.jade',
Expand All @@ -25,63 +26,3 @@ angular.module('template.js', []);
}]);
```

### Install

Via npm:
```shell
npm install --save-dev npm-html2js
```

And load it in your build process in your ```package.json```:
```json
"scripts": {
"build": "npm-html2js -i 'files/**/*.html' -o 'dist/template.js'"
},
```


### Options

##### Input

Path to your templates. The module supports globbing so you can use path like ```src/**/*.tpl.html```

**example:**
```shell
npm-html2js -i 'files/**/*.tpl.html'
```

##### output

Path to the expected output file.

**example:**
```shell
npm-html2js ... -o 'dist/template.js'
```

##### module

The name of the parent Angular module for each set of templates. Defaults to the task target prefixed by templates.js

**example:**
```shell
npm-html2js ... -m 'myModule'
```

##### jade

if the filename ends with ```.jade```, the task will automatically render file's content using Jade then comile into JS

##### base

The prefix relative to the project directory that should be stripped from each template path to produce a module identifier for the template. For example, a template located at ```src/projects/projects.tpl.html``` would be identified as just ```projects/projects.tpl.html```.

**example:**
```shell
npm-html2js ... -b 'src'
```

##### help

Display the command line options
64 changes: 10 additions & 54 deletions bin/npm-html2js.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,17 @@
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');

var html2js = require('./../src/index.js');

var args = process.argv.slice(2);

'use strict';

var templateModule = fs.readFileSync(path.join(__dirname, './../tmpl/templateModule.tmpl'), 'utf-8');
var templateCache = fs.readFileSync(path.join(__dirname, './../tmpl/templateCache.tmpl'), 'utf-8');
var usage = fs.readFileSync(path.join(__dirname, './../tmpl/usage.md')).toString()
var fs = require('fs');
var path = require('path');
var html2js = require('./../src/index.js');

var opts = {};
opts.extension = 'html';
opts.tplPath = '**/*.tpl.'
opts.moduleName = 'app.template'
var argv = require('minimist')(process.argv.slice(2));

var arg;
while (args.length) {
arg = args.shift();
switch (arg) {
case '-h':
case '--help':
console.error(usage);
process.exit(0);
break;
case '-i':
case '--input':
opts.tplPath = args.shift()
break;
case '-o':
case '--output':
opts.filename = args.shift()
break;
case '-e':
case '--exclude':
opts.exclude = args.shift()
break;
case '-m':
case '--module':
opts.moduleName = args.shift()
break;
case '-b':
case '--base':
opts.basePath = args.shift()
break;
case '-q':
case '--quotes':
opts.quotes = true
break;
default:
break;
}
if (argv.help) {
var usage = fs.readFileSync(path.join(__dirname, './../tmpl/usage.md')).toString();
console.error(usage);
process.exit(0);
}

opts.output = (opts.filename) ? path.join(process.cwd(), opts.filename) : null

opts.tplPath = (opts.tplPath === '**/*.tpl.') ? opts.tplPath + opts.extension : opts.tplPath;

html2js(opts)
html2js(argv);
2 changes: 2 additions & 0 deletions example/files/subfolder/excluded.tpl.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div
h1 This file is excluded !
8 changes: 4 additions & 4 deletions example/output/template.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
angular.module('template.js', []);
.run(['$templateCache', function($templateCache) {
.$templateCache.put('files/file1.tpl.jade',
.$templateCache.put('/foo/files/file1.tpl.jade',
'\n' +
'<div>\n' +
' <h1>Hello World from file1!</h1>\n' +
'</div>')
.$templateCache.put('files/file2.tpl.jade',
.$templateCache.put('/foo/files/file2.tpl.jade',
'\n' +
'<div>\n' +
' <h1>Hello World from file2!</h1>\n' +
'</div>')
.$templateCache.put('files/subfolder/subfile1.tpl.jade',
.$templateCache.put('/foo/files/subfolder/subfile1.tpl.jade',
'\n' +
'<div>\n' +
' <h1>Hello World from subfile1!</h1>\n' +
'</div>')
.$templateCache.put('files/subfolder/subfile2.tpl.jade',
.$templateCache.put('/foo/files/subfolder/subfile2.tpl.jade',
'\n' +
'<div>\n' +
' <h1>Hello World from subfile2!</h1>\n' +
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
},
"keywords": "html2js, grunt-html2js, karma-html2js, jade, template, $templateCache, angular, workflow, npm",
"dependencies": {
"glob": "^4.3.2",
"jade": "^1.8.2",
"combined-stream": "0.0.7",
"async": "^0.9.0",
"through2": "^0.6.3",
"chai": "^1.10.0"
"async": "^0.9.2",
"chai": "^2.3.0",
"combined-stream": "1.0.3",
"glob": "^5.0.6",
"jade": "^1.9.2",
"lodash": "^3.9.3",
"minimist": "^1.1.1",
"through2": "^0.6.5"
},
"devDependencies": {
"mocha": "^2.1.0"
"mocha": "^2.2.5"
},
"scripts": {
"test": "mocha test/**.spec.js"
Expand Down
124 changes: 46 additions & 78 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,50 @@
var CombinedStream = require('combined-stream')
var async = require('async');
var fs = require('fs');
var glob = require('glob')
var jade = require('jade');
var path = require('path');
var through = require('through2');
var util = require('util');
#!/usr/bin/env node

var templateModule = fs.readFileSync(path.join(__dirname, './../tmpl/templateModule.tmpl'), 'utf-8');
var templateCache = fs.readFileSync(path.join(__dirname, './../tmpl/templateCache.tmpl'), 'utf-8');

module.exports = function(opts, callback) {
opts = opts || {}


var filename = 'template.js'

var tplPath = opts.tplPath || path.join(process.cwd(), '**/*.tpl.html')
var output = opts.output || path.join(process.cwd(), filename);
var moduleName = opts.moduleName || 'app.template'
var basePath = opts.basePath;
var quotes = opts.quotes;
var exclude = opts.exclude || '';

glob(tplPath, function (err, files) {
if (err)
throw new Error(err)

var cs = CombinedStream.create()
var tpl = [];

async.eachSeries(
files,
function (file, done) {
if (file.indexOf(exclude))
return done();

cs.append(fs.createReadStream(path.resolve(file)))
done()
},
function (err) {
if (err)
throw err
'use strict';

var filesIndex = 0;
cs.pipe(through(function(chunk, enc, cb) {
var route = files[filesIndex]
var _ = require('lodash');
var fs = require('fs');
var jade = require('jade');
var path = require('path');
var util = require('util');

var html = chunk.toString();

if (basePath)
route = route.replace(basePath + '/', '');
if (route.indexOf('.jade') !== -1)
html = jade.render(html, { pretty: true });

html = html.replace(/\\/g, '\\\\');
html = html.replace(/'/g, '\\\'');
html = html.replace(/\r?\n/g, '\\n\' +\n \'');

//if (quotes)
//html = html. replace("'", '"')

var tmpl = util.format(templateCache, route, html)
tpl.push(tmpl)

filesIndex++

cb();
}))
}
)

cs.on('end', function() {
var template = util.format(templateModule, moduleName, tpl.join(''))
fs.writeFileSync(output, template, 'utf8')
var templateCache = fs.readFileSync(path.join(__dirname, './../tmpl/templateCache.tmpl'), 'utf-8');
var templateModule = fs.readFileSync(path.join(__dirname, './../tmpl/templateModule.tmpl'), 'utf-8');

if (callback)
callback();
})
})
}
module.exports = function(args, callback) {
var opts = {
'_': [],
module: 'app.templates',
prefix: '/',
output: null,
};
args = args || {};
_.merge(opts, args);

var tpl = [];
opts._.forEach(function(file) {
var fullpath = path.resolve(file);
var data = fs.readFileSync(fullpath, 'utf-8');
if (fullpath.indexOf('.jade') !== -1) {
data = jade.render(data, { pretty: true });
}

data = data.replace(/\\/g, '\\\\');
data = data.replace(/\n$/, '');
data = data.replace(/'/g, '\\\'');
data = data.replace(/\r?\n/g, '\\n\' +\n \'');

tpl.push(util.format(templateCache, opts.prefix + file, data));
});

var data = util.format(templateModule, opts.module, tpl.join(''));
if (opts.output) {
fs.writeFileSync(opts.output, data, 'utf8');
} else {
console.log(data);
}

if (callback) {
callback();
}
};
Loading