├── test ├── mocha.opts └── test.js ├── .travis.yml ├── .gitignore ├── .editorconfig ├── .jshintrc ├── LICENSE ├── package.json ├── index.js └── README.md /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter min 2 | --ui bdd 3 | --growl 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "12" 4 | - "11" 5 | - "10" 6 | - "8" 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 2, 10 | "latedef": true, 11 | "newcap": true, 12 | "noarg": true, 13 | "quotmark": "single", 14 | "regexp": true, 15 | "undef": true, 16 | "unused": true, 17 | "strict": false, 18 | "trailing": true, 19 | "smarttabs": true 20 | } 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mickel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-angular-templatecache", 3 | "version": "3.0.1", 4 | "description": "Concatenates and registers AngularJS templates in the $templateCache.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": "miickel/gulp-angular-templatecache", 10 | "engines": { 11 | "node": ">=8.0.0" 12 | }, 13 | "files": [ 14 | "index.js" 15 | ], 16 | "keywords": [ 17 | "gulpplugin", 18 | "gulp", 19 | "angular", 20 | "angularjs", 21 | "ng", 22 | "html2js", 23 | "templatecache" 24 | ], 25 | "author": { 26 | "name": "Mickel", 27 | "email": "andersson.mickel@gmail.com", 28 | "url": "http://mickel.me" 29 | }, 30 | "contributors": [ 31 | "Simon Kurtz (https://github.com/simonua)", 32 | "Andrew Nichols (https://github.com/tandrewnichols)", 33 | "Donny Mellstrom (https://github.com/dmellstrom)", 34 | "Basa (https://github.com/Basa0)", 35 | "Jakob Hägg (https://github.com/Jakooob)", 36 | "Tereza Tomcova (https://github.com/the-ress)", 37 | "Tom Soal (https://github.com/tomsoal)" 38 | ], 39 | "license": "MIT", 40 | "dependencies": { 41 | "gulp-concat": "2.6.1", 42 | "gulp-footer": "2.1.0", 43 | "gulp-header": "2.0.7", 44 | "jsesc": "2.5.2", 45 | "lodash.template": "^4.4.0", 46 | "map-stream": "0.0.7", 47 | "stream-combiner": "0.2.2", 48 | "through2": "^3.0.1" 49 | }, 50 | "devDependencies": { 51 | "mocha": "latest", 52 | "vinyl": "^2.2.0" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var mapStream = require('map-stream'); 2 | var streamCombiner = require('stream-combiner'); 3 | var path = require('path'); 4 | var through2 = require('through2'); 5 | var lodashTemplate = require('lodash.template'); 6 | var concat = require('gulp-concat'); 7 | var header = require('gulp-header'); 8 | var footer = require('gulp-footer'); 9 | var jsesc = require('jsesc'); 10 | 11 | /** 12 | * "constants" 13 | */ 14 | 15 | var TEMPLATE_HEADER = 'angular.module(\'<%= module %>\'<%= standalone %>).run([\'$templateCache\', function($templateCache) {'; 16 | var TEMPLATE_BODY = '$templateCache.put(\'<%= url %>\',\'<%= contents %>\');'; 17 | var TEMPLATE_FOOTER = '}]);'; 18 | 19 | var DEFAULT_FILENAME = 'templates.js'; 20 | var DEFAULT_MODULE = 'templates'; 21 | var MODULE_TEMPLATES = { 22 | 23 | requirejs: { 24 | header: 'define([\'angular\'], function(angular) { \'use strict\'; return ', 25 | footer: '});' 26 | }, 27 | 28 | browserify: { 29 | header: '\'use strict\'; module.exports = ' 30 | }, 31 | 32 | es6: { 33 | header: 'import angular from \'angular\'; export default ', 34 | }, 35 | 36 | iife: { 37 | header: '(function(){\'use strict\';', 38 | footer: '})();' 39 | } 40 | 41 | }; 42 | 43 | /** 44 | * Add files to templateCache. 45 | */ 46 | 47 | function templateCacheFiles(root, base, templateBody, transformUrl, escapeOptions) { 48 | 49 | return function templateCacheFile(file, callback) { 50 | if (file.processedByTemplateCache) { 51 | return callback(null, file); 52 | } 53 | 54 | if (file.stat && file.stat.isDirectory()) { 55 | return callback(null, file); 56 | } 57 | 58 | var template = templateBody || TEMPLATE_BODY; 59 | var url; 60 | 61 | file.path = path.normalize(file.path); 62 | 63 | /** 64 | * Rewrite url 65 | */ 66 | 67 | if (typeof base === 'function') { 68 | url = path.join(root, base(file)); 69 | } else { 70 | url = path.join(root, file.path.replace(base || file.base, '')); 71 | } 72 | 73 | if (root === '.' || root.indexOf('./') === 0) { 74 | url = './' + url; 75 | } 76 | 77 | if (typeof transformUrl === 'function') { 78 | url = transformUrl(url); 79 | } 80 | 81 | /** 82 | * Normalize url (win only) 83 | */ 84 | 85 | if (process.platform === 'win32') { 86 | url = url.replace(/\\/g, '/'); 87 | } 88 | 89 | /** 90 | * Create buffer 91 | */ 92 | 93 | file.contents = Buffer.from(lodashTemplate(template)({ 94 | url: url, 95 | contents: jsesc(file.contents.toString('utf8'), escapeOptions), 96 | file: file 97 | })); 98 | 99 | file.processedByTemplateCache = true; 100 | 101 | callback(null, file); 102 | 103 | }; 104 | 105 | } 106 | 107 | /** 108 | * templateCache a stream of files. 109 | */ 110 | 111 | function templateCacheStream(root, base, templateBody, transformUrl, escapeOptions) { 112 | 113 | /** 114 | * Set relative base 115 | */ 116 | 117 | if (typeof base !== 'function' && base && base.substr(-1) !== path.sep) { 118 | base += path.sep; 119 | } 120 | 121 | /** 122 | * templateCache files 123 | */ 124 | 125 | return mapStream(templateCacheFiles(root, base, templateBody, transformUrl, escapeOptions)); 126 | 127 | } 128 | 129 | /** 130 | * Wrap templateCache with module system template. 131 | */ 132 | 133 | function wrapInModule(moduleSystem) { 134 | var moduleTemplate = MODULE_TEMPLATES[moduleSystem]; 135 | 136 | if (!moduleTemplate) { 137 | return through2.obj(); 138 | } 139 | 140 | return streamCombiner( 141 | header(moduleTemplate.header || ''), 142 | footer(moduleTemplate.footer || '') 143 | ); 144 | 145 | } 146 | 147 | /** 148 | * Concatenates and registers AngularJS templates in the $templateCache. 149 | * 150 | * @param {string} [filename='templates.js'] 151 | * @param {object} [options] 152 | */ 153 | 154 | function templateCache(filename, options) { 155 | 156 | /** 157 | * Prepare options 158 | */ 159 | 160 | if (typeof filename === 'string') { 161 | options = options || {}; 162 | } else { 163 | options = filename || {}; 164 | filename = options.filename || DEFAULT_FILENAME; 165 | } 166 | 167 | /** 168 | * Normalize moduleSystem option 169 | */ 170 | 171 | if (options.moduleSystem) { 172 | options.moduleSystem = options.moduleSystem.toLowerCase(); 173 | } 174 | 175 | /** 176 | * Prepare header / footer 177 | */ 178 | 179 | var templateHeader = 'templateHeader' in options ? options.templateHeader : TEMPLATE_HEADER; 180 | var templateFooter = 'templateFooter' in options ? options.templateFooter : TEMPLATE_FOOTER; 181 | 182 | /** 183 | * Build templateCache 184 | */ 185 | 186 | return streamCombiner( 187 | templateCacheStream(options.root || '', options.base, options.templateBody, options.transformUrl, options.escapeOptions || {}), 188 | concat(filename), 189 | header(templateHeader, { 190 | module: options.module || DEFAULT_MODULE, 191 | standalone: options.standalone ? ', []' : '' 192 | }), 193 | footer(templateFooter, { 194 | module: options.module || DEFAULT_MODULE 195 | }), 196 | wrapInModule(options.moduleSystem) 197 | ); 198 | 199 | } 200 | 201 | 202 | /** 203 | * Expose templateCache 204 | */ 205 | 206 | module.exports = templateCache; 207 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-angular-templatecache 2 | 3 | ## The [npm package](https://www.npmjs.com/package/gulp-angular-templatecache) related to this repo was deprecated after v3.0.1 was published on 02/17/22. Ownership has been responsibly transferred to npm. 4 | 5 | --- 6 | 7 | [![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://npmjs.org/package/gulp-angular-templatecache) 8 | [![NPM version](http://img.shields.io/npm/v/gulp-angular-templatecache.svg?style=flat)](https://npmjs.org/package/gulp-angular-templatecache) 9 | [![NPM version](http://img.shields.io/npm/dm/gulp-angular-templatecache.svg?style=flat)](https://npmjs.org/package/gulp-angular-templatecache) 10 | [![Build Status](http://img.shields.io/travis/miickel/gulp-angular-templatecache.svg?style=flat)](http://travis-ci.org/miickel/gulp-angular-templatecache) 11 | [![Dependency Status](http://img.shields.io/gemnasium/miickel/gulp-angular-templatecache.svg?style=flat)](https://gemnasium.com/miickel/gulp-angular-templatecache) 12 | 13 | > Concatenates and registers AngularJS templates in the `$templateCache`. 14 | 15 | Install | 16 | Example | 17 | API | 18 | [Releases](https://github.com/miickel/gulp-angular-templatecache/releases) | 19 | License 20 | 21 | ---- 22 | 23 | 24 | ## Install 25 | 26 | Install with [npm](https://npmjs.org/package/gulp-angular-templatecache) 27 | 28 | ``` 29 | npm install gulp-angular-templatecache --save-dev 30 | ``` 31 | 32 | 33 | ## Example 34 | 35 | **gulpfile.js** 36 | 37 | > Concatenate the contents of all .html-files in the templates directory and save to _public/templates.js_ (default filename). 38 | 39 | ```js 40 | var templateCache = require('gulp-angular-templatecache'); 41 | 42 | gulp.task('default', function () { 43 | return gulp.src('templates/**/*.html') 44 | .pipe(templateCache()) 45 | .pipe(gulp.dest('public')); 46 | }); 47 | ``` 48 | 49 | **Result (_public/templates.js_)** 50 | 51 | > Sample output (prettified). 52 | 53 | ```js 54 | angular.module("templates").run([$templateCache, 55 | function($templateCache) { 56 | $templateCache.put("template1.html", 57 | // template1.html content (escaped) 58 | ); 59 | $templateCache.put("template2.html", 60 | // template2.html content (escaped) 61 | ); 62 | // etc. 63 | } 64 | ]); 65 | 66 | ``` 67 | 68 | Include this file in your app and AngularJS will use the $templateCache when available. 69 | 70 | __Note:__ this plugin will __not__ create a new AngularJS module by default, but use a module called `templates`. If you would like to create a new module, set [options.standalone](https://github.com/miickel/gulp-angular-templatecache#standalone---boolean-standalonefalse) to `true`. 71 | 72 | __Note:__ if you use Visual Studio on Windows, you might encounter this error message: `ASPNETCOMPILER : error ASPRUNTIME: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.` 73 | 74 | This is most likely due to long path names, and can be fixed by adding `lodash.bind` as a dev dependecy in your package.json. Anyway, if you encounter this error, please drop a note in #62, and we might merge #63. 75 | 76 | 77 | ## API 78 | 79 | gulp-angular-templatecache([filename](https://github.com/miickel/gulp-angular-templatecache#filename---string-filenametemplatesjs), [options](https://github.com/miickel/gulp-angular-templatecache#options)) 80 | 81 | ---- 82 | 83 | ### filename - {string} [filename='templates.js'] 84 | 85 | > Name to use when concatenating. 86 | 87 | ### options 88 | 89 | #### root - {string} 90 | 91 | > Prefix for template URLs. 92 | 93 | #### module - {string} [module='templates'] 94 | 95 | > Name of AngularJS module. 96 | 97 | #### standalone - {boolean} [standalone=false] 98 | 99 | > Create a new AngularJS module, instead of using an existing. 100 | 101 | #### base {string | function} [base=file.base] 102 | 103 | > Override file base path. 104 | 105 | #### moduleSystem {string} 106 | 107 | > Wrap the templateCache in a module system. Currently supported systems: `RequireJS`, `Browserify`, `ES6` and `IIFE` (Immediately-Invoked Function Expression). 108 | 109 | #### transformUrl {function} 110 | 111 | > Transform the generated URL before it's put into `$templateCache`. 112 | 113 | ```js 114 | transformUrl: function(url) { 115 | return url.replace(/\.tpl\.html$/, '.html') 116 | } 117 | ``` 118 | 119 | #### templateHeader {string} [templateHeader=see below] 120 | 121 | > Override template header. 122 | 123 | ```js 124 | var TEMPLATE_HEADER = 'angular.module("<%= module %>"<%= standalone %>).run(["$templateCache", function($templateCache) {'; 125 | ``` 126 | 127 | #### templateBody {string} [templateBody=see below] 128 | 129 | > Override template body. 130 | 131 | ```js 132 | var TEMPLATE_BODY = '$templateCache.put("<%= url %>","<%= contents %>");'; 133 | ``` 134 | 135 | #### templateFooter {string} [templateFooter=see below] 136 | 137 | > Override template footer. 138 | 139 | ```js 140 | var TEMPLATE_FOOTER = '}]);'; 141 | ``` 142 | 143 | #### escapeOptions - {object} 144 | 145 | > Options for jsesc module. See [jsesc API](https://www.npmjs.com/package/jsesc#api) 146 | 147 | 148 | ## Changes 149 | 150 | > This plugin uses Semantic Versioning 2.0.0 151 | 152 | ### 1.1.0 and newer 153 | 154 | See [Releases](https://github.com/miickel/gulp-angular-templatecache/releases) 155 | 156 | ### 1.0.0 157 | 158 | > Cleaner code, more tests and improved documentation. Thoroughly used in development. 159 | 160 | - adds 161 | - `options.standalone` (**breaking**) 162 | - fixes 163 | - Windows support 164 | - changes 165 | - `filename` now optional 166 | 167 | ### 0.3.0 168 | 169 | - adds 170 | - `options.module` 171 | 172 | ### 0.2.0 and earlier 173 | 174 | > Only used by mad men 175 | 176 | ![](http://media3.giphy.com/media/bAplZhiLAsNnG/giphy.gif) 177 | 178 | 179 | ## License 180 | 181 | The MIT License (MIT) 182 | 183 | Copyright (c) 2014 [Mickel](http://mickel.me) 184 | 185 | Permission is hereby granted, free of charge, to any person obtaining a copy of 186 | this software and associated documentation files (the "Software"), to deal in 187 | the Software without restriction, including without limitation the rights to 188 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 189 | the Software, and to permit persons to whom the Software is furnished to do so, 190 | subject to the following conditions: 191 | 192 | The above copyright notice and this permission notice shall be included in all 193 | copies or substantial portions of the Software. 194 | 195 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 196 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 197 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 198 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 199 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 200 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 201 | 202 | [![Analytics](https://ga-beacon.appspot.com/UA-46880034-1/gulp-angular-templatecache/readme?pixel)](https://github.com/igrigorik/ga-beacon) 203 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var assert = require('assert'); 3 | var Vinyl = require('vinyl'); 4 | var templateCache = require('../index'); 5 | 6 | describe('gulp-angular-templatecache', function () { 7 | 8 | 9 | it('should build valid $templateCache from multiple source-files', function (cb) { 10 | var stream = templateCache('templates.js'); 11 | 12 | stream.on('data', function (file) { 13 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 14 | assert.equal(file.relative, 'templates.js'); 15 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'

