-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
62 lines (50 loc) · 1.24 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
var loaderUtils = require('loader-utils')
var wrapped = require('wrapped')
var map = {
'\\b': '\b',
'\\f': '\f',
'\\n': '\n',
'\\r': '\r',
'\\t': '\t',
'\\v': '\v',
'\\"': '"',
"\\'": "'",
'\\\\': '\\'
}
module.exports = function(source) {
var ctx = this
ctx.cacheable && ctx.cacheable()
source = JSON.stringify(source)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
.replace(/^"|"$/g, '')
.replace(/\\(.)/g, function(char) {
return map[char] || char
})
var options = loaderUtils.getOptions(ctx) || {}
var middleware = [].concat(options.middleware || [])
if (options.async) {
var callback = ctx.async()
var i = 0
var l = middleware.length
function next(err, source) {
if (err) return ctx.emitError(err)
if (i === l) {
if (!options.raw) {
source = 'module.exports = ' + JSON.stringify(source)
}
return callback(null, source)
}
wrapped(middleware[i++]).apply(ctx, [source, next])
}
next(null, source)
} else {
middleware.forEach(fn => {
source = fn.apply(ctx, [source])
})
if (!options.raw) {
source = 'module.exports = ' + JSON.stringify(source)
}
return source
}
}