-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
71 lines (61 loc) · 2.64 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
/*jslint node:true */
"use strict";
var projectName = require("./package.json").name;
var loaderUtils = require("loader-utils");
var regexCharsRegex = /[-[\]{}()*+?.,\\^$\/|#]/g;
function escapeRegexChars(comment) {
return comment && regexCharsRegex.test(comment)
? comment.replace(regexCharsRegex, '\\$&')
: comment;
}
function StripBlockLoader(content) {
var options = loaderUtils.getOptions(this) || {};
var startComment = options.start || 'develblock:start';
var endComment = options.end || 'develblock:end';
var removeOuterWhitespace = options.removeOuterWhitespace === true;
var omitReplacementMarker = options.omitReplacementMarker === true;
var startWhitespaceMatcher = "";
var endWhitespaceMatcher = "";
var whitespaceMatcher = "[\\t ]*";
// in case there are any regex chars in the comment string itself
startComment = escapeRegexChars(startComment);
endComment = escapeRegexChars(endComment);
// if removeOuterWhitespace is true, remove whitespace on
// the line outside of the comment tags to start and end
// the dev block
if (removeOuterWhitespace) {
startWhitespaceMatcher = "^" + whitespaceMatcher;
endWhitespaceMatcher = whitespaceMatcher + "\\n?"
}
var regexPattern = new RegExp(startWhitespaceMatcher +
"\\/\\* ?" + startComment + " ?\\*\\/[\\s\\S]*?\\/\\* ?" +
endComment + " ?\\*\\/" + endWhitespaceMatcher, "g");
var startWhitespaceMatcher = "";
var endWhitespaceMatcher = "";
var whitespaceMatcher = "[\\t ]*"
// if removeOuterWhitespace is true, remove whitespace on
// the line outside of the comment tags to start and end
// the dev block
if (removeOuterWhitespace) {
startWhitespaceMatcher = "^" + whitespaceMatcher;
endWhitespaceMatcher = whitespaceMatcher + "\\n?"
}
var regexPattern = new RegExp(startWhitespaceMatcher +
"\\/\\* ?" + startComment + " ?\\*\\/[\\s\\S]*?\\/\\* ?" +
endComment + " ?\\*\\/" + endWhitespaceMatcher, "g");
// format the replacement comment str, but omit the replacement
// comment entirely if empty string is passed in as the value
// for the 'replacementText' option
var replacement = (typeof options.replacementText === 'string')
? escapeRegexChars(options.replacementText)
: (omitReplacementMarker ? '' : (projectName + ':removed'));
replacement = (!!replacement)
? ('/* ' + replacement + ' */')
: '';
content = content.replace(regexPattern, replacement);
if (this.cacheable) {
this.cacheable(true);
}
return content;
}
module.exports = StripBlockLoader;