2.0.0 • Published 7 years ago

yellfy-pug-inheritance v2.0.0

Weekly downloads
34
License
MIT
Repository
github
Last release
7 years ago

Package deprecated

Please, use emitty. It's easier and more convenient.




yellfy-pug-inheritance

Determine the inheritance of Pug (ex. Jade) templates.

Travis Status

Install

$ npm i -D yellfy-pug-inheritance

Why?

Because existing solutions use redundant modules such as Pug parser and Glob.

Usage

const pugInheritance = require('yellfy-pug-inheritance');

pugInheritance.updateTree('./path/to/pug/files').then((inheritance) => {
  console.log(inheritance.tree);
  // 'main.pug': [
  //   'a.pug',
  //   'dir/b.pug'
  // ],
  // 'a.pug': [],
  // 'dir/b.pug': []
});

API

updateTree(dir, options) → result

Options

changedFile

  • Type: String
  • Default: ''

The name of the file that has been changed since the last build tree.

treeCache

  • Type: ITreeStorage{ [filename: string]: string[] }
  • Default: {}

The previous tree of dependencies that will be used as cache.

jade

  • Type: Boolean
  • Default: false

Working with Jade files.

Result

tree

  • Return: ITreeStorage{ [filename: string]: string[] }

Returns the dependency tree for all files.

getDependencies(filename)

  • Return: string[]

Returns an array of dependencies for specified file.

checkDependency(filename, filenameToCheck)

  • Return: boolean

Returns true if the specified file is in the dependencies of the specified file.

Gulp example

// npm i gulpjs/gulp#4.0 gulp-if gulp-filter yellfy-pug-inheritance gulp-pug
const gulp = require('gulp');
const gulpif = require('gulp-if');
const filter = require('gulp-filter');
const pugInheritance = require('yellfy-pug-inheritance');
const pug = require('gulp-pug');

// Cache
let pugInheritanceCache = {};

// Root directory that contains your Pug files
const pugDirectory = 'app/templates';

// Watch task
gulp.task('watch', () => {
  global.watch = true;

  gulp.watch([`${pugDirectory}/**/*.pug`], gulp.series('templates'))
    .on('all', (event, filepath) => {
      global.changedTempalteFile = filepath.replace(/\\/g, '/');
    });
});

// Filter for files
function pugFilter(file, inheritance) {
  const filepath = `${pugDirectory}/${file.relative}`;
  if (inheritance.checkDependency(filepath, global.changedTempalteFile)) {
    console.log(`Compiling: ${filepath}`);
    return true;
  }

  return false;
}

// Templates task
gulp.task('templates', () => {
  return new Promise((resolve, reject) => {
    const changedFile = global.changedTempalteFile;
    const options = {
      changedFile,
      treeCache: pugInheritanceCache
    };

    // Update cache for all files or only for specified file
    pugInheritance.updateTree(pugDirectory, options).then((inheritance) => {
      // Save cache for secondary compilations
      pugInheritanceCache = inheritance.tree;

      return gulp.src(`${pugDirectory}/*.pug`)
        // We can use Cache only for Watch mode
        .pipe(gulpif(global.watch, filter((file) => pugFilter(file, inheritance))))
        .pipe(pug({ pretty: true }))
        .pipe(gulp.dest('build'))
        .on('end', resolve)
        .on('error', reject);
    });
  });
});

Changelog

See the Releases section of our GitHub project for changelogs for each release version.

License

This software is released under the terms of the MIT license.

2.0.0

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

8 years ago

1.0.0

8 years ago

1.0.0-beta.1

8 years ago

1.0.0-beta.0

8 years ago

1.0.0-alpha.3

8 years ago

1.0.0-alpha.2

8 years ago

1.0.0-alpha.1

8 years ago

1.0.0-alpha.0

8 years ago