1 | # gulp-inline-ng2-template
|
2 |
|
3 | Inline Angular2 HTML and CSS files into JavaScript ES5/ES6 and TypeScript files (and possibly more - not tested).
|
4 |
|
5 | [![Build Status](https://travis-ci.org/ludohenin/gulp-inline-ng2-template.svg?branch=master)](https://travis-ci.org/ludohenin/gulp-inline-ng2-template)
|
6 | [![npm](https://img.shields.io/npm/dm/gulp-inline-ng2-template.svg?maxAge=2592000)](https://www.npmjs.com/package/gulp-inline-ng2-template)
|
7 |
|
8 | This plugin uses the [ES6 template strings](https://github.com/lukehoban/es6features#template-strings) syntax by default _(which requires the use of a transpiler -typescript, babel, traceur- to produce valid ES5 files)_ but you can opt-in for ES5 one.
|
9 |
|
10 | Very convenient to unit test your component or bundle your components/application (avoid extra HTTP request and keeps your source clean).
|
11 |
|
12 | __note:__
|
13 |
|
14 | * 2.0.0 - __Breaking changes__
|
15 | * Refactor the parser and make it async
|
16 | * `templateProcessor` and `styleProcessor` now accept a callback as 3rd argument
|
17 | * If you're not using the processor functions, everything will work as in 1.x.
|
18 | * 1.1.5 adds `customFilePath` option
|
19 | * 1.1.4 adds `supportNonExistentFiles` option
|
20 | * 1.1.0 adds templateFunction when templateUrl is a function
|
21 | * 1.0.0 - __Breaking changes__
|
22 | * Add suppport for processors (templates & styles)
|
23 | * Refactor configuration object (`html` and `css` prop dropped)
|
24 | * Drop jade dependency and related config
|
25 | * 0.0.11 adds option to remove line breaks
|
26 | * 0.0.10 adds components relative asset paths support (see Configuration)
|
27 | * 0.0.8 adds Jade support (add `jade: true` to your config) => __dropped in 1.0.0__
|
28 | * 0.0.6 adds support to style sheets
|
29 |
|
30 | ## TOC
|
31 |
|
32 | * [Installation](#installation)
|
33 | * [Configuration](#configuration)
|
34 | * [Options](#options)
|
35 | * [Processors configuration](#processors-configuration)
|
36 | * [Processor Examples](#processor-examples)
|
37 | * [Template function](#template-function)
|
38 | * [CustomFilePath configuration](#customfilepath-configuration)
|
39 | * [Example usage](#example-usage)
|
40 | * [Browserify transform example](#browserify-transform-example)
|
41 | * [How it works](#how-it-works)
|
42 | * [Contribute](#contribute)
|
43 | * [Contributors](#contributors)
|
44 | * [Todo](#todo)
|
45 | * [Licence](#licence)
|
46 |
|
47 | ## Installation
|
48 |
|
49 | ```bash
|
50 | npm install gulp-inline-ng2-template --save-dev
|
51 | ```
|
52 |
|
53 | ## Configuration
|
54 |
|
55 | ### Options
|
56 |
|
57 | You can pass a configuration object to the plugin.
|
58 | ```javascript
|
59 | defaults = {
|
60 | base: '/', // Angular2 application base folder
|
61 | target: 'es6', // Can swap to es5
|
62 | indent: 2, // Indentation (spaces)
|
63 | useRelativePaths: false // Use components relative assset paths
|
64 | removeLineBreaks: false // Content will be included as one line
|
65 | templateExtension: '.html', // Update according to your file extension
|
66 | templateFunction: false, // If using a function instead of a string for `templateUrl`, pass a reference to that function here
|
67 | templateProcessor: function (ext, file, callback) {/* ... */},
|
68 | styleProcessor: function (ext, file, callback) {/* ... */},
|
69 | customFilePath: function(ext, file) {/* ... */},
|
70 | supportNonExistentFiles: false // If html or css file do not exist just return empty content
|
71 | };
|
72 | ```
|
73 |
|
74 | ### Processors configuration
|
75 |
|
76 | ```typescript
|
77 | /**
|
78 | * Processor function call signature and type return
|
79 | *
|
80 | * @Param{String} file extension (type)
|
81 | * @Param{String} file content
|
82 | * @Param{Function} callback function (err, result) => void
|
83 | * @Return{void}
|
84 | */
|
85 | function processor(ext, file, cb) {
|
86 | // sync OR async implementation of your source files processing goes here ...
|
87 | cb(null, file);
|
88 | }
|
89 | ```
|
90 |
|
91 | ### Processor Examples
|
92 |
|
93 | Minify template file before inlining them
|
94 |
|
95 | ```javascript
|
96 | import inlineTemplate from 'gulp-inline-ng2-template';
|
97 | import htmlMinifier from 'html-minifier';
|
98 |
|
99 | const pluginOptions = {
|
100 | base: mySrcPath,
|
101 | templateProcessor: minifyTemplate
|
102 | };
|
103 |
|
104 | function minifyTemplate(ext, file, cb) {
|
105 | try {
|
106 | var minifiedFile = htmlMinifier.minify(file, {
|
107 | collapseWhitespace: true,
|
108 | caseSensitive: true,
|
109 | removeComments: true,
|
110 | removeRedundantAttributes: true
|
111 | });
|
112 | cb(null, minifiedFile);
|
113 | }
|
114 | catch (err) {
|
115 | cb(err);
|
116 | }
|
117 | }
|
118 | ```
|
119 |
|
120 | _Credit [@lcrodriguez](https://github.com/lcrodriguez)_
|
121 |
|
122 | ### Template function
|
123 |
|
124 | Inside your component: `templateUrl: templateFunc('app.html')`
|
125 |
|
126 | ```es6
|
127 | /**
|
128 | * Template function call signature and type return
|
129 | *
|
130 | * @Param{String} filename
|
131 | * @Return{String} returned filename
|
132 | */
|
133 | templateFunction: function (filename) {
|
134 | // ...
|
135 | return newFilename;
|
136 | }
|
137 | ```
|
138 |
|
139 | ### CustomFilePath configuration
|
140 |
|
141 | ```typescript
|
142 | /**
|
143 | * Custom function name call signature and type return
|
144 | *
|
145 | * @Param{String} file extension (type)
|
146 | * @Param{String} file path
|
147 | * @Return{String} returned file path updated
|
148 | */
|
149 | function customFilePath(ext, file) {
|
150 | return file;
|
151 | }
|
152 | ```
|
153 |
|
154 | ## Example usage
|
155 |
|
156 | ```javascript
|
157 | //...
|
158 | var inlineNg2Template = require('gulp-inline-ng2-template');
|
159 |
|
160 | var result = gulp.src('./app/**/*.ts')
|
161 | .pipe(inlineNg2Template({ base: '/app' }))
|
162 | .pipe(tsc());
|
163 |
|
164 | return result.js
|
165 | .pipe(gulp.dest(PATH.dest));
|
166 | ```
|
167 |
|
168 | ## Browserify transform example
|
169 |
|
170 | Example transform function to use with Browserify.
|
171 |
|
172 | ```javascript
|
173 | // ng2inlinetransform.js
|
174 | var ng2TemplateParser = require('gulp-inline-ng2-template/parser');
|
175 | var through = require('through2');
|
176 | var options = {target: 'es5'};
|
177 |
|
178 | function (file) {
|
179 | return through(function (buf, enc, next){
|
180 | ng2TemplateParser({contents: buf, path: file}, options)((err, result) => {
|
181 | this.push(result);
|
182 | process.nextTick(next);
|
183 | });
|
184 | });
|
185 | }
|
186 | ```
|
187 |
|
188 | ```javascript
|
189 | // gulp task
|
190 | return browserify('main.ts', {} )
|
191 | .add(config.angularApp.additionalFiles)
|
192 | .plugin(require('tsify'), {target: 'es5'})
|
193 | .transform('./ng2inlinetransform')
|
194 | .bundle()
|
195 | .pipe(gulp.dest(config.rootDirectory))
|
196 | ```
|
197 |
|
198 | _Thanks to [@zsedem](https://github.com/zsedem)_
|
199 |
|
200 | ## How it works
|
201 |
|
202 | __app.html__
|
203 | ```html
|
204 | <p>
|
205 | Hello {{ world }}
|
206 | </p>
|
207 | ```
|
208 |
|
209 | __app.css__
|
210 | ```css
|
211 | .hello {
|
212 | color: red;
|
213 | }
|
214 | ```
|
215 |
|
216 | __app.ts__
|
217 | ```javascript
|
218 | import {Component, View} from 'angular2/angular2';
|
219 | @Component({ selector: 'app' })
|
220 | @View({
|
221 | templateUrl: './app.html',
|
222 | styleUrls: ['./app.css'],
|
223 | directives: [CORE_DIRECTIVES]
|
224 | })
|
225 | class AppCmp {}
|
226 | ```
|
227 |
|
228 | __result (app.ts)__
|
229 | ```javascript
|
230 | import {Component, View} from 'angular2/angular2';
|
231 | @Component({ selector: 'app' })
|
232 | @View({
|
233 | template: `
|
234 | <p>
|
235 | Hello {{ world }}
|
236 | </p>
|
237 | `,
|
238 | styles: [`
|
239 | .hello {
|
240 | color: red;
|
241 | }
|
242 | `],
|
243 | directives: [CORE_DIRECTIVES]
|
244 | })
|
245 | class AppCmp {}
|
246 | ```
|
247 |
|
248 | ## Contribute
|
249 |
|
250 | ```bash
|
251 | git clone https://github.com/ludohenin/gulp-inline-ng2-template
|
252 | cd gulp-inline-ng2-template
|
253 | npm install
|
254 | npm run test-dev
|
255 | ```
|
256 |
|
257 | ## Contributors
|
258 |
|
259 | ![Contributors](https://webtask.it.auth0.com/api/run/wt-ludovic_henin-yahoo_com-0/contributors-list/ludohenin/gulp-inline-ng2-template.svg)
|
260 |
|
261 | ## Todo
|
262 |
|
263 | - [ ] Append styles into `styles` View config property if it exist
|
264 | - [ ] Add support for source maps
|
265 | - [ ] Add option `skipCommented`
|
266 |
|
267 | ## Licence
|
268 |
|
269 | MIT
|