├── .gitignore ├── .travis.yml ├── README.md ├── index.js ├── package.json └── test ├── fixtures ├── fixture1.pug ├── fixture2.pug ├── fixture3.pug ├── fixture4.pug ├── fixture5.pug └── subfolder │ └── fixture5.pug └── main.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.11" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is fork of [Juanfran's](https://github.com/juanfran) [gulp-jade-inheritance](https://github.com/juanfran/gulp-jade-inheritance) gulp-plugin migrated to new Pug parser (former Jade). 2 | 3 | --- 4 | #gulp-pug-inheritance 5 | [![Build Status](https://travis-ci.org/pure180/gulp-pug-inheritance.svg?branch=master)](https://travis-ci.org/pure180/gulp-pug-inheritance) 6 | [![Dependency Status](https://david-dm.org/pure180/gulp-jade-inheritance.svg)](https://david-dm.org/pure180/gulp-jade-inheritance) 7 | > Rebuild a jade file with other files that have extended or included those file 8 | 9 | Inspired by [jade-inheritance](https://github.com/paulyoung/jade-inheritance) 10 | 11 | ## Install 12 | 13 | ```shell 14 | npm install gulp-pug-inheritance --save-dev 15 | ``` 16 | 17 | ## Usage 18 | 19 | `gulpfile.js` 20 | ```js 21 | var pugInheritance = require('gulp-pug-inheritance'); 22 | var jade = require('gulp-pug'); 23 | 24 | gulp.task('jade-inheritance', function() { 25 | gulp.src('/jade/example.jade') 26 | .pipe(pugInheritance({basedir: '/jade/'})) 27 | .pipe(jade()); 28 | }); 29 | ``` 30 | 31 | In this example jade compile `example.jade` and all other files that have been extended or included `example.jade`. The plugin searches for those dependencies in the `basedir` directory. 32 | 33 | ### Only process changed files 34 | 35 | You can use `gulp-pug-inheritance` with `gulp-changed` and `gulp-cached` to only process the files that have changed. This also prevent partials from being processed separately by marking them with an underscore before their name. 36 | 37 | ```js 38 | 'use strict'; 39 | var gulp = require('gulp'); 40 | var pugInheritance = require('gulp-pug-inheritance'); 41 | var jade = require('gulp-jade'); 42 | var changed = require('gulp-changed'); 43 | var cached = require('gulp-cached'); 44 | var gulpif = require('gulp-if'); 45 | var filter = require('gulp-filter'); 46 | 47 | gulp.task('jade', function() { 48 | return gulp.src('app/**/*.jade') 49 | 50 | //only pass unchanged *main* files and *all* the partials 51 | .pipe(changed('dist', {extension: '.html'})) 52 | 53 | //filter out unchanged partials, but it only works when watching 54 | .pipe(gulpif(global.isWatching, cached('jade'))) 55 | 56 | //find files that depend on the files that have changed 57 | .pipe(pugInheritance({basedir: 'app'})) 58 | 59 | //filter out partials (folders and files starting with "_" ) 60 | .pipe(filter(function (file) { 61 | return !/\/_/.test(file.path) && !/^_/.test(file.relative); 62 | })) 63 | 64 | //process jade templates 65 | .pipe(jade()) 66 | 67 | //save all the files 68 | .pipe(gulp.dest('dist')); 69 | }); 70 | gulp.task('setWatch', function() { 71 | global.isWatching = true; 72 | }); 73 | gulp.task('watch', ['setWatch', 'jade'], function() { 74 | //your watch functions... 75 | }); 76 | ``` 77 | 78 | If you want to prevent partials from being processed, mark them with an underscore before their name or their parent folder's name. Example structure: 79 | 80 | ``` 81 | /app/index.jade 82 | /app/_header.jade 83 | /app/_partials/article.jade 84 | /dist/ 85 | ``` 86 | 87 | To install all that's need for it: 88 | 89 | ```shell 90 | npm install gulp-pug-inheritance gulp-pug gulp-changed gulp-cached gulp-if gulp-filter --save-dev 91 | ``` 92 | 93 | ### jade >= 1.11 94 | 95 | if your using jade 1.11 add `"jade": "^1.11.0"` to your `package.json` to overwrite the jade-inheritance version. [Issue](https://github.com/paulyoung/jade-inheritance/issues/15) 96 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var es = require('event-stream'); 4 | var _ = require("lodash"); 5 | var vfs = require('vinyl-fs'); 6 | var through2 = require('through2'); 7 | var gutil = require('gulp-util'); 8 | var PugInheritance = require('pug-inheritance'); 9 | var PLUGIN_NAME = 'gulp-pug-inheritance'; 10 | 11 | 12 | function gulpPugInheritance(options) { 13 | options = options || {}; 14 | 15 | var stream; 16 | var errors = {}; 17 | var files = []; 18 | 19 | function writeStream(currentFile) { 20 | if (currentFile && currentFile.contents.length) { 21 | files.push(currentFile); 22 | } 23 | } 24 | 25 | function endStream() { 26 | if (files.length) { 27 | var pugInheritanceFiles = []; 28 | var filesPaths = []; 29 | 30 | options = _.defaults(options, {'basedir': process.cwd()}); 31 | 32 | _.forEach(files, function(file) { 33 | try { 34 | var pugInheritance = new PugInheritance(file.path, options.basedir, options); 35 | 36 | } catch (e) { 37 | // prevent multiple errors on the same file 38 | var alreadyShown; 39 | if (errors[e.message]) { 40 | alreadyShown = true; 41 | } 42 | 43 | clearTimeout(errors[e.message]); 44 | errors[e.message] = setTimeout(function () { 45 | delete errors[e.message]; 46 | }, 500); //debounce 47 | 48 | if (alreadyShown) { 49 | return; 50 | } 51 | 52 | var err = new gutil.PluginError(PLUGIN_NAME, e); 53 | stream.emit("error", err); 54 | return; 55 | } 56 | 57 | var fullpaths = _.map(pugInheritance.files, function (file) { 58 | return options.basedir + "/" + file; 59 | }); 60 | 61 | filesPaths = _.union(filesPaths, fullpaths); 62 | }); 63 | 64 | if(filesPaths.length) { 65 | vfs.src(filesPaths, {'base': options.basedir}) 66 | .pipe(es.through( 67 | function (f) { 68 | stream.emit('data', f); 69 | }, 70 | function () { 71 | stream.emit('end'); 72 | } 73 | )); 74 | } else { 75 | stream.emit('end'); 76 | } 77 | } else { 78 | stream.emit('end'); 79 | } 80 | } 81 | 82 | stream = es.through(writeStream, endStream); 83 | 84 | return stream; 85 | }; 86 | 87 | module.exports = gulpPugInheritance; 88 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-pug-inheritance", 3 | "version": "0.2.1", 4 | "description": "Rebuild only changed jade files and all it dependencies", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git@github.com:pure180/gulp-pug-inheritance.git" 12 | }, 13 | "keywords": [ 14 | "gulpplugin", 15 | "jade", 16 | "pug", 17 | "jade-inheritance", 18 | "pug-inheritance" 19 | ], 20 | "author": "Juanfran Alcántara", 21 | "maintainers" : [ 22 | "Daniel Pfisterer " 23 | ], 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/juanfran/gulp-jade-inheritance/issues" 27 | }, 28 | "homepage": "https://github.com/pure180/gulp-pug-inheritance", 29 | "devDependencies": { 30 | "chai": "~3.5.0", 31 | "mocha": "~3.1.0", 32 | "path": "^0.12.7", 33 | "proxyquire": "^1.7.10", 34 | "sinon": "^1.17.6" 35 | }, 36 | "dependencies": { 37 | "event-stream": "^3.3.4", 38 | "gulp-util": "^3.0.7", 39 | "pug-inheritance": "^2.0.0", 40 | "lodash": "^4.16.4", 41 | "through2": "^2.0.1", 42 | "vinyl-fs": "^2.4.3" 43 | }, 44 | "skipInheritances": [ 45 | "node_modules" 46 | ] 47 | } 48 | -------------------------------------------------------------------------------- /test/fixtures/fixture1.pug: -------------------------------------------------------------------------------- 1 | p fixture 1 -------------------------------------------------------------------------------- /test/fixtures/fixture2.pug: -------------------------------------------------------------------------------- 1 | p fixture 2 2 | include fixture1 -------------------------------------------------------------------------------- /test/fixtures/fixture3.pug: -------------------------------------------------------------------------------- 1 | include fixture2 -------------------------------------------------------------------------------- /test/fixtures/fixture4.pug: -------------------------------------------------------------------------------- 1 | p fixture4 -------------------------------------------------------------------------------- /test/fixtures/fixture5.pug: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pure180/gulp-pug-inheritance/acf94bd77f9203c3d6bc7946145a3b8c55654a95/test/fixtures/fixture5.pug -------------------------------------------------------------------------------- /test/fixtures/subfolder/fixture5.pug: -------------------------------------------------------------------------------- 1 | p fixture 5 -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | var pluginPath = '../index'; 2 | var plugin = require(pluginPath); 3 | var chai = require('chai'); 4 | var path = require('path'); 5 | var expect = chai.expect; 6 | var gutil = require('gulp-util'); 7 | var fs = require('fs'); 8 | 9 | // var proxyquire = require('proxyquire'); 10 | // var sinon = require('sinon'); 11 | // var gutil = require('gulp-util'); 12 | 13 | var getFixtureFile = function (path) { 14 | return new gutil.File({ 15 | path: './test/fixtures/' + path, 16 | cwd: './test/', 17 | base: './test/fixtures/', 18 | contents: fs.readFileSync('./test/fixtures/' + path) 19 | }); 20 | }; 21 | 22 | describe('gulp-pug-inheritance', function(done) { 23 | it('pug with parents', function(done) { 24 | var fixture = getFixtureFile('fixture1.pug'); 25 | 26 | var fileNames = [ 27 | path.join('test', 'fixtures', 'fixture1.pug'), 28 | path.join('test', 'fixtures', 'fixture2.pug'), 29 | path.join('test', 'fixtures', 'fixture3.pug'), 30 | ]; 31 | 32 | var files = []; 33 | 34 | var stream = plugin(); 35 | stream 36 | .on('data', function (file) { 37 | expect(fileNames).to.include(file.relative); 38 | 39 | files.push(file); 40 | }) 41 | .once('end', function() { 42 | expect(files).to.have.length(3); 43 | 44 | done(); 45 | }); 46 | 47 | stream.write(fixture); 48 | stream.end(); 49 | }); 50 | 51 | it('pug without parents', function(done) { 52 | var fixture = getFixtureFile('fixture4.pug'); 53 | 54 | var files = []; 55 | 56 | var stream = plugin({extension: '.pug'}); 57 | stream 58 | .on('data', function (file) { 59 | files.push(file); 60 | }) 61 | .once('end', function() { 62 | expect(files).to.have.length(1); 63 | 64 | done(); 65 | }); 66 | 67 | stream.write(fixture); 68 | stream.end(); 69 | }); 70 | 71 | it('empty pug', function(done) { 72 | var fixture = getFixtureFile('fixture5.pug'); 73 | 74 | var files = []; 75 | 76 | var stream = plugin({extension: '.pug'}); 77 | stream 78 | .on('data', function (file) { 79 | files.push(file); 80 | }) 81 | .once('end', function() { 82 | expect(files).to.have.length(0); 83 | 84 | done(); 85 | }); 86 | 87 | stream.write(fixture); 88 | stream.end(); 89 | }); 90 | 91 | describe('custom basedir', function(done) { 92 | it('wrong path', function(done) { 93 | var fixture = getFixtureFile('fixture1.pug'); 94 | 95 | var files = []; 96 | 97 | var stream = plugin({basedir: 'test/fixtures5', extension: '.pug'}); 98 | stream 99 | .on('data', function (file) { 100 | files.push(file); 101 | }) 102 | .once('end', function() { 103 | expect(files).to.have.length(1); 104 | 105 | done(); 106 | }); 107 | 108 | stream.write(fixture); 109 | stream.end(); 110 | }); 111 | 112 | it('valid path', function(done) { 113 | var fixture = getFixtureFile('fixture1.pug'); 114 | 115 | var files = []; 116 | 117 | var stream = plugin({basedir: 'test/fixtures', extension: '.pug'}); 118 | stream 119 | .on('data', function (file) { 120 | files.push(file); 121 | }) 122 | .once('end', function() { 123 | expect(files).to.have.length(3); 124 | 125 | done(); 126 | }); 127 | 128 | stream.write(fixture); 129 | stream.end(); 130 | }); 131 | }); 132 | 133 | it('subfolder pug', function(done) { 134 | var fixture = getFixtureFile('subfolder/fixture5.pug'); 135 | 136 | var files = []; 137 | 138 | var stream = plugin({basedir: 'test/fixtures', extension: '.pug'}); 139 | stream 140 | .on('data', function (file) { 141 | expect(file.base).to.be.eql('test/fixtures'); 142 | files.push(file); 143 | }) 144 | .once('end', function() { 145 | expect(files).to.have.length(1); 146 | 147 | done(); 148 | }); 149 | 150 | stream.write(fixture); 151 | stream.end(); 152 | }); 153 | }); 154 | --------------------------------------------------------------------------------