├── .gitignore ├── .npmignore ├── README.md ├── index.js ├── package.json ├── template.html └── test ├── expected.html └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | test.js 3 | example 4 | examples 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## indexhtml 2 | 3 | Generate a customized index.html file 4 | 5 | ## Install 6 | 7 | ```bash 8 | $ npm install indexhtml 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | var indexhtml = require('indexhtml') 15 | 16 | indexhtml({ 17 | title: 'hello world', 18 | js: 'bundle.js', // or [..] is ok 19 | css: ['default.css', 'pretty.css'], // or 'single.css' is ok 20 | meta: [ 21 | { name: 'viewport', content: 'width=device-width, initial-scale=1' } 22 | ], 23 | content: '

yo

' 24 | }) 25 | ``` 26 | 27 | will output: 28 | 29 | ```html 30 | 31 | 32 | 33 | 34 | 35 | hello world 36 | 37 | 38 | 39 | 40 |

yo

41 | 42 | 43 | 44 | ``` 45 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var path = require("path"); 3 | var template = fs.readFileSync(path.join(__dirname, 'template.html')); 4 | var render = require("format-text"); 5 | 6 | module.exports = generate; 7 | 8 | function generate (options) { 9 | if (typeof options.js == 'string') options.js = [options.js]; 10 | if (typeof options.css == 'string') options.css = [options.css]; 11 | if (options.meta && !Array.isArray(options.meta)) options.meta = [options.meta]; 12 | 13 | return render(template, { 14 | title: options.title || '', 15 | content: options.content || '', 16 | js: options.js ? options.js.map(script).join('\n') : '', 17 | css: options.css ? options.css.map(link).join('\n') : '', 18 | meta: options.meta ? options.meta.map(meta).join('\n') : '' 19 | }); 20 | } 21 | 22 | function script (src) { 23 | return ''; 24 | } 25 | 26 | function link (href) { 27 | return ''; 28 | } 29 | 30 | function meta (options) { 31 | var buf = ' 2 | 3 | 4 | 5 | 6 | {title} 7 | {css} 8 | 9 | 10 | {content} 11 | {js} 12 | 13 | 14 | -------------------------------------------------------------------------------- /test/expected.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | hello world 7 | 8 | 9 | 10 | 11 |

yo

12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var indexhtml = require("../"); 2 | var test = require("prova"); 3 | var fs = require("fs"); 4 | var path = require("path"); 5 | 6 | test('generating html', function (t) { 7 | t.plan(1); 8 | 9 | var expected = fs.readFileSync(path.join(__dirname, './expected.html')).toString().replace(/\n\s*/g, ''); 10 | 11 | var actual = indexhtml({ 12 | title: 'hello world', 13 | js: 'bundle.js', // or [..] is ok 14 | css: ['default.css', 'pretty.css'], // or 'single.css' is ok 15 | meta: [ 16 | { name: 'viewport', content: 'width=device-width, initial-scale=1' } 17 | ], 18 | content: '

yo

' 19 | }); 20 | 21 | t.equal(expected, actual.replace(/\n\s*/g, '')); 22 | }); 23 | --------------------------------------------------------------------------------