-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
176 lines (143 loc) · 4.26 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
167
168
169
170
171
172
173
174
175
176
'use strict'
const entries = require('object.entries')
const escapeStringRegexp = require('escape-string-regexp')
const through = require('through2')
const BufferStreams = require('bufferstreams')
const getFileExt = require('path').extname
const PluginError = require('plugin-error')
const PLUGIN_NAME = 'gulp-remove-code'
const regexCache = new Map()
const extensions = new Map([
['.coffee', [['#', ''], ['###', '###']]],
['.css', [['/*', '*/']]],
['.html', [['<!--', '-->']]],
['.cshtml', [['@*', '*@'], ['<!--', '-->']]],
['.jade', [['//-', '']]],
['.js', [['//', ''], ['/*', '*/']]],
['.ts', [['//', ''], ['/*', '*/']]],
['.jsx', [['//', ''], ['/*', '*/']]],
['.tsx', [['//', ''], ['/*', '*/']]]
])
function applyReplacements (buffer, {commentTypes, conditions}) {
let contents = buffer.toString()
if (buffer.length > 0) {
for (const [key, value] of conditions) {
for (const [commentStart, commentEnd] of commentTypes) {
const regex = getRemovalTagsRegExp(commentStart, commentEnd, key)
contents = contents.replace(regex, function (ignore, original, capture) {
const not = (capture === '!')
return (value ^ not) ? '' : original
})
}
}
}
return Buffer.from(contents)
}
/**
* @param {string} commentStart
* @param {string} commentEnd
* @param {string} key
* @returns {RegExp}
*/
function getRemovalTagsRegExp (commentStart, commentEnd, key) {
const cacheKey = `${commentStart}${commentEnd}${key}`
if (regexCache.has(cacheKey)) {
return regexCache.get(cacheKey)
}
const escapedCommentStart = escapeStringRegexp(commentStart)
const escapedKey = escapeStringRegexp(key)
const escapedCommentEnd = escapeStringRegexp(commentEnd)
const pattern = [
'(',
escapedCommentStart,
'\\s*removeIf\\((!?)',
escapedKey,
'\\)\\s*',
escapedCommentEnd,
'\\s*' +
'(\\n|\\r|.)*?',
escapedCommentStart,
'\\s*endRemoveIf\\((!?)',
escapedKey,
'\\)\\s*',
escapedCommentEnd,
')'
].join('')
const re = new RegExp(pattern, 'gi')
regexCache.set(cacheKey, re)
return re
}
// --------------------------------------------------------------------------------------------------
module.exports = function (options) {
options = Object.assign({}, options)
options.conditions = []
for (const condition of entries(options)) {
if (condition[0] !== 'commentStart' && condition[0] !== 'commentEnd') {
options.conditions.push(condition)
}
}
return through.obj(function (file, enc, callback) {
const stream = this
options = prepareOptions(file, options)
file.contents = getFileContents(file, options, stream)
stream.push(file)
callback()
})
}
function getFileContents (file, options, stream) {
if (file.isNull()) {
return file.contents
}
if (file.isBuffer()) {
return getBufferContents(file, options, stream)
}
if (file.isStream()) {
return getStreamContents(file, options, stream)
}
}
function getBufferContents (file, options, stream) {
const parsed = removeCode(file, file.contents, options)
if (parsed instanceof PluginError) {
stream.emit('error', parsed)
return Buffer.from('')
}
return parsed
}
function getStreamContents (file, options, stream) {
const streamer = new BufferStreams(function (err, buf, callback) {
if (err) {
stream.emit('error', getError(err))
return callback()
}
const parsed = removeCode(file, buf, options)
if (parsed instanceof PluginError) {
stream.emit('error', parsed)
return callback()
}
callback(null, parsed)
})
return file.contents.pipe(streamer)
}
function removeCode (file, buf, options) {
try {
const result = applyReplacements(buf, options)
return Buffer.from(result)
} catch (error) {
return getError(error)
}
}
function prepareOptions (file, options) {
if (!file.isNull()) {
if (!options.commentStart) {
// Detect comment tokens
const fileExt = getFileExt(file.path)
options.commentTypes = extensions.has(fileExt) ? extensions.get(fileExt) : [['//', '']]
} else {
options.commentTypes = [[options.commentStart, options.commentEnd || '']]
}
}
return options
}
function getError (error) {
return new PluginError(PLUGIN_NAME, error, {showStack: true})
}