├── fixtures ├── a.hbs ├── b.hbs ├── c.hbs ├── multiple │ ├── button.hbs │ └── page.hbs └── engines │ ├── a.hbs │ ├── b.hbs │ └── c.hbs ├── .travis.yml ├── .gitattributes ├── .editorconfig ├── .gitignore ├── utils.js ├── gulpfile.js ├── LICENSE ├── .verb.md ├── package.json ├── index.js ├── .eslintrc.json ├── README.md └── test.js /fixtures/a.hbs: -------------------------------------------------------------------------------- 1 |

{{title}}

2 |

<%= title() %>

-------------------------------------------------------------------------------- /fixtures/b.hbs: -------------------------------------------------------------------------------- 1 |

{{title}}

2 |

<%= title() %>

-------------------------------------------------------------------------------- /fixtures/c.hbs: -------------------------------------------------------------------------------- 1 |

{{title}}

2 |

<%= title() %>

-------------------------------------------------------------------------------- /fixtures/multiple/button.hbs: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /fixtures/engines/a.hbs: -------------------------------------------------------------------------------- 1 |

{{title}}

2 |

<%= title %>

-------------------------------------------------------------------------------- /fixtures/engines/b.hbs: -------------------------------------------------------------------------------- 1 |

{{title}}

2 |

<%= title %>

-------------------------------------------------------------------------------- /fixtures/engines/c.hbs: -------------------------------------------------------------------------------- 1 |

{{title}}

2 |

<%= title %>