I\\\'m template A!

\');\n$templateCache.put(\'/template-b.html\',\'

I\\\'m template B!

\');}]);'); 16 | cb(); 17 | }); 18 | 19 | stream.write(new Vinyl({ 20 | base: __dirname, 21 | path: __dirname + '/template-a.html', 22 | contents: Buffer.from('

I\'m template A!

') 23 | })); 24 | 25 | stream.write(new Vinyl({ 26 | base: __dirname, 27 | path: __dirname + '/template-b.html', 28 | contents: Buffer.from('

I\'m template B!

') 29 | })); 30 | 31 | stream.end(); 32 | }); 33 | 34 | it('should allow options as first parameter if no filename is specified', function (cb) { 35 | var stream = templateCache({ 36 | standalone: true, 37 | root: '/views' 38 | }); 39 | 40 | stream.on('data', function (file) { 41 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 42 | assert.equal(file.relative, 'templates.js'); 43 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/views/template-a.html\',\'

I\\\'m template A!

\');}]);'); 44 | cb(); 45 | }); 46 | 47 | stream.write(new Vinyl({ 48 | base: __dirname, 49 | path: __dirname + '/template-a.html', 50 | contents: Buffer.from('

I\'m template A!

') 51 | })); 52 | 53 | stream.end(); 54 | }); 55 | 56 | it('should ignore directories', function (cb) { 57 | var stream = templateCache(); 58 | 59 | stream.on('data', function (file) { 60 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 61 | assert.equal(file.relative, 'templates.js'); 62 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/directory/template-a.html\',\'

I\\\'m template A!

\');}]);'); 63 | cb(); 64 | }); 65 | 66 | stream.write(new Vinyl({ 67 | base: __dirname, 68 | path: __dirname + '/directory', 69 | contents: null, 70 | stat: { 71 | isDirectory: () => true 72 | } 73 | })); 74 | 75 | stream.write(new Vinyl({ 76 | base: __dirname, 77 | path: __dirname + '/directory/template-a.html', 78 | contents: Buffer.from('

I\'m template A!

') 79 | })); 80 | 81 | stream.end(); 82 | }); 83 | 84 | 85 | describe('options.root', function () { 86 | 87 | it('should set root', function (cb) { 88 | var stream = templateCache('templates.js', { 89 | root: '/views' 90 | }); 91 | 92 | stream.on('data', function (file) { 93 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 94 | assert.equal(file.relative, 'templates.js'); 95 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/views/template-a.html\',\'

I\\\'m template A!

\');}]);'); 96 | cb(); 97 | }); 98 | 99 | stream.write(new Vinyl({ 100 | base: __dirname, 101 | path: __dirname + '/template-a.html', 102 | contents: Buffer.from('

I\'m template A!

') 103 | })); 104 | 105 | stream.end(); 106 | }); 107 | 108 | it('should preserve the "./" if there is one in front of the root', function (cb) { 109 | var stream = templateCache('templates.js', { 110 | root: './' 111 | }); 112 | 113 | stream.on('data', function (file) { 114 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 115 | assert.equal(file.relative, 'templates.js'); 116 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'./template-a.html\',\'

I\\\'m template A!

\');}]);'); 117 | cb(); 118 | }); 119 | 120 | stream.write(new Vinyl({ 121 | base: __dirname, 122 | path: __dirname + '/template-a.html', 123 | contents: Buffer.from('

I\'m template A!

') 124 | })); 125 | 126 | stream.end(); 127 | }); 128 | 129 | it('should preserve the "." if there is one in front of the root', function (cb) { 130 | var stream = templateCache('templates.js', { 131 | root: '.' 132 | }); 133 | 134 | stream.on('data', function (file) { 135 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 136 | assert.equal(file.relative, 'templates.js'); 137 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'./template-a.html\',\'

I\\\'m template A!

\');}]);'); 138 | cb(); 139 | }); 140 | 141 | stream.write(new Vinyl({ 142 | base: __dirname, 143 | path: __dirname + '/template-a.html', 144 | contents: Buffer.from('

I\'m template A!

') 145 | })); 146 | 147 | stream.end(); 148 | }); 149 | 150 | it('should preserve the root as is, if the root folder name start with a "." character', function (cb) { 151 | var stream = templateCache('templates.js', { 152 | root: '.root/' 153 | }); 154 | 155 | stream.on('data', function (file) { 156 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 157 | assert.equal(file.relative, 'templates.js'); 158 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'.root/template-a.html\',\'

I\\\'m template A!

\');}]);'); 159 | cb(); 160 | }); 161 | 162 | stream.write(new Vinyl({ 163 | base: __dirname, 164 | path: __dirname + '/template-a.html', 165 | contents: Buffer.from('

I\'m template A!

') 166 | })); 167 | 168 | stream.end(); 169 | }); 170 | 171 | }); 172 | 173 | 174 | describe('options.transformUrl', function () { 175 | 176 | it('should change the URL to the output of the function', function (cb) { 177 | var stream = templateCache('templates.js', { 178 | transformUrl: function(url) { 179 | return url.replace(/template/, 'tpl'); 180 | } 181 | }); 182 | 183 | stream.on('data', function (file) { 184 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 185 | assert.equal(file.relative, 'templates.js'); 186 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/tpl-a.html\',\'

I\\\'m template A!

\');}]);'); 187 | cb(); 188 | }); 189 | 190 | stream.write(new Vinyl({ 191 | base: __dirname, 192 | path: __dirname + '/template-a.html', 193 | contents: Buffer.from('

I\'m template A!

') 194 | })); 195 | 196 | stream.end(); 197 | }); 198 | 199 | it('should set the final url, after any root option has been applied', function (cb) { 200 | var stream = templateCache('templates.js', { 201 | root: './views', 202 | transformUrl: function(url) { 203 | return '/completely/transformed/final'; 204 | } 205 | }); 206 | 207 | stream.on('data', function (file) { 208 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 209 | assert.equal(file.relative, 'templates.js'); 210 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/completely/transformed/final\',\'

I\\\'m template A!

\');}]);'); 211 | cb(); 212 | }); 213 | 214 | stream.write(new Vinyl({ 215 | base: __dirname, 216 | path: __dirname + '/template-a.html', 217 | contents: Buffer.from('

I\'m template A!

') 218 | })); 219 | 220 | stream.end(); 221 | }); 222 | 223 | }); 224 | 225 | 226 | describe('options.standalone', function () { 227 | 228 | it('should create standalone Angular module', function (cb) { 229 | var stream = templateCache('templates.js', { 230 | standalone: true 231 | }); 232 | 233 | stream.on('data', function (file) { 234 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 235 | assert.equal(file.relative, 'templates.js'); 236 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'

I\\\'m template A!

\');}]);'); 237 | cb(); 238 | }); 239 | 240 | stream.write(new Vinyl({ 241 | base: __dirname, 242 | path: __dirname + '/template-a.html', 243 | contents: Buffer.from('

I\'m template A!

') 244 | })); 245 | 246 | stream.end(); 247 | }); 248 | 249 | }); 250 | 251 | 252 | describe('options.filename', function () { 253 | 254 | it('should default to templates.js if not specified', function (cb) { 255 | var stream = templateCache(); 256 | 257 | stream.on('data', function (file) { 258 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 259 | assert.equal(file.relative, 'templates.js'); 260 | cb(); 261 | }); 262 | 263 | stream.write(new Vinyl({ 264 | base: __dirname, 265 | path: __dirname + '/template-a.html', 266 | contents: Buffer.from('

I\'m template A!

') 267 | })); 268 | 269 | stream.end(); 270 | }); 271 | 272 | it('should set filename', function (cb) { 273 | var stream = templateCache({ 274 | standalone: true, 275 | root: '/views', 276 | filename: 'foobar.js' 277 | }); 278 | 279 | stream.on('data', function (file) { 280 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/foobar.js')); 281 | assert.equal(file.relative, 'foobar.js'); 282 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/views/template-a.html\',\'

I\\\'m template A!

\');}]);'); 283 | cb(); 284 | }); 285 | 286 | stream.write(new Vinyl({ 287 | base: __dirname, 288 | path: __dirname + '/template-a.html', 289 | contents: Buffer.from('

I\'m template A!

') 290 | })); 291 | 292 | stream.end(); 293 | }); 294 | 295 | }); 296 | 297 | 298 | describe('options.base', function () { 299 | 300 | it('should set base url', function (cb) { 301 | var stream = templateCache({ 302 | standalone: true, 303 | root: '/views', 304 | base: path.resolve(__dirname, '..') 305 | }); 306 | 307 | stream.on('data', function (file) { 308 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 309 | assert.equal(file.relative, 'templates.js'); 310 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/views/test/template-a.html\',\'

I\\\'m template A!

\');}]);'); 311 | cb(); 312 | }); 313 | 314 | stream.write(new Vinyl({ 315 | base: __dirname, 316 | path: __dirname + '/template-a.html', 317 | contents: Buffer.from('

I\'m template A!

') 318 | })); 319 | 320 | stream.end(); 321 | }); 322 | 323 | it('should allow functions', function (cb) { 324 | var stream = templateCache({ 325 | standalone: true, 326 | root: '/templates', 327 | base: function (file) { 328 | return '/all/' + file.relative; 329 | } 330 | }); 331 | 332 | stream.on('data', function (file) { 333 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 334 | assert.equal(file.relative, 'templates.js'); 335 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/templates/all/template-a.html\',\'

I\\\'m template A!

\');}]);'); 336 | cb(); 337 | }); 338 | 339 | stream.write(new Vinyl({ 340 | base: __dirname, 341 | path: __dirname + '/template-a.html', 342 | contents: Buffer.from('

I\'m template A!

') 343 | })); 344 | 345 | stream.end(); 346 | }); 347 | 348 | }); 349 | 350 | 351 | describe('options.moduleSystem', function () { 352 | 353 | it('should support Browserify-style exports', function (cb) { 354 | var stream = templateCache('templates.js', { 355 | moduleSystem: 'Browserify', 356 | standalone: true 357 | }); 358 | 359 | stream.on('data', function (file) { 360 | assert.equal(file.path, path.normalize(__dirname + '/templates.js')); 361 | assert.equal(file.relative, 'templates.js'); 362 | assert.equal(file.contents.toString('utf8'), '\'use strict\'; module.exports = angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'

I\\\'m template A!

\');}]);'); 363 | cb(); 364 | }); 365 | 366 | stream.write(new Vinyl({ 367 | base: __dirname, 368 | path: __dirname + '/template-a.html', 369 | contents: Buffer.from('

I\'m template A!

') 370 | })); 371 | 372 | stream.end(); 373 | }); 374 | 375 | it('should support RequireJS-style exports', function (cb) { 376 | var stream = templateCache('templates.js', { 377 | moduleSystem: 'RequireJS' 378 | }); 379 | 380 | stream.on('data', function (file) { 381 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 382 | assert.equal(file.relative, 'templates.js'); 383 | assert.equal(file.contents.toString('utf8'), 'define([\'angular\'], function(angular) { \'use strict\'; return angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'

I\\\'m template A!

\');}]);});'); 384 | cb(); 385 | }); 386 | 387 | stream.write(new Vinyl({ 388 | base: __dirname, 389 | path: __dirname + '/template-a.html', 390 | contents: Buffer.from('

I\'m template A!

') 391 | })); 392 | 393 | stream.end(); 394 | }); 395 | 396 | it('should support ES6-style exports', function (cb) { 397 | var stream = templateCache('templates.js', { 398 | moduleSystem: 'ES6' 399 | }); 400 | 401 | stream.on('data', function (file) { 402 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 403 | assert.equal(file.relative, 'templates.js'); 404 | assert.equal(file.contents.toString('utf8'), 'import angular from \'angular\'; export default angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'

I\\\'m template A!

\');}]);'); 405 | cb(); 406 | }); 407 | 408 | stream.write(new Vinyl({ 409 | base: __dirname, 410 | path: __dirname + '/template-a.html', 411 | contents: Buffer.from('

I\'m template A!

') 412 | })); 413 | 414 | stream.end(); 415 | }); 416 | 417 | it('should support IIFE-style exports', function (cb) { 418 | var stream = templateCache('templates.js', { 419 | moduleSystem: 'IIFE' 420 | }); 421 | 422 | stream.on('data', function (file) { 423 | assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js')); 424 | assert.equal(file.relative, 'templates.js'); 425 | assert.equal(file.contents.toString('utf8'), '(function(){\'use strict\';angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'

I\\\'m template A!

\');}]);})();'); 426 | cb(); 427 | }); 428 | 429 | stream.write(new Vinyl({ 430 | base: __dirname, 431 | path: __dirname + '/template-a.html', 432 | contents: Buffer.from('

I\'m template A!

') 433 | })); 434 | 435 | stream.end(); 436 | }); 437 | 438 | }); 439 | 440 | describe('options.templateHeader & options.templateFooter', function () { 441 | 442 | it('should override TEMPLATE_HEADER & TEMPLATE_FOOTER', function (cb) { 443 | var stream = templateCache('templates.js', { 444 | templateHeader: 'var template = "', 445 | templateFooter: '";' 446 | }); 447 | 448 | stream.on('data', function (file) { 449 | assert.equal(file.path, path.normalize(__dirname + '/templates.js')); 450 | assert.equal(file.relative, 'templates.js'); 451 | assert.equal(file.contents.toString('utf8'), 'var template = "$templateCache.put(\'/template-a.html\',\'yoo\');";'); 452 | cb(); 453 | }); 454 | 455 | stream.write(new Vinyl({ 456 | base: __dirname, 457 | path: __dirname + '/template-a.html', 458 | contents: Buffer.from('yoo') 459 | })); 460 | 461 | stream.end(); 462 | }); 463 | 464 | it('should accept empty strings as header and footer', function (cb) { 465 | var stream = templateCache('templates.js', { 466 | templateHeader: '', 467 | templateFooter: '' 468 | }); 469 | 470 | stream.on('data', function (file) { assert 471 | assert.equal(file.path, path.normalize(__dirname + '/templates.js')); 472 | assert.equal(file.relative, 'templates.js'); 473 | assert.equal(file.contents.toString('utf8'), '$templateCache.put(\'/template-a.html\',\'yoo\');'); 474 | cb(); 475 | }); 476 | 477 | stream.write(new Vinyl({ 478 | base: __dirname, 479 | path: __dirname + '/template-a.html', 480 | contents: Buffer.from('yoo') 481 | })); 482 | 483 | stream.end(); 484 | }); 485 | }); 486 | 487 | describe('options.templateBody', function () { 488 | 489 | it('should override TEMPLATE_BODY', function (cb) { 490 | var stream = templateCache('templates.js', { 491 | templateBody: '$templateCache.put(\'<%= url %>\',\'<%= contents %>\');', 492 | }); 493 | 494 | stream.on('data', function (file) { 495 | assert.equal(file.path, path.normalize(__dirname + '/templates.js')); 496 | assert.equal(file.relative, 'templates.js'); 497 | assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'yoo\');}]);'); 498 | cb(); 499 | }); 500 | 501 | stream.write(new Vinyl({ 502 | base: __dirname, 503 | path: __dirname + '/template-a.html', 504 | contents: Buffer.from('yoo') 505 | })); 506 | 507 | stream.end(); 508 | }); 509 | 510 | }); 511 | 512 | 513 | }); 514 | --------------------------------------------------------------------------------