-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
73 lines (59 loc) · 2.12 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import path from 'node:path'
import debugUtil from 'debug'
import pug from 'pug'
import DEFAULT_PUG_OPTIONS from './defaults.js'
const debug = debugUtil('Eleventy:Plugins:Pug')
const debugDev = debugUtil('Dev:Eleventy:Plugins:Pug')
export default {
outputFileExtension: 'html',
options: DEFAULT_PUG_OPTIONS,
async init() {
debug('Plugin initialized.')
debugDev('%O', this)
},
/**
* Provides runtime Pug compilation for Eleventy.
* @param {String} inputSource - Source of the input file
* @param {String} inputPath - Path of the input file
* @returns {String} The resulting code returned by Pug (currently, `pug.render()`).
*/
async compile(inputSource, inputPath) {
debugDev('inputSource: %s', inputSource)
debugDev('inputPath: %s', inputPath)
// @TODO: Determine if Eleventy provides a built-in way to
// detect when rendering a layout.
// sometimes this is a layout, in which case it will
// not be the same as `page.inputPath` (see `page`, below).
//
// const filename = path.resolve('.', inputPath)
// debug('compile resolved inputPath to filename: %s', filename)
// @TODO: Register Pug template dependencies for caching & performance
// @see https://www.11ty.dev/docs/languages/custom/#registering-dependencies
/** @param {Object} arg - Provided by Eleventy at runtime */
return async function(arg) {
debugDev('rendering... received arg: %O', arg)
debug( 'about to render... inputPath: %O', inputPath)
// TODO: clean up the setup and updating of options
const renderOptions = Object.assign(
{},
{
basedir: arg.eleventy.directories.includes,
filename: inputPath,
},
arg
)
// console.log('\n\nrenderOptions: %O\n\n', renderOptions)
/*
* Using `pug.render()` is the simplest path to get this plugin working.
*
* However:
* - `pug.compile()` returns a function that can be cached,
* _and_ that function has a `.dependencies` property!
*
* - @see: `CustomEngine.getCompileCacheKey()`
* - would this actually speed up build times for large projects?
*/
return pug.render(inputSource, renderOptions)
}
}
}