-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
34 lines (25 loc) · 1016 Bytes
/
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
const createFilter = require('rollup-pluginutils').createFilter
const MagicString = require('magic-string')
const START_COMMENT = 'start_comment'
const END_COMMENT = 'end_comment'
function stripCode (options = {}) {
const { include, exclude, sourceMap, pattern, sourcemap } = options
const filter = createFilter(include, exclude)
return {
name: 'stripCode',
transform (source, id) {
if (!filter(id)) return
const startComment = options.start_comment || START_COMMENT
const endComment = options.end_comment || END_COMMENT
const defaultPattern = new RegExp(`([\\t ]*\\/\\* ?${startComment} ?\\*\\/)[\\s\\S]*?(\\/\\* ?${endComment} ?\\*\\/[\\t ]*\\n?)`, 'g')
let map
const code = source.replace(pattern || defaultPattern, '')
if (sourceMap !== false && sourcemap !== false) {
const magicString = new MagicString(code)
map = magicString.generateMap({ hires: true })
}
return { code, map }
}
}
}
module.exports = stripCode