Skip to content

Pug (ex Jade) HTML template engine plugin #1433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The generated project has dependencies that require
* [Linting and formatting code](#linting-and-formatting-code)
* [Support for offline applications](#support-for-offline-applications)
* [Commands autocompletion](#commands-autocompletion)
* [HTML template engine integration](#html-template-engine-integration)
* [CSS preprocessor integration](#css-preprocessor-integration)
* [3rd Party Library Installation](#3rd-party-library-installation)
* [Updating angular-cli](#updating-angular-cli)
Expand Down Expand Up @@ -222,6 +223,12 @@ ng completion >> ~/.bash_profile
source ~/.bash_profile
```

### HTML Template Engine integration

We currently support `Pug` (ex `Jade`) HTML template engine.
To use `Pug` install `npm install pug --save` in your project and rename `.html` template extensions to `.pug`. Templates will be compiled automatically.

If you already have `pug` installed globally the plugin will be instatinated automatically and the templates will be compiled to HTML.

### CSS Preprocessor integration

Expand Down
54 changes: 54 additions & 0 deletions lib/broccoli/angular-broccoli-pug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* jshint node: true, esversion: 6 */
'use strict';

const requireOrNull = require('./require-or-null');
const Plugin = require('broccoli-caching-writer');
const fse = require('fs-extra');
const path = require('path');
const Funnel = require('broccoli-funnel');

let pug = requireOrNull('pug');
if (!pug) {
pug = requireOrNull(`${process.env.PROJECT_ROOT}/node_modules/pug`);
}

class PugPlugin extends Plugin {
constructor(inputNodes, options) {
super(inputNodes, {});

options = options || {};
Plugin.call(this, inputNodes, {
cacheInclude: [/\.pug$|\.jade$/]
});
this.options = options;
}

build() {
return Promise.all(this.listEntries().map(e => {
let fileName = path.resolve(e.basePath, e.relativePath);
return this.compile(fileName, this.inputPaths[0], this.outputPath);
}));
}

compile(fileName, inputPath, outputPath) {
return new Promise(resolve => {
let filePath = fileName.replace(inputPath, outputPath).replace(/\.pug$|\.jade$/, '.html');
let html = pug.renderFile(fileName, {
pretty: true
});
fse.outputFileSync(filePath, html, 'utf8');
resolve();
});
}
}

exports.makeBroccoliTree = (sourceDir, options) => {
if (pug) {
let pugSrcTree = new Funnel(sourceDir, {
include: ['**/*.pug', '**/*.jade'],
allowEmpty: true
});

return new PugPlugin([pugSrcTree], options);
}
};
6 changes: 3 additions & 3 deletions lib/broccoli/angular2-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class Angular2App extends BroccoliPlugin {

var buildTrees = [assetTree, tsTree, indexTree, vendorNpmTree];

// Add available and supported CSS plugins.
for (const suffix of ['sass', 'less', 'stylus', 'compass']) {
// Add available CSS and HTML template plugins.
for (const suffix of ['sass', 'less', 'stylus', 'compass', 'pug']) {
const plugin = require(`./angular-broccoli-${suffix}`);
const tree = plugin.makeBroccoliTree(this._inputNode, this._options[`${suffix}Compiler`]);

Expand All @@ -145,7 +145,7 @@ class Angular2App extends BroccoliPlugin {
return n;
}
}));
}
}

var merged = new BroccoliMergeTrees(buildTrees, { overwrite: true });

Expand Down