UNPKG

3.53 kBMarkdownView Raw
1# angular2-template-loader
2Chain-to loader for webpack that inlines all html and style's in angular2 components.
3
4[![Build Status](https://travis-ci.org/TheLarkInn/angular2-template-loader.svg?branch=master)](https://travis-ci.org/TheLarkInn/angular2-template-loader)
5[![Coverage](https://codecov.io/gh/TheLarkInn/angular2-template-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/TheLarkInn/angular2-template-loader)
6[![Taylor Swift](https://img.shields.io/badge/secured%20by-taylor%20swift-brightgreen.svg)](https://twitter.com/SwiftOnSecurity)
7
8### Quick Links
9- [Installation](#installation)
10- [Requirements](#requirements)
11- [Example usage](#example-usage)
12- [How does it work](#how-does-it-work)
13- [Common Issues](#common-issues)
14
15### Installation
16Install the webpack loader from [npm](https://www.npmjs.com/package/angular2-template-loader).
17- `npm install angular2-template-loader --save-dev`
18
19Chain the `angular2-template-loader` to your currently used typescript loader.
20
21```js
22loaders: ['awesome-typescript-loader', 'angular2-template-loader'],
23```
24
25### Requirements
26To be able to use the template loader you must have a loader registered, which can handle `.html` and `.css` files.
27> The most recommended loader is [`raw-loader`](https://github.com/webpack/raw-loader)
28
29This loader allows you to decouple templates from the component file and maintain AoT compilation. This is particularly useful when building complex components that have large templates.
30
31### Example Usage
32
33#### Webpack
34Here is an example markup of the `webpack.config.js`, which chains the `angular2-template-loader` to the `tsloader`
35
36```js
37module: {
38 loaders: [
39 {
40 test: /\.ts$/,
41 loaders: ['awesome-typescript-loader', 'angular2-template-loader?keepUrl=true'],
42 exclude: [/\.(spec|e2e)\.ts$/]
43 },
44 /* Embed files. */
45 {
46 test: /\.(html|css)$/,
47 loader: 'raw-loader',
48 exclude: /\.async\.(html|css)$/
49 },
50 /* Async loading. */
51 {
52 test: /\.async\.(html|css)$/,
53 loaders: ['file?name=[name].[hash].[ext]', 'extract']
54 }
55 ]
56}
57```
58
59#### Angular
60```js
61@Component({
62 selector: 'awesome-button',
63 template: require('./button.template.html'),
64 styles: [require('./button.style.css')]
65})
66export class AwesomeButtonComponent { }
67```
68
69### How does it work
70The `angular2-template-loader` searches for `templateUrl` and `styleUrls` declarations inside of the Angular 2 Component metadata and replaces the paths with the corresponding `require` statement.
71If `keepUrl=true` is added to the loader's query string, `templateUrl` and `styleUrls` will not be replaced by `template` and `style` respectively so you can use a loader like `file-loader`.
72
73The generated `require` statements will be handled by the given loader for `.html` and `.js` files.
74
75### Common Issues
76In some cases the webpack compilation will fail due to unknown `require` statements in the source.<br/>
77This is caused by the [way the template loader works](#how-does-it-work).
78
79> The Typescript transpiler doesn't have any typings for the `require` method, which was generated by the loader.
80
81We recommend the installation of type defintions, which contain a declaration of the `require` method.
82- [@types/node](https://www.npmjs.com/package/@types/node) | [dt~node](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/node.d.ts)
83- [@types/requirejs](https://www.npmjs.com/package/@types/requirejs) | [dt~requirejs](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/requirejs)