-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
79 lines (61 loc) · 2.02 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
const readJSON = require('read-package-json')
const multipipe = require('multipipe')
const from2 = require('from2-array')
const resolve = require('resolve')
const map = require('map-limit')
const findup = require('findup')
const path = require('path')
const bl = require('bl')
module.exports = loader
function loader (source) {
const filename = this.resourcePath
const dirname = this.context
const done = this.async()
const self = this
this.cacheable(true)
findup(dirname, 'package.json', foundPackage)
function foundPackage (err, pkgDir) {
if (err) return done(err)
if (!pkgDir) return done(null, source)
const pkgFile = path.join(pkgDir, 'package.json')
readJSON(pkgFile, function (err, json) {
if (err) return done(err)
const pkgTransforms = [].concat(
json.browserify && json.browserify.transform
).filter(Boolean)
map(pkgTransforms, 10, function (transform, next) {
transform = Array.isArray(transform) ? transform : [transform]
const name = transform[0]
const opts = transform[1] || {}
opts._flags = opts._flags || []
if (typeof name === 'function') {
return next(null, name(filename, opts))
}
resolve(name, {
basedir: pkgDir
}, function (err, name) {
if (err) return next(err)
const TransformStream = require(name)
if (typeof TransformStream !== 'function') {
return next(new Error(
'Browserify transform at ' + name + ' did not export a function'
))
}
next(null, TransformStream(filename, opts))
})
}, function (err, transforms) {
if (err) return done(err)
transforms.forEach(function (tr) {
tr.on('file', function (file) {
self.addDependency(file)
})
})
transforms = []
.concat(from2([source]))
.concat(transforms)
multipipe.apply(this, transforms)
.pipe(bl(done))
})
})
}
}