Skip to content

Commit b0342a6

Browse files
author
Vlad Mashkin
committed
Initial commit
0 parents  commit b0342a6

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Vladyslav Mashkin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, 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,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# lodash-template-loader
2+
Lodash template loader for Webpack.
3+
4+
This is tiny webpack loader which wraps lodash `_.template()` to render templates during webpack build.
5+
Global data for templates (views) is passed using `imports` property of loader options object.
6+
7+
Other available options:
8+
- [options.escape] (RegExp): The HTML "escape" delimiter.
9+
- [options.evaluate] (RegExp): The "evaluate" delimiter.
10+
- [options.interpolate] (RegExp): The "interpolate" delimiter.
11+
12+
### Install
13+
14+
```sh
15+
$ npm i lodash-template-loader --save
16+
```
17+
18+
### Usage
19+
20+
WebPack 2.x
21+
```javascript
22+
module: {
23+
rules: [ {
24+
test: /\.html$/,
25+
use: [
26+
{
27+
loader: 'apply-loader'
28+
},
29+
{
30+
loader: 'lodash-template-loader',
31+
options: {
32+
imports: { // data object which will be passed to templates for rendering
33+
title: 'Hello World!'
34+
}
35+
}
36+
}
37+
]
38+
} ]
39+
}
40+
```

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const loaderUtils = require('loader-utils');
5+
6+
module.exports = function (source) {
7+
const options = loaderUtils.getOptions(this) || {};
8+
9+
if (_.isRegExp(_.templateSettings.escape)) {
10+
_.templateSettings.escape = options.escape;
11+
}
12+
if (_.isRegExp(_.templateSettings.evaluate)) {
13+
_.templateSettings.evaluate = options.evaluate;
14+
}
15+
if (_.isRegExp(_.templateSettings.interpolate)) {
16+
_.templateSettings.interpolate = options.interpolate;
17+
}
18+
if (_.isObject(options.imports)) {
19+
_.templateSettings.imports = options.imports;
20+
}
21+
22+
const compiled = _.template(source);
23+
24+
return 'module.exports = ' + JSON.stringify(compiled) + ';';
25+
};

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "lodash-template-loader",
3+
"version": "1.0.0",
4+
"description": "Lodash template loader for Webpack",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/jsdream/lodash-template-loader.git"
12+
},
13+
"keywords": [
14+
"webpack",
15+
"lodash",
16+
"template",
17+
"loader"
18+
],
19+
"author": "Vladyslav Mashkin",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/jsdream/lodash-template-loader/issues"
23+
},
24+
"homepage": "https://github.com/jsdream/lodash-template-loader#readme",
25+
"dependencies": {
26+
"lodash": "^4.17.5"
27+
}
28+
}

0 commit comments

Comments
 (0)