Skip to content

Commit

Permalink
Added baggage example and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
WearyMonkey committed Mar 7, 2015
1 parent 4cecb07 commit 8463e36
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
node_modules
.idea
test/test.js
test/npm-debug.log
test/npm-debug.log
bundle.js
55 changes: 54 additions & 1 deletion README.md
Expand Up @@ -13,6 +13,8 @@ or raw-loader. This gives you the flexibility to pick and choose your HTML loade
ngTemplate loader will export the path of the HTML file, so you can use require directly AngularJS with templateUrl parameters e.g.

``` javascript
require('ngtemplate!html!./test.html');

app.directive('testDirective', function() {
return {
restrict: 'E',
Expand All @@ -21,6 +23,8 @@ app.directive('testDirective', function() {
});
```

To remove the extra `require`, check out the [Baggage Example](#baggage-example) below.

ngTemplate creates a JS module that initialises the $templateCache with the HTML under the file path e.g.

``` javascript
Expand Down Expand Up @@ -78,7 +82,7 @@ require("!ngtemplate?module=myTemplates&relativeTo=/projects/test/app!html!file.

Make sure you use the same path separator for the `prefix` and `relativeTo` parameters, all templateUrls and in your webpack.config.js file.

## webpack config
## Webpack Config

It's recommended to adjust your `webpack.config` so `ngtemplate!html!` is applied automatically on all files ending on `.html`:

Expand All @@ -97,6 +101,55 @@ module.exports = {

Then you only need to write: `require("file.html")`.

## Baggage Example

ngTemplate loader works well with the [Baggage Loader](https://github.com/deepsweet/baggage-loader) to remove all those
extra HTML and CSS requires. See an example of a directive and webpack.config.js below. Or take a look at more complete
example in the examples/baggage folder.

With a folder structure:

app/
├── app.js
├── index.html
├── webpack.config.js
└── my-directive/
├── my-directive.js
├── my-directive.css
└── my-directive.html

and a webpack.config.js like:

``` javascript
module.exports = {
module: {
preLoaders: [
{
test: /\.js$/,
loader: 'baggage?[file].html&[file].css'
}
],
loaders: [
{
test: /\.html$/,
loader: "ngtemplate?relativeTo=" + __dirname + "/!html"
}
]
}
};
```

You can now skip the initial require of html and css like so:

``` javascript
app.directive('myDirective', function() {
return {
restrict: 'E',
templateUrl: require('./my-directive.html')
}
});
```

## Install

`npm install ngtemplate-loader`
Expand Down
14 changes: 14 additions & 0 deletions examples/baggage/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js"></script>
<script src="bundle.js"></script>
</head>
<body ng-app="baggageExample">

<my-directive></my-directive>

</body>
</html>
1 change: 1 addition & 0 deletions examples/baggage/my-directive/my-directive.html
@@ -0,0 +1 @@
hello {{foo}}
10 changes: 10 additions & 0 deletions examples/baggage/my-directive/my-directive.js
@@ -0,0 +1,10 @@
angular.module('baggageExample', [])
.directive('myDirective', function() {
return {
restrict: 'E',
templateUrl: require('./my-directive.html'),
link: function(scope) {
scope.foo = 'world';
}
}
});
23 changes: 23 additions & 0 deletions examples/baggage/webpack.config.js
@@ -0,0 +1,23 @@
var path = require('path');
require('../../index.js');

module.exports = {
entry: {
test: path.join(__dirname, "my-directive/my-directive.js")
},
module: {
preLoaders: [
{ test: /\.js$/, loader: 'baggage?[file].html' }
],
loaders: [
// replace ../../../index.js with ngtemplate
{ test: /\.html$/, loader: "../../../index.js?relativeTo=" + __dirname + "!html" }
]
},
output: {
path: __dirname,
publicPath: "/",
filename: "bundle.js",
sourceMapFilename: "bundle.map"
}
};
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -24,11 +24,12 @@
],
"dependencies": {
"jsesc": "^0.5.0",
"loader-utils": "~0.2.2"
"loader-utils": "~0.2.6"
},
"devDependencies": {
"baggage-loader": "^0.2.1",
"html-loader": "^0.2.2",
"raw-loader": "^0.5.1",
"webpack": "^1.2.0"
"webpack": "^1.7.0"
}
}
9 changes: 9 additions & 0 deletions test/src/baggage-test/baggage-test.js
@@ -0,0 +1,9 @@
angular.module('testModule')
.directive('baggage', function() {

return {
restrict: 'E',
templateUrl: require('../../index.js!html-loader!')
}

});
Empty file.

0 comments on commit 8463e36

Please sign in to comment.