Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ module: {
}
```

## Including templates

The loader adds a `def.loadfile(filename)` function to include and compile doT templates.

For example, a `parts.dot` file:

```
{{##def.works:
{{='It works!'}}
#}}
```

And some other template file:

```
{{#def.loadfile('parts.dot')}}
{{#def.works}}
```

## Similar projects

[`dot-loader`](https://github.com/ross-pfahler/dot-loader) is another loader
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs';
import path from 'path';

import doT from 'dot';
import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';
Expand Down Expand Up @@ -40,5 +43,14 @@ export default function (source) {

doT.templateSettings.selfcontained = true;

return 'export default ' + doT.template(source);
var webpackContext = this;

function loadfile (filename) {
const filepath = path.resolve(webpackContext.context, filename);
webpackContext.addDependency(filepath);

return fs.readFileSync(filepath);
}

return 'export default ' + doT.template(source, undefined, {loadfile: loadfile});
};