-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.28 KB
/
index.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
'use strict'
const fs = require('fs')
const Path = require('path')
const less = require('less')
const fp = require('fastify-plugin')
function fastifyLess (fastify, opts, next) {
if (opts.prefix === undefined) {
opts.prefix = '/css'
}
let cache = {}
let len = opts.prefix.length + 1
fastify.get(opts.prefix + '/*', (request, reply) => {
const path = request.raw.url.substr(len)
const f = Path.join(opts.root, Path.basename(path, '.css') + '.less')
if (opts.cache && cache[f]) {
return reply.header('content-type', 'text/css').send(cache[f])
}
fs.readFile(
f,
{encoding: 'utf8'},
(err, res) => {
if (err) {
return reply.code(404)
.header('content-type', 'text/plain')
.send('not found')
}
less.render(res, {
paths: [opts.root]
}, (err, res) => {
if (err) {
return reply.code(500)
.header('content-type', 'text/plain')
.send(err.message)
}
if (opts.cache && !cache[f]) {
cache[f] = res.css
}
reply.header('content-type', 'text/css').send(res.css)
})
})
})
next()
}
module.exports = fp(fastifyLess, {
fastify: '>= 1.2.0',
name: 'fastify-staticless'
})