-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
136 lines (128 loc) · 3.67 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
const exorcist = require('exorcist')
const zlib = require('zlib')
const fs = require('fs-extra')
const browserify = require('browserify')
const watchify = require('watchify')
const Uglify = require('uglify-js')
const glob = require('glob')
const path = require('path')
const EventEmitter = require('events')
const commonShake = require('common-shakeify')
module.exports = function init (opts) {
['fileName', 'source', 'dest'].forEach(prop => {
if (!opts.hasOwnProperty(prop)) {
throw new TypeError('You must provide a ' + prop + ' property in options')
}
})
opts.source = path.resolve(opts.source)
opts.dest = path.resolve(opts.dest)
opts.emitter = new EventEmitter()
glob(opts.source + '/**/' + opts.fileName, {}, (err, files) => {
if (err) opts.emitter.emit('error', err)
opts.compiledCount = 0
opts.total = files.length
files.forEach(inputPath => {
opts.emitter.emit('found', inputPath)
const outputPath = path.join(opts.dest, path.relative(opts.source, inputPath))
compile(inputPath, outputPath, opts)
})
})
return opts.emitter
}
function compile (inputPath, outputPath, opts) {
const browserifyOpts = Object.assign({
cache: {},
packageCache: {},
plugin: [],
debug: true,
entries: [inputPath]
}, opts.browserify || {})
if (opts.watch) browserifyOpts.plugin.push(watchify)
fs.ensureDirSync(path.dirname(outputPath))
const b = browserify(browserifyOpts)
if (opts.globalTransform) {
opts.globalTransform.forEach(t => b.transform(t, {global: true}))
}
bundle(outputPath, opts, b)
b.on('update', () => {
opts.emitter.emit('update', inputPath)
bundle(outputPath, opts, b)
})
}
function bundle (outputPath, opts, b) {
const write = fs.createWriteStream(outputPath)
if (opts.unassertify) {
b.transform('unassertify')
}
if (opts.uglifyify) {
b.transform('uglifyify', {global: true})
}
if (opts.commonShakeify) {
b.plugin(commonShake, {})
}
b.bundle()
.on('error', function (err) {
opts.emitter.emit('error', err)
this.emit('end')
})
.pipe(exorcist(outputPath + '.map'))
.pipe(write)
write.on('finish', (err, x, y) => {
if (err) opts.emitter.emit('error', err)
opts.emitter.emit('compile', outputPath)
if (opts.compress) {
compress(outputPath, opts)
} else {
checkCount(opts)
}
})
}
function checkCount (opts) {
opts.compiledCount += 1
if (opts.compiledCount === opts.total) {
opts.emitter.emit('build', opts.total)
}
}
function compress (outputPath, opts) {
const code = fs.readFileSync(outputPath, 'utf8')
const map = fs.readFileSync(outputPath + '.map', 'utf8')
uglify(outputPath, code, map, opts, () => {
gzip(outputPath, opts, () => {
opts.emitter.emit('compress', outputPath)
checkCount(opts)
})
})
}
function uglify (outputPath, code, map, opts, callback) {
const base = path.basename(outputPath)
const result = Uglify.minify(code, {
sourceMap: {
content: map,
filename: base,
url: base + '.map'
}
})
if (result.error) {
opts.emitter.emit('error', result.error)
}
fs.writeFile(outputPath, result.code, err => {
if (err) opts.emitter.emit('error', err)
fs.writeFile(outputPath + '.map', result.map, err => {
if (err) opts.emitter.emit('error', err)
callback()
})
})
}
function gzip (outputPath, opts, callback) {
const write = fs.createWriteStream(outputPath + '.gz')
fs.createReadStream(outputPath)
.pipe(zlib.createGzip())
.pipe(write)
write.on('error', (err) => {
if (err) opts.emitter.emit('error', err)
})
write.on('finish', (err) => {
if (err) opts.emitter.emit('error', err)
callback()
})
}