-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (51 loc) · 1.6 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
'use strict'
const anymatch = require('anymatch')
const deepClone = require('@jsbits/deep-clone')
const jscc = require('jscc')
const flatten = require('flatten-brunch-map')
const reIgnore = /\b(?:bower_components|node_modules|vendor)\//
const rePattern = /\.(?:js|ts)x?$/
const hasProp = Object.prototype.hasOwnProperty
const parseOptions = (config) => {
const opts = deepClone(config.plugins && config.plugins.jscc || {})
opts.sourceMap = !!config.sourceMaps &&
(opts.sourceMap === true || opts.sourceMaps === true)
if (!hasProp.call(opts, 'keepLines')) {
opts.keepLines = true
}
if (!hasProp.call(opts, 'prefixes')) {
opts.prefixes = /(?:\/[/*]|<!--) ?/
}
return opts
}
class JsccPlugin {
constructor (config) {
const opts = parseOptions(config || {})
if (opts.sourceMap) {
this.canGenMap = anymatch(opts.sourceMapFor || rePattern)
}
if (opts.pattern) {
this.pattern = opts.pattern
}
this.ignored = anymatch(opts.ignore || reIgnore)
this.options = opts
}
compile (file) {
if (this.ignored(file.path)) {
return Promise.resolve(file)
}
return new Promise((resolve) => {
const opts = deepClone(this.options)
opts.sourceMap = opts.sourceMap && this.canGenMap(file.path)
const output = jscc(file.data, file.path, opts)
if (output.map) {
output.map.file = file.path
}
resolve(flatten(file, output.code, output.map))
})
}
}
JsccPlugin.prototype.brunchPlugin = true
JsccPlugin.prototype.pattern = rePattern
JsccPlugin.prototype.type = 'javascript'
module.exports = JsccPlugin