-------------------------------------------------------------------------------- /fixtures/multiple/page.hbs: -------------------------------------------------------------------------------- 1 | {{> button }} 2 | {{> button }} 3 | {{> button }} 4 | {{> button }} 5 | {{> button }} 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4.7' 12 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var utils = require('lazy-cache')(require); 4 | var fn = require; 5 | require = utils; 6 | 7 | /** 8 | * Utils 9 | */ 10 | 11 | require('async-array-reduce', 'reduce'); 12 | require('file-is-binary', 'isBinary'); 13 | require('is-valid-app', 'isValid'); 14 | require('mixin-deep', 'merge'); 15 | require('through2', 'through'); 16 | require = fn; 17 | 18 | utils.debug = require('debug')('base:assemble:render-file'); 19 | 20 | /** 21 | * Expose `utils` 22 | */ 23 | 24 | module.exports = utils; 25 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var mocha = require('gulp-mocha'); 5 | var istanbul = require('gulp-istanbul'); 6 | var eslint = require('gulp-eslint'); 7 | 8 | var lint = ['index.js', 'utils.js', 'test.js']; 9 | 10 | gulp.task('coverage', function() { 11 | return gulp.src('index.js') 12 | .pipe(istanbul()) 13 | .pipe(istanbul.hookRequire()); 14 | }); 15 | 16 | gulp.task('mocha', ['coverage'], function() { 17 | return gulp.src('test.js') 18 | .pipe(mocha({reporter: 'spec'})) 19 | .pipe(istanbul.writeReports()) 20 | }); 21 | 22 | gulp.task('eslint', function() { 23 | return gulp.src(lint) 24 | .pipe(eslint()) 25 | }); 26 | 27 | gulp.task('default', ['mocha', 'eslint']); 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2017, Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var renderFile = require('{%= name %}'); 5 | var assemble = require('assemble'); 6 | 7 | // register as an instance plugin with assemble 8 | var app = assemble() 9 | .use(renderFile()); 10 | 11 | // then use in a vinyl pipeline 12 | app.src('*.hbs') 13 | .pipe(app.renderfile()) 14 | .pipe(app.dest('foo')); 15 | ``` 16 | 17 | ### noop engine 18 | 19 | By default, when no engine is found for a file an error is thrown. To get around this you can either define a `noop` engine, or use disable the [engineStrict option](#optionsengineStrict). 20 | 21 | A noop engine follows the same signature as any engine, but must be registered using the key: `noop`. 22 | 23 | **Example** 24 | 25 | ```js 26 | app.engine('noop', function(view, opts, next) { 27 | // do whatever you want to `view`, or nothing 28 | next(null, view); 29 | }); 30 | ``` 31 | 32 | ## Options 33 | 34 | ### options.engineStrict 35 | 36 | By default, when no engine is found for a file an error is thrown. This can be disabled with the following: 37 | 38 | ```js 39 | app.option('engineStrict', false); 40 | ``` 41 | 42 | When disabled and an engine is not found, files are just passed through. 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "assemble-render-file", 3 | "description": "Assemble plugin for rendering views in a vinyl pipeline.", 4 | "version": "1.0.3", 5 | "homepage": "https://github.com/assemble/assemble-render-file", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Jon Schlinkert (http://twitter.com/jonschlinkert)" 10 | ], 11 | "repository": "assemble/assemble-render-file", 12 | "bugs": { 13 | "url": "https://github.com/assemble/assemble-render-file/issues" 14 | }, 15 | "license": "MIT", 16 | "files": [ 17 | "index.js", 18 | "utils.js" 19 | ], 20 | "main": "index.js", 21 | "engines": { 22 | "node": ">=4.7.0" 23 | }, 24 | "scripts": { 25 | "test": "mocha" 26 | }, 27 | "dependencies": { 28 | "async-array-reduce": "^0.2.1", 29 | "debug": "^2.6.8", 30 | "file-is-binary": "^1.0.0", 31 | "is-valid-app": "^0.3.0", 32 | "lazy-cache": "^2.0.2", 33 | "mixin-deep": "^1.2.0", 34 | "plugin-error": "^0.1.2", 35 | "through2": "^2.0.3" 36 | }, 37 | "devDependencies": { 38 | "assemble-fs": "^1.0.0", 39 | "assemble-streams": "^1.0.1", 40 | "engine-base": "^0.1.3", 41 | "engine-handlebars": "^0.8.2", 42 | "gulp": "^3.9.1", 43 | "gulp-eslint": "^3.0.1", 44 | "gulp-format-md": "^0.1.12", 45 | "gulp-istanbul": "^1.1.1", 46 | "gulp-mocha": "^3.0.1", 47 | "mocha": "^3.4.1", 48 | "should": "^11.2.1", 49 | "templates": "^1.2.8" 50 | }, 51 | "keywords": [ 52 | "assemble", 53 | "assembleplugin", 54 | "boilerplate", 55 | "build", 56 | "cli", 57 | "cli-app", 58 | "command-line", 59 | "create", 60 | "dev", 61 | "development", 62 | "engine", 63 | "file", 64 | "framework", 65 | "front", 66 | "frontend", 67 | "plugin", 68 | "project", 69 | "projects", 70 | "render", 71 | "scaffold", 72 | "scaffolder", 73 | "scaffolding", 74 | "stream", 75 | "template", 76 | "templates", 77 | "view", 78 | "views", 79 | "vinyl", 80 | "webapp", 81 | "yeoman", 82 | "yo" 83 | ], 84 | "verb": { 85 | "run": true, 86 | "toc": false, 87 | "layout": "default", 88 | "tasks": [ 89 | "readme" 90 | ], 91 | "plugins": [ 92 | "gulp-format-md" 93 | ], 94 | "related": { 95 | "list": [ 96 | "assemble", 97 | "assemble-loader", 98 | "assemble-streams", 99 | "base", 100 | "verb" 101 | ] 102 | }, 103 | "reflinks": [ 104 | "verb", 105 | "verb-readme-generator" 106 | ], 107 | "lint": { 108 | "reflinks": true 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-render-file 3 | * 4 | * Copyright (c) 2015-2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var PluginError = require('plugin-error'); 11 | var utils = require('./utils'); 12 | 13 | /** 14 | * Render a vinyl file. 15 | * 16 | * ```js 17 | * app.src('*.hbs') 18 | * .pipe(app.renderFile()); 19 | * ``` 20 | * 21 | * @name .renderFile 22 | * @param {Object} `locals` Optional locals to pass to the template engine for rendering. 23 | * @return {Object} 24 | * @api public 25 | */ 26 | 27 | module.exports = function(config) { 28 | return function plugin(app) { 29 | if (!utils.isValid(app, 'assemble-render-file', ['app', 'collection'])) return; 30 | var opts = utils.merge({}, app.options, config); 31 | var debug = utils.debug; 32 | 33 | app.define('renderFile', function(engine, locals) { 34 | if (typeof engine !== 'string') { 35 | locals = engine; 36 | engine = null; 37 | } 38 | 39 | debug('renderFile: engine "%s"', engine); 40 | 41 | locals = locals || {}; 42 | var collection = {}; 43 | 44 | if (locals && !locals.isCollection) { 45 | opts = utils.merge({}, opts, locals); 46 | } else { 47 | collection = locals; 48 | locals = {}; 49 | } 50 | 51 | var View = opts.View || opts.File || collection.View || this.View; 52 | var files = []; 53 | 54 | return utils.through.obj(function(file, enc, next) { 55 | var stream = this; 56 | 57 | if (file.isNull()) { 58 | next(null, file); 59 | return; 60 | } 61 | 62 | if (utils.isBinary(file)) { 63 | next(null, file); 64 | return; 65 | } 66 | 67 | if (file.data.render === false || opts.render === false) { 68 | next(null, file); 69 | return; 70 | } 71 | 72 | if (!file.isView) file = new View(file); 73 | files.push(file); 74 | 75 | app.handleOnce('onLoad', file, function(err) { 76 | if (err) { 77 | handleError(app, err, file, files, next); 78 | return; 79 | } 80 | 81 | app.emit('_prepare', file); 82 | next(); 83 | }); 84 | 85 | }, function(cb) { 86 | var stream = this; 87 | 88 | // run `onLoad` middleware 89 | utils.reduce(files, [], function(acc, file, next) { 90 | debug('renderFile, preRender: %s', file.path); 91 | 92 | resolveEngine(app, locals, engine); 93 | 94 | if (!locals.engine && app.isFalse('engineStrict')) { 95 | stream.push(file); 96 | next(); 97 | return; 98 | } 99 | 100 | // render the file 101 | app.render(file, locals, function(err, res) { 102 | if (typeof res === 'undefined' || err) { 103 | handleError(app, err, file, files, next); 104 | return; 105 | } 106 | 107 | debug('renderFile, postRender: %s', file.relative); 108 | stream.push(res); 109 | next(); 110 | }); 111 | }, cb); 112 | }); 113 | }); 114 | 115 | return plugin; 116 | }; 117 | }; 118 | 119 | function handleError(app, err, view, files, next) { 120 | var last = files[files.length - 1]; 121 | var errOpts = {fileName: last.path, showStack: true}; 122 | if (!err || !err.message) { 123 | err = 'view cannot be rendered'; 124 | } 125 | err = new PluginError('assemble-render-file', err, errOpts); 126 | err.files = files; 127 | err.view = last; 128 | next(err); 129 | } 130 | 131 | function resolveEngine(app, locals, engine) { 132 | if (typeof engine === 'string') { 133 | locals.engine = engine; 134 | return; 135 | } 136 | if (locals.engine) { 137 | return; 138 | } 139 | // allow a `noop` engine to be defined 140 | if (app.engines['.noop']) { 141 | locals.engine = '.noop'; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # assemble-render-file [![NPM version](https://img.shields.io/npm/v/assemble-render-file.svg?style=flat)](https://www.npmjs.com/package/assemble-render-file) [![NPM monthly downloads](https://img.shields.io/npm/dm/assemble-render-file.svg?style=flat)](https://npmjs.org/package/assemble-render-file) [![NPM total downloads](https://img.shields.io/npm/dt/assemble-render-file.svg?style=flat)](https://npmjs.org/package/assemble-render-file) [![Linux Build Status](https://img.shields.io/travis/assemble/assemble-render-file.svg?style=flat&label=Travis)](https://travis-ci.org/assemble/assemble-render-file) 2 | 3 | > Assemble plugin for rendering views in a vinyl pipeline. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save assemble-render-file 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var renderFile = require('assemble-render-file'); 17 | var assemble = require('assemble'); 18 | 19 | // register as an instance plugin with assemble 20 | var app = assemble() 21 | .use(renderFile()); 22 | 23 | // then use in a vinyl pipeline 24 | app.src('*.hbs') 25 | .pipe(app.renderfile()) 26 | .pipe(app.dest('foo')); 27 | ``` 28 | 29 | ### noop engine 30 | 31 | By default, when no engine is found for a file an error is thrown. To get around this you can either define a `noop` engine, or use disable the [engineStrict option](#optionsengineStrict). 32 | 33 | A noop engine follows the same signature as any engine, but must be registered using the key: `noop`. 34 | 35 | **Example** 36 | 37 | ```js 38 | app.engine('noop', function(view, opts, next) { 39 | // do whatever you want to `view`, or nothing 40 | next(null, view); 41 | }); 42 | ``` 43 | 44 | ## Options 45 | 46 | ### options.engineStrict 47 | 48 | By default, when no engine is found for a file an error is thrown. This can be disabled with the following: 49 | 50 | ```js 51 | app.option('engineStrict', false); 52 | ``` 53 | 54 | When disabled and an engine is not found, files are just passed through. 55 | 56 | ## About 57 | 58 | ### Related projects 59 | 60 | * [assemble-loader](https://www.npmjs.com/package/assemble-loader): Assemble plugin (^0.6.0) for loading globs of views onto custom view collections. Also works with… [more](https://github.com/assemble/assemble-loader) | [homepage](https://github.com/assemble/assemble-loader "Assemble plugin (^0.6.0) for loading globs of views onto custom view collections. Also works with verb or other Templates.js based applications.") 61 | * [assemble-streams](https://www.npmjs.com/package/assemble-streams): Assemble pipeline plugin for pushing views into a vinyl stream. | [homepage](https://github.com/assemble/assemble-streams "Assemble pipeline plugin for pushing views into a vinyl stream.") 62 | * [assemble](https://www.npmjs.com/package/assemble): Get the rocks out of your socks! Assemble makes you fast at creating web projects… [more](https://github.com/assemble/assemble) | [homepage](https://github.com/assemble/assemble "Get the rocks out of your socks! Assemble makes you fast at creating web projects. Assemble is used by thousands of projects for rapid prototyping, creating themes, scaffolds, boilerplates, e-books, UI components, API documentation, blogs, building websit") 63 | * [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/node-base/base) | [homepage](https://github.com/node-base/base "base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.") 64 | * [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://github.com/verbose/verb) | [homepage](https://github.com/verbose/verb "Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used on hundreds of projects of all sizes to generate everything from API docs to readmes.") 65 | 66 | ### Contributing 67 | 68 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 69 | 70 | ### Contributors 71 | 72 | | **Commits** | **Contributor** | 73 | | --- | --- | 74 | | 65 | [jonschlinkert](https://github.com/jonschlinkert) | 75 | | 11 | [doowb](https://github.com/doowb) | 76 | 77 | ### Building docs 78 | 79 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 80 | 81 | To generate the readme, run the following command: 82 | 83 | ```sh 84 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 85 | ``` 86 | 87 | ### Running tests 88 | 89 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 90 | 91 | ```sh 92 | $ npm install && npm test 93 | ``` 94 | 95 | ### Author 96 | 97 | **Jon Schlinkert** 98 | 99 | * [github/jonschlinkert](https://github.com/jonschlinkert) 100 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 101 | 102 | ### License 103 | 104 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 105 | MIT 106 | 107 | *** 108 | 109 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 20, 2017._ -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | require('should'); 5 | var Templates = require('templates'); 6 | var afs = require('assemble-fs'); 7 | var through = require('through2'); 8 | var streams = require('assemble-streams'); 9 | var assert = require('assert'); 10 | var renderFile = require('./'); 11 | var path = require('path'); 12 | var app; 13 | 14 | var cwd = path.resolve.bind(path, __dirname, 'fixtures'); 15 | 16 | describe('app.renderFile()', function() { 17 | beforeEach(function() { 18 | app = new Templates() 19 | .use(streams()) 20 | .use(renderFile()) 21 | .use(afs()) 22 | 23 | app.engine('hbs', require('engine-handlebars')); 24 | app.engine('foo', require('engine-base')); 25 | app.engine('*', require('engine-base')); 26 | 27 | app.create('layouts', {viewType: 'layout', engine: 'hbs'}); 28 | app.create('files', {engine: '*'}); 29 | 30 | app.file('a', {content: 'this is <%= title() %>'}); 31 | app.file('b', {content: 'this is <%= title() %>'}); 32 | app.file('c', {content: 'this is <%= title() %>'}); 33 | 34 | app.layout('default', {content: 'Before\n{% body %}\nAfter'}); 35 | 36 | app.option('renameKey', function(key) { 37 | return path.basename(key, path.extname(key)); 38 | }); 39 | 40 | app.helper('title', function() { 41 | if (this.context.title) { 42 | return this.context.title; 43 | } 44 | var view = this.context.view; 45 | var title = view.title || view.data.title; 46 | if (title) { 47 | return title; 48 | } 49 | var key = view.key; 50 | var ctx = this.context[key]; 51 | if (ctx && ctx.title) return ctx.title; 52 | return key; 53 | }); 54 | }); 55 | 56 | it('should render views from src', function(cb) { 57 | app.on('_prepare', function(file) { 58 | file.prepared = true; 59 | }); 60 | 61 | var stream = app.src(cwd('*.hbs')); 62 | var files = []; 63 | 64 | stream.pipe(app.renderFile()) 65 | .on('error', cb) 66 | .on('data', function(file) { 67 | files.push(file); 68 | }) 69 | .on('end', function() { 70 | assert(files[0].prepared); 71 | cb(); 72 | }); 73 | }); 74 | 75 | it('should render views from src', function(cb) { 76 | var stream = app.src(cwd('*.hbs')); 77 | var files = []; 78 | 79 | stream.pipe(app.renderFile()) 80 | .on('error', cb) 81 | .on('data', function(file) { 82 | files.push(file); 83 | }) 84 | .on('end', function() { 85 | assert.equal(files[0].basename, 'a.hbs'); 86 | assert.equal(files[1].basename, 'b.hbs'); 87 | assert.equal(files[2].basename, 'c.hbs'); 88 | cb(); 89 | }); 90 | }); 91 | 92 | it('should set the layout to use on the plugin options', function(cb) { 93 | var view = app.files.getView('a'); 94 | var files = []; 95 | 96 | view.toStream() 97 | .pipe(app.renderFile({layout: 'default'})) 98 | .on('error', cb) 99 | .on('data', function(file) { 100 | files.push(file); 101 | }) 102 | .on('end', function() { 103 | assert.equal(files[0].content, 'Before\nthis is a\nAfter'); 104 | cb(); 105 | }); 106 | }); 107 | 108 | it('should render views with the engine that matches the file extension', function(cb) { 109 | var stream = app.src(cwd('*.hbs')); 110 | var files = []; 111 | 112 | stream.pipe(app.renderFile()) 113 | .on('error', cb) 114 | .on('data', function(file) { 115 | files.push(file); 116 | }) 117 | .on('end', function() { 118 | assert(/

a<\/h1>/.test(files[0].content)); 119 | assert(/

b<\/h1>/.test(files[1].content)); 120 | assert(/

c<\/h1>/.test(files[2].content)); 121 | cb(); 122 | }); 123 | }); 124 | 125 | it('should render views from src with the engine passed on the opts', function(cb) { 126 | var stream = app.src(cwd('*.hbs')); 127 | var files = []; 128 | 129 | stream.pipe(app.renderFile({engine: '*'})) 130 | .on('error', cb) 131 | .on('data', function(file) { 132 | files.push(file); 133 | }) 134 | .on('end', function() { 135 | assert(/

a<\/h2>/.test(files[0].content)); 136 | assert(/

b<\/h2>/.test(files[1].content)); 137 | assert(/

c<\/h2>/.test(files[2].content)); 138 | cb(); 139 | }); 140 | }); 141 | 142 | it('should use the context passed on the opts', function(cb) { 143 | var stream = app.src(cwd('*.hbs')); 144 | var files = []; 145 | 146 | stream.pipe(app.renderFile({a: {title: 'foo'}})) 147 | .on('error', cb) 148 | .on('data', function(file) { 149 | files.push(file); 150 | }) 151 | .on('end', function() { 152 | assert(/

foo<\/h1>/.test(files[0].content)); 153 | assert(/

b<\/h1>/.test(files[1].content)); 154 | assert(/

c<\/h1>/.test(files[2].content)); 155 | cb(); 156 | }); 157 | }); 158 | 159 | it('should support noop engines', function(cb) { 160 | var stream = app.src(path.join(__dirname, '.*')); 161 | var files = []; 162 | 163 | app.engine('noop', function(view, opts, next) { 164 | next(null, view); 165 | }); 166 | 167 | stream.pipe(app.renderFile()) 168 | .on('error', cb) 169 | .on('finish', cb); 170 | }); 171 | 172 | it('should pass files through when `engineStrict` is false', function(cb) { 173 | var stream = app.src(path.join(__dirname, '.*')); 174 | 175 | app.option('engineStrict', false); 176 | stream.pipe(app.renderFile()) 177 | .on('error', cb) 178 | .on('finish', cb); 179 | }); 180 | 181 | it('should render the files in a collection', function(cb) { 182 | var files = []; 183 | app.toStream('files') 184 | .pipe(app.renderFile()) 185 | .on('error', cb) 186 | .on('data', function(file) { 187 | assert(file); 188 | assert(file.path); 189 | assert(file.contents); 190 | files.push(file); 191 | }) 192 | .on('end', function() { 193 | assert(/this is a/.test(files[0].content)); 194 | assert(/this is b/.test(files[1].content)); 195 | assert(/this is c/.test(files[2].content)); 196 | assert.equal(files.length, 3); 197 | cb(); 198 | }); 199 | }); 200 | 201 | it('should handle engine errors', function(cb) { 202 | var files = []; 203 | app.create('notdefined', {engine: '*'}); 204 | app.notdefined('foo', {content: '<%= bar %>'}) 205 | app.toStream('notdefined') 206 | .pipe(app.renderFile()) 207 | .on('error', function(err) { 208 | assert.equal(typeof err, 'object'); 209 | assert.equal(err.message, 'bar is not defined'); 210 | cb(); 211 | }) 212 | .on('end', function() { 213 | cb(new Error('expected renderFile to handle the error.')); 214 | }); 215 | }); 216 | }); 217 | 218 | describe('render behavior', function() { 219 | beforeEach(function(cb) { 220 | app = new Templates() 221 | .use(streams()) 222 | .use(renderFile()) 223 | .use(afs()) 224 | 225 | var hbs = require('engine-handlebars'); 226 | hbs.Handlebars.helpers = {}; 227 | 228 | app.engine('hbs', hbs); 229 | app.engine('foo', require('engine-base')); 230 | app.create('partials', {viewType: 'partial'}); 231 | app.partial('button', {content: 'Click me!'}); 232 | 233 | app.data({title: 'foo'}); 234 | cb(); 235 | }); 236 | 237 | it('should render views with the engine specified on arguments', function(cb) { 238 | var stream = app.src(cwd('engines/*.hbs')); 239 | var files = []; 240 | 241 | stream.pipe(app.renderFile('foo')) 242 | .on('error', cb) 243 | .on('data', function(file) { 244 | files.push(file); 245 | }) 246 | .on('end', function() { 247 | assert(/

foo<\/h2>/.test(files[0].content)); 248 | assert(/

foo<\/h2>/.test(files[1].content)); 249 | assert(/

foo<\/h2>/.test(files[2].content)); 250 | cb(); 251 | }); 252 | }); 253 | 254 | it('should not render views with `render: false` defined in front-matter', function(cb) { 255 | var stream = app.src(cwd('engines/*.hbs')); 256 | var files = []; 257 | 258 | stream 259 | .pipe(through.obj(function(file, enc, next) { 260 | file.data.render = false; 261 | next(null, file); 262 | })) 263 | .pipe(app.renderFile('foo')) 264 | .on('error', cb) 265 | .on('data', function(file) { 266 | files.push(file); 267 | }) 268 | .on('end', function() { 269 | assert(/

<%= title %><\/h2>/.test(files[0].content)); 270 | assert(/

<%= title %><\/h2>/.test(files[1].content)); 271 | assert(/

<%= title %><\/h2>/.test(files[2].content)); 272 | cb(); 273 | }); 274 | }); 275 | 276 | it('should render with the same engine multiple times', function(cb) { 277 | var stream = app.src(cwd('engines/*.hbs')); 278 | var files = []; 279 | 280 | stream 281 | .pipe(app.renderFile('foo')) 282 | .pipe(app.renderFile('foo')) 283 | .pipe(app.renderFile('foo')) 284 | .on('error', cb) 285 | .on('data', function(file) { 286 | files.push(file); 287 | }) 288 | .on('end', function() { 289 | assert(/

foo<\/h2>/.test(files[0].content)); 290 | assert(/

foo<\/h2>/.test(files[1].content)); 291 | assert(/

foo<\/h2>/.test(files[2].content)); 292 | cb(); 293 | }); 294 | }); 295 | 296 | it('should render a template with multiple duplicate partials', function(cb) { 297 | var files = []; 298 | app.src(cwd('multiple/page.hbs')) 299 | .pipe(app.renderFile('hbs')) 300 | .on('error', cb) 301 | .on('data', function(file) { 302 | files.push(file); 303 | }) 304 | .on('end', function() { 305 | assert.equal(files[0].content, 'Click me!Click me!Click me!Click me!Click me!'); 306 | cb(); 307 | }); 308 | }); 309 | 310 | it('should render views with multiple calls to renderFile', function(cb) { 311 | var stream = app.src(cwd('engines/*.hbs')); 312 | var files = []; 313 | 314 | stream 315 | .pipe(app.renderFile('foo')) 316 | .pipe(app.renderFile('hbs')) 317 | .on('error', cb) 318 | .on('data', function(file) { 319 | files.push(file); 320 | }) 321 | .on('end', function() { 322 | assert(/

foo<\/h1>/.test(files[0].content)); 323 | assert(/

foo<\/h1>/.test(files[1].content)); 324 | assert(/

foo<\/h1>/.test(files[2].content)); 325 | 326 | assert(/

foo<\/h2>/.test(files[0].content)); 327 | assert(/

foo<\/h2>/.test(files[1].content)); 328 | assert(/

foo<\/h2>/.test(files[2].content)); 329 | cb(); 330 | }); 331 | }); 332 | 333 | it('should render views with multiple calls to renderFile and locals', function(cb) { 334 | var stream = app.src(cwd('engines/a.hbs')); 335 | var files = []; 336 | 337 | stream 338 | .pipe(app.renderFile('foo', {title: 'foo'})) 339 | .on('error', cb) 340 | .pipe(app.renderFile('hbs', {title: 'bar'})) 341 | .on('error', cb) 342 | .on('data', function(file) { 343 | files.push(file); 344 | }) 345 | .on('end', function() { 346 | assert(/

bar<\/h1>/.test(files[0].content)); 347 | assert(/

foo<\/h2>/.test(files[0].content)); 348 | cb(); 349 | }); 350 | }); 351 | }); 352 | 353 | --------------------------------------------------------------------------------