• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

HISTORY.mdD24-Sep-2023358 2012

LICENSED24-Sep-20231 KiB1916

README.mdD24-Sep-20234 KiB10673

index.jsD24-Sep-20233 KiB10286

package.jsonD24-Sep-2023598 2726

README.md

1# pug-load
2
3The pug loader is responsible for loading the depenendencies of a given pug file.  It adds `fullPath` and `str` properties to every `Include` and `Extends` node.  It also adds an `ast` property to any `Include` nodes that are loading pug and any `Extends` nodes.  It then recursively loads the dependencies of any of those included files.
4
5[![Build Status](https://img.shields.io/travis/pugjs/pug-load/master.svg)](https://travis-ci.org/pugjs/pug-load)
6[![Dependencies Status](https://david-dm.org/pugjs/pug/status.svg?path=packages/pug-load)](https://david-dm.org/pugjs/pug?path=packages/pug-load)
7[![DevDependencies Status](https://david-dm.org/pugjs/pug/dev-status.svg?path=packages/pug-load)](https://david-dm.org/pugjs/pug?path=packages/pug-load&type=dev)
8[![NPM version](https://img.shields.io/npm/v/pug-load.svg)](https://www.npmjs.org/package/pug-load)
9[![Coverage Status](https://img.shields.io/codecov/c/github/pugjs/pug-load.svg)](https://codecov.io/gh/pugjs/pug-load)
10
11## Installation
12
13    npm install pug-load
14
15## Usage
16
17```js
18var load = require('pug-load');
19```
20
21### `load(ast, options)`
22### `load.string(str, filename, options)`
23### `load.file(filename, options)`
24
25Loads all dependencies of the Pug AST. `load.string` and `load.file` are syntactic sugar that parses the string or file instead of you doing it yourself.
26
27`options` may contain the following properties:
28
29- `lex` (function): **(required)** the lexer used
30- `parse` (function): **(required)** the parser used
31- `resolve` (function): a function used to override `load.resolve`. Defaults to `load.resolve`.
32- `read` (function): a function used to override `load.read`. Defaults to `load.read`.
33- `basedir` (string): the base directory of absolute inclusion. This is **required** when absolute inclusion (file name starts with `'/'`) is used. Defaults to undefined.
34
35The `options` object is passed to `load.resolve` and `load.read`, or equivalently `options.resolve` and `options.read`.
36
37### `load.resolve(filename, source, options)`
38
39Callback used by `pug-load` to resolve the full path of an included or extended file given the path of the source file.
40
41`filename` is the included file. `source` is the name of the parent file that includes `filename`.
42
43This function is not meant to be called from outside of `pug-load`, but rather for you to override.
44
45### `load.read(filename, options)`
46
47Callback used by `pug-load` to return the contents of a file.
48
49`filename` is the file to read.
50
51This function is not meant to be called from outside of `pug-load`, but rather for you to override.
52
53### `load.validateOptions(options)`
54
55Callback used `pug-load` to ensure the options object is valid. If your overriden `load.resolve` or `load.read` uses a different `options` scheme, you will need to override this function as well.
56
57This function is not meant to be called from outside of `pug-load`, but rather for you to override.
58
59### Example
60
61```js
62var fs = require('fs');
63var lex = require('pug-lexer');
64var parse = require('pug-parser');
65var load = require('pug-load');
66
67// you can do everything very manually
68
69var str = fs.readFileSync('bar.pug', 'utf8');
70var ast = load(parse(lex(str, 'bar.pug'), 'bar.pug'), {
71  lex: lex,
72  parse: parse,
73  resolve: function (filename, source, options) {
74    console.log('"' + filename + '" file requested from "' + source + '".');
75    return load.resolve(filename, source, options);
76  }
77});
78
79// or you can do all that in just two steps
80
81var str = fs.readFileSync('bar.pug', 'utf8');
82var ast = load.string(str, 'bar.pug', {
83  lex: lex,
84  parse: parse,
85  resolve: function (filename, source, options) {
86    console.log('"' + filename + '" file requested from "' + source + '".');
87    return load.resolve(filename, source, options);
88  }
89});
90
91// or you can do all that in only one step
92
93var ast = load.file('bar.pug', {
94  lex: lex,
95  parse: parse,
96  resolve: function (filename, source, options) {
97    console.log('"' + filename + '" file requested from "' + source + '".');
98    return load.resolve(filename, source, options);
99  }
100});
101```
102
103## License
104
105  MIT
106