• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Gulp plugin for compiling the HTML files of your Angular app to Javascript.

gulp-ng-html2js NPM version Build Status Dependency Status

A plugin for gulp which generates AngularJS modules, which pre-load your HTML code into the $templateCache. This way AngularJS doesn't need to request the actual HTML files anymore.

Usage

First, install gulp-ng-html2js as a development dependency:

npm install --save-dev gulp-ng-html2js

Then, add it to your gulpfile.js:

var ngHtml2Js = require("gulp-ng-html2js");

gulp.src("./partials/*.html")
	.pipe(ngHtml2Js({
		moduleName: "MyAwesomePartials",
		prefix: "/partials"
	}))
	.pipe(gulp.dest("./dist/partials"));

The main reason to use this module would be optimization. By pre-loading the HTML files, you can spare requests and loading time when the files are actually needed. When you are optimizing, you should do it properly. So, we should add the following plugins: gulp-minify-html, gulp-uglify, and gulp-concat:

var ngHtml2Js = require("gulp-ng-html2js");
var minifyHtml = require("gulp-minify-html");
var concat = require("gulp-concat");
var uglify = require("gulp-uglify");

gulp.src("./partials/*.html")
	.pipe(minifyHtml({
		empty: true,
		spare: true,
		quotes: true
	}))
	.pipe(ngHtml2Js({
		moduleName: "MyAwesomePartials",
		prefix: "/partials"
	}))
	.pipe(concat("partials.min.js"))
	.pipe(uglify())
	.pipe(gulp.dest("./dist/partials"));

This way you end up with 1 single, minified Javascript file, which pre-loads all the (minified) HTML templates.

If you have your modules sorted into directories that match the module name, you could do something like this:

// This picks up files like this:
//   partials/date-picker/year.html (as well as month.html, day.html)
//   partials/expanded-combo-box/combobox.html
//   partials/forms/feedback.html (as well as survey.html, contact.html)
// Returns modules like this:
//   datePicker, expandedComboBox, forms
gulp.src("./partials/**/*.html")
    .pipe(ngHtml2Js({
		moduleName: function (file) {
			var pathParts = file.path.split('/');
			var folder = pathParts[pathParts.length - 2];
			return folder.replace(/-[a-z]/g, function (match) {
				return match.substr(1).toUpperCase();
			});
		}
	}))
	.pipe(concat("partials.min.js"))
	.pipe(gulp.dest('./dist/partials'));
}

API

ngHtml2Js(options)

options.moduleName

Type: String or Function

The name of the generated AngularJS module. Uses the file url if omitted.

When this is a function, the returned value will be the module name. The function will be passed the vinyl file object so the module name can be determined from the path, content, last access time or any other property. Returning undefined will fall back to the file url.

options.declareModule

Type: Boolean

Whether to attempt to declare a new module (used with options.moduleName). True if omitted.

Set this to false if options.moduleName is already declared.

options.prefix

Type: String

The prefix which should be prepended to the file path to generate the file url.

options.stripPrefix

Type: String

The prefix which should be subtracted from the file path to generate the file url.

options.rename

Type: Function

A function that allows the generate file url to be manipulated. For example:

function (templateUrl, templateFile) {
  return templateUrl.replace('.tpl.html', '.html');
}

options.template

Type: String

A custom Lodash template for generating the Javacript code. The template is called with the following params:

  • moduleName: the resulting module name.
  • template
    • url: the resulting template url.
    • content: the HTML content of the input file.
    • escapedContent: the escaped HTML content of the input file. Note: the HTML content is escaped for usage in a single quoted string.
    • prettyEscapedContent: the readable, escaped HTML content of the input file.

Example

{
  template: "$templateCache.put('<%= template.url %>', '<%= template.escapedContent %>');"
}

options.extension

Type: String

The file extension of the generated files. Defaults to .js. Can be used to generate TypeScript files and create a gulp TypeScript - job to convert them. For a working example take a look at angular-systemjs-typescript-boilerplate

options.export

Type: String

  • commonjs: export the angular module using module.exports =
  • system: export the angular module using export default

Note this does not export anything with declareModule set to true.

Example

{
  export: 'commonjs'
}
{
  export: 'system'
}

License

MIT License

More Repositories

1

node-toogoodtogo-watcher

Node.js cli tool for monitoring your favorite TooGoodToGo businesses. Docker image available.
JavaScript
363
star
2

ui-router.stateHelper

A helper module for AngularUI Router, which allows you to define your states as an object tree.
JavaScript
235
star
3

lodash-deep

Lodash mixins for (deep) object accessing / manipulation.
JavaScript
222
star
4

node-pm2-windows-startup

Utility to make PM2 automatically resurrect on Windows startup
JavaScript
218
star
5

angular-recursion

A service which makes it easy possible to have recursive Angular directives.
JavaScript
176
star
6

WinLess

An awesome Windows GUI for less.js
C#
132
star
7

node-start-on-windows-boot

Simple utility to enable / disable starting a program on Windows boot, by modifiying the right registry values.
JavaScript
60
star
8

jquery.tabbable

Simple utility for selecting the next / previous ':tabbable' element.
JavaScript
47
star
9

obs-scene-execute-command-script

OBS script for executing any CLI command whenever whenever a scene is activated
Lua
28
star
10

node-transip-dns-cli

Node.js cli tool for updating TransIP DNS entries, including DDNS. Docker image available.
JavaScript
21
star
11

node-onvif-ptz-cli

Node.js cli tool for controlling PTZ cameras via the ONVIF protocol. Docker image available.
JavaScript
19
star
12

angular-class

An angular service for creating classes
JavaScript
16
star
13

docker-compose-homeassistant

Docker Compose example repository based on my home server setup, which runs on a Raspberry Pi.
Shell
9
star
14

mediawiki-dia

A MediaWiki extension which renders .dia files as .svg.
PHP
2
star
15

node-ibood-watcher

Node script to check ibood.com for new products during a 'hunt'.
JavaScript
1
star
16

angular-material-multi-level-sidenav-examle

Created with StackBlitz ⚡️
TypeScript
1
star
17

meat-fan

BBQ Fan Controller design with ESPHome firmware
1
star
18

webdriverio-wait-and

JavaScript
1
star
19

marklagendijk.nl

My personal website
TypeScript
1
star
20

node-istock-balance-scraper

Scrapes your current balance from you iStock account
JavaScript
1
star