-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
240 lines (212 loc) · 5.47 KB
/
index.js
File metadata and controls
240 lines (212 loc) · 5.47 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict'
const EventEmitter = require('events')
const MDeps = require('css-module-deps')
const resolve = require('resolve')
const readonly = require('read-only-stream')
const topoSort = require('deps-topo-sort2')
const path = require('path')
const splicer = require('labeled-stream-splicer')
const Transform = require('stream').Transform
class Depsify extends EventEmitter {
constructor(entries, opts) {
super()
opts = opts || {}
if (typeof entries === 'string' || Array.isArray(entries)) {
opts = Object.assign({}, opts, {
entries: [].concat(opts.entries || [], entries),
})
} else {
opts = Object.assign({}, entries, opts)
}
this._options = opts
opts.basedir = opts.basedir || process.cwd()
this.pipeline = this._createPipeline(opts)
;[].concat(opts.transform).filter(Boolean)
.forEach(p => this.transform(p))
;[].concat(opts.processor).filter(Boolean)
.forEach(p => this.processor(p))
;[].concat(opts.entries).filter(Boolean)
.forEach(file => this.add(file, { basedir: opts.basedir }))
;[].concat(opts.plugin).filter(Boolean)
.forEach(p => this.plugin(p, { basedir: opts.basedir }))
}
_createPipeline(opts) {
this._mdeps = this._createDeps(opts)
this._mdeps.on('file', file => {
pipeline.emit('file', file)
this.emit('file', file)
})
this._mdeps.on('package', pkg => {
pipeline.emit('package', pkg)
this.emit('package', pkg)
})
this._mdeps.on('transform', (tr, file) => {
pipeline.emit('transform', tr, file)
this.emit('transform', tr, file)
})
// for factor-bundle
this._bpack = this.pack(opts)
let pipeline = splicer.obj([
'record', [ this._recorder() ],
'deps', [ this._mdeps ],
'syntax', [],
'sort', [ this._sort() ],
'dedupe', [],
'label', [ this._label() ],
'emit-deps', [ this._emitDeps() ],
'debug', [],
'pack', [ this._bpack ],
'wrap', [],
])
return pipeline
}
_createDeps(opts) {
return MDeps(Object.assign(
{}, opts, { transform: [], processor: [] }
))
}
_emitDeps() {
let self = this
return Transform({
objectMode: true,
transform: function (row, enc, next) {
self.emit('dep', row)
this.push(row)
next()
},
})
}
_recorder() {
let recorded = this._recorded = []
return Transform({
objectMode: true,
transform: function (row, enc, next) {
recorded.push(row)
next(null, row)
},
})
}
_sort() {
let rows = []
return Transform({
objectMode: true,
transform: function (row, enc, next) {
rows.push(row)
next()
},
flush: function (next) {
let ids = []
let idMap = rows.reduce(function (o, row) {
let id = row.id || row.file
o[id] = row
ids.push(id)
return o
}, {})
ids.sort().forEach((id, index) => {
idMap[id].index = index + 1
})
ids.forEach(id => {
let row = idMap[id]
row.indexDeps = Object.keys(row.deps || {}).reduce(function (o, dep) {
o[dep] = idMap[row.deps[dep]].index
return o
}, {})
this.push(row)
})
next()
},
})
}
_label() {
var self = this
return Transform({
objectMode: true,
transform: function (row, enc, next) {
let prev = row.id
row.id = row.index
row.deps = row.indexDeps
self.emit('label', prev, row.id)
next(null, row)
},
})
}
pack() {
let rows = []
return [
Transform({
objectMode: true,
transform: function (row, enc, next) {
rows.push(row)
next()
},
flush: function (next) {
rows.sort(function (a, b) {
return a.file < b.file ? -1 : 1
}).forEach(row => {
this.push(row)
})
next()
},
}),
topoSort(),
Transform({
objectMode: true,
transform: function (row, enc, next) {
next(null, row.source)
},
}),
]
}
add(file, opts) {
opts = opts || {}
let basedir = opts.basedir || this._options.basedir
if (Array.isArray(file)) {
file.forEach(f => this.add(f, opts))
} else if (typeof file === 'string') {
file = path.resolve(basedir, file)
this.pipeline.write({ file: file })
} else {
this.pipeline.write(Object.assign({ basedir: basedir }, file, opts))
}
return this
}
processor(processor) {
this.pipeline.write({ processor: processor })
return this
}
transform(tr) {
this.pipeline.write({ transform: tr })
return this
}
reset() {
this.pipeline = this._createPipeline(this._options)
this._bundled = false
this.emit('reset')
}
plugin(p, opts) {
if (Array.isArray(p)) {
opts = p[1]
p = p[0]
}
let basedir = opts && opts.basedir || this._options.basedir
if (typeof p === 'function') {
p(this, opts)
} else {
require(resolve.sync(p, { basedir: basedir }))(this, opts)
}
return this
}
bundle() {
if (this._bundled) {
let recorded = this._recorded
this.reset()
recorded.forEach(x => this.pipeline.write(x))
}
let output = readonly(this.pipeline)
this.emit('bundle', output)
this.pipeline.end()
this._bundled = true
return output
}
}
module.exports = Depsify