UNPKG

3.81 kBMarkdownView Raw
1# gulp-inline-ng2-template
2
3Inline 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
7This 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.
8
9Very convenient to unit test your component or bundle your components/application (avoid extra HTTP request and keeps your source clean).
10
11__note:__
12
13* 1.1.0 adds templateFunction when templateUrl is a function
14* 1.0.0 - __Breaking changes__
15 * Add suppport for processors (templates & styles)
16 * Refactor configuration object (`html` and `css` prop dropped)
17 * Drop jade dependency and related config
18* 0.0.11 adds option to remove line breaks
19* 0.0.10 adds components relative asset paths support (see Configuration)
20* 0.0.8 adds Jade support (add `jade: true` to your config) => __dropped in 1.0.0__
21* 0.0.6 adds support to style sheets
22
23# Installation
24
25```bash
26npm install gulp-inline-ng2-template --save-dev
27```
28
29# Configuration
30
31You can pass a configuration object to the plugin.
32```javascript
33defaults = {
34 base: '/', // Angular2 application base folder
35 target: 'es6', // Can swap to es5
36 indent: 2, // Indentation (spaces)
37 useRelativePaths: false // Use components relative assset paths
38 removeLineBreaks: false // Content will be included as one line
39 templateExtension: '.html', // Update according to your file extension
40 templateFunction: false, // If using a function instead of a string for `templateUrl`, pass a reference to that function here
41 templateProcessor: function ...,
42 styleProcessor: function ...
43};
44```
45
46## Preprocessors configuration
47
48```typescript
49/**
50 * Processor function call signature and type return
51 *
52 * @Param{String} file extension (type)
53 * @Param{String} file content
54 * @Return{String} returned file to be inlined
55 */
56function processor(ext, file) {
57 // sync implementation of your source files processing goes here ...
58 return file;
59}
60```
61
62## Template function
63
64Inside your component: `templateUrl: templateFunc('app.html')`
65
66```es6
67/**
68 * Template function call signature and type return
69 *
70 * @Param{String} filename
71 * @Return{String} returned filename
72 */
73templateFunction: function (filename) {
74 // ...
75 return newFilename;
76}
77```
78
79# Example usage
80
81```javascript
82//...
83var inlineNg2Template = require('gulp-inline-ng2-template');
84
85var result = gulp.src('./app/**/*.ts')
86 .pipe(inlineNg2Template({ base: '/app' }))
87 .pipe(tsc());
88
89return result.js
90 .pipe(gulp.dest(PATH.dest));
91```
92
93# How it works
94
95__app.html__
96```html
97<p>
98 Hello {{ world }}
99</p>
100```
101
102__app.css__
103```css
104.hello {
105 color: red;
106}
107```
108
109__app.ts__
110```javascript
111import {Component, View} from 'angular2/angular2';
112@Component({ selector: 'app' })
113@View({
114 templateUrl: './app.html',
115 styleUrls: ['./app.css'],
116 directives: [CORE_DIRECTIVES]
117})
118class AppCmp {}
119```
120
121__result (app.ts)__
122```javascript
123import {Component, View} from 'angular2/angular2';
124@Component({ selector: 'app' })
125@View({
126 template: `
127 <p>
128 Hello {{ world }}
129 </p>
130 `,
131 styles: [`
132 .hello {
133 color: red;
134 }
135 `],
136 directives: [CORE_DIRECTIVES]
137})
138class AppCmp {}
139```
140
141# Test
142
143```bash
144git clone https://github.com/ludohenin/gulp-inline-ng2-template
145cd gulp-inline-ng2-template
146npm install
147npm run test-dev
148```
149
150# Todo
151
152- [ ] Append styles into `styles` View config property if it exist
153- [ ] Add support for source maps
154- [ ] Add option `skipCommented`
155
156# Licence
157
158MIT