-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (168 loc) · 4.8 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var mixy = require('mixy')
var Transform = require('stream').Transform
var splicer = require('labeled-stream-splicer')
var File = require('vinyl')
var browserPack = require('browser-pack')
var createCustomFactory = require('./lib/custom-factor')
var BundleFactory = require('./lib/bundle-factory')
var createFileResolver = require('./lib/resolver')
module.exports = function (b, opts) {
var basedir = b._options.basedir || process.cwd()
var packOpts = mixy.mix({}, b._options, { raw: true, hasExports: true })
b.on('reset', reset)
reset()
function reset() {
var input = []
var output = through(
function (row, enc, next) { next() },
function () {}
)
b.pipeline.push(createPipeline(['output', output], 'common-bundle.wrap'))
b.pipeline.splice(b.pipeline.indexOf('record') + 1, 0, createPipeline([
'record', through(function (row, enc, next) {
if (row.file) {
input.push(row.file)
}
next(null, row)
}),
], 'common-bundle.record'))
b.pipeline.splice('pack', 0, createPipeline([
// group rows into bundles
'group', createBundleStream(opts, basedir, input),
// emit meta info about bundles and rows
'map', collectMaps({ basedir: basedir, input: input })
.on('map', b.emit.bind(b, 'common.map')),
// pack and create a vinyl object for each bundle
'vinylify', vinylify({ basedir: basedir, packOpts: packOpts, packer: browserPack })
.on('pipeline', b.emit.bind(b, 'common.pipeline')),
'wrap', through(
function (file, enc, next) {
output.push(file)
next()
},
function (next) {
output.push(null)
next()
}
),
], 'common-bundle.pack'))
}
}
function createBundleStream(opts, basedir, input) {
var factory = new BundleFactory({ basedir: basedir })
function write(row, enc, next) {
factory.addModule(row)
next()
}
function end(next) {
factory.start()
createCustomFactory(opts).call(factory, input, factory.rowMap)
factory.end()
var bundleMap = factory.getBundleMap()
factory.getBundles().forEach(function (b) {
var modules = []
bundleMap[b].modules.forEach(function (m) {
modules.push(factory.rowMap[m])
})
this.push({
file: b,
modules: modules,
deps: bundleMap[b].deps,
})
}, this)
next()
}
return through(write, end)
}
function through(write, end, label) {
var s = Transform({ objectMode: true })
s._transform = write
s._flush = end
if (label) {
s.label = label
}
return s
}
function vinylify(opts) {
var basedir = opts.basedir
var packOpts = opts.packOpts
var packer = opts.packer
return through(
function (bundle, enc, next) {
var packOptions = mixy.mix({}, packOpts, { to: bundle.file })
var pipeline = splicer.obj([
'pack', [ packer(packOptions) ],
'wrap', [],
])
this.emit('pipeline', bundle.file, pipeline)
this.push(new File({
contents: pipeline,
path: bundle.file,
base: basedir,
}))
bundle.modules.sort(function (a, b) {
return a.file < b.file ? -1 : 1
})
.forEach(function (row) {
pipeline.write(row)
})
pipeline.end()
next()
}
)
}
function collectMaps(opts) {
var input = opts.input
var basedir = opts.basedir
var bundles = []
var resolver = createFileResolver(basedir)
function write(bundle, enc, next) {
bundles.push(bundle)
next()
}
function end(next) {
// input won't be ready until the first bundle arrives
// relative bundle file path => { modules: [moduleID], deps: [bundlePath] }
var bundleMap = Object.create(null)
// absolute file path => id
var idMap = Object.create(null)
// id => [bundlePath]
var moduleMap = Object.create(null)
bundles.forEach(function (bundle) {
var relFile = resolver.relative(bundle.file)
var deps = bundle.deps.map(resolver.relative)
bundleMap[relFile] = {
modules: bundle.modules.map(function (row) {
if (!moduleMap[row.id]) {
moduleMap[row.id] = {
file: resolver.relative(row.file),
bundles: [deps.concat(relFile)],
}
} else {
moduleMap[row.id].bundles.push(deps.concat(relFile))
}
idMap[row.file] = row.id
return row.id
}),
deps: deps,
}
this.push(bundle)
}, this)
input = input.map(function (file) {
return idMap[file]
})
this.emit('map', {
bundles: bundleMap,
modules: moduleMap,
entries: input,
basedir: basedir,
})
next()
}
return through(write, end)
}
function createPipeline(streams, label) {
var res = splicer.obj(streams)
res.label = label
return res
}