diff --git a/README.md b/README.md index 54bf182..ec0a1de 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.js b/src/index.js index c1f0704..df97c0e 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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}); };