forked from emanuelet/hapi-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (144 loc) · 5.34 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict'
var Boom = require('boom'),
sass = require('node-sass'),
Hoek = require('hoek'),
fs = require('fs'),
dirname = require('path').dirname,
mkdirp = require('mkdirp'),
join = require('path').join;
const {
promisify
} = require('util');
const sassRenderAsync = promisify(sass.render);
var internals = {
defaults: {
/* https://github.com/sass/node-sass#options */
debug: false,
force: false,
src: './lib/sass',
dest: './public/css',
routePath: '/css/{file}.css',
outputStyle: 'compressed',
sourceComments: 'none',
srcExtension: 'scss'
},
error: function (h, err) {
if (err.code == 'ENOENT') {
return Boom.notFound();
} else {
return Boom.internal(err);
}
},
log: function () {
var args = Array.prototype.slice.call(arguments);
args[0] = '[hapi-sass] ' + args[0];
console.log.apply(console, args)
}
};
exports.plugin = {
register: (server, options) => {
var settings = Hoek.applyToDefaults(internals.defaults, options);
// Force compilation
var force = settings.force;
// Debug option
var debug = settings.debug;
// Source dir required
var src = settings.src;
if (!src) {
return Boom.badRequest('hapi-sass requires "src" directory');
}
// Default dest dir to source
var dest = settings.dest ? settings.dest : src;
server.route({
method: 'GET',
path: settings.routePath,
config: {
files: {
relativeTo: './'
}
},
handler: async function (request, h) {
var cssPath = join(dest, request.params.file + '.css'),
sassPath = join(src, request.params.file + '.' + settings.srcExtension),
sassDir = dirname(sassPath);
if (debug) {
internals.log("Processing Request with values: %j", {
"sassPath": sassPath,
"dest": dest,
"sassDir": sassDir
})
}
var compile = async () => {
if (debug) {
internals.log('Compiling Sass at %s', sassPath);
}
var results = await sassRenderAsync({
file: sassPath,
includePaths: [sassDir].concat(settings.includePaths || []),
imagePath: settings.imagePath,
outputStyle: settings.outputStyle,
functions: settings.functions,
sourceComments: settings.sourceComments
});
let err = results.err;
if (err) {
if (debug) {
let message = err.formatted ? err.formatted : err.message;
internals.log('Compilation failed: %s', message);
}
return internals.error(h, err);
}
if (debug) {
internals.log('Compilation ok');
}
let errMk = await mkdirp(dirname(cssPath), 0x1c0);
if (errMk) {
return errMk;
}
let errFs = await fs.writeFile(cssPath, results.css, 'utf8');
if (errFs && debug) {
internals.log("Error writing file - %s", errFs.message);
}
return h.response(results.css).type('text/css');
};
if (force) {
return compile();
}
let [errFs1, sassStats] = await fs.stat(sassPath);
if (errFs1) {
return internals.error(h, errFs1);
}
let [errFs2, cssStats] = await fs.stat(cssPath);
if (errFs2) {
if (errFs2.code == 'ENOENT') {
// css has not been compiled
if (debug) {
internals.log('Compiled file not found, compiling %s', cssPath);
}
compile();
} else {
internals.error(h, errFs2);
}
} else { // compiled version exists, check mtimes
if (sassStats.mtime.getTime() > cssStats.mtime.getTime()) { // the sass version is newer
if (debug) {
internals.log('Sass file is newer, compiling %s', cssPath);
}
compile();
} else {
// serve
if (debug) {
internals.log('Compiled file found and up to date. Serving');
}
return h.file(cssPath);
}
}
}
});
},
multiple: true,
dependencies: 'inert',
name: 'hapi-sass',
pkg: require('./package.json'),
version: require('./package.json').version
};