Skip to content

Commit 0855861

Browse files
add method to apply tinyify to a separate browserify pipeline, closes #1 (#4)
add method to apply tinyify to a separate browserify pipeline, closes #1
1 parent 3a1f8fd commit 0855861

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

index.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
var packFlat = require('browser-pack-flat/plugin')
2+
var packFlatStream = require('browser-pack-flat')
23
var commonShake = require('common-shakeify')
34
var unassertify = require('unassertify')
45
var uglify = require('minify-stream')
56
var envify = require('envify/custom')
67
var uglifyify = require('uglifyify')
78

9+
function makeUglifyOptions (debug) {
10+
var uglifyOpts = {
11+
output: {
12+
ascii_only: true
13+
},
14+
mangle: {
15+
safari10: true
16+
}
17+
}
18+
if (!debug) {
19+
uglifyOpts.sourceMap = false
20+
}
21+
return uglifyOpts
22+
}
23+
824
module.exports = function (b, opts) {
925
if (typeof b !== 'object') {
1026
throw new Error('tinyify: must be used as a plugin, not a transform')
1127
}
1228

1329
opts = Object.assign({
14-
flat: true
30+
flat: true,
31+
env: {}
1532
}, opts)
1633

1734
var env = Object.assign({
@@ -42,16 +59,26 @@ module.exports = function (b, opts) {
4259
b.plugin(commonShake)
4360

4461
// Minify the final output.
45-
var uglifyOpts = {
46-
output: {
47-
ascii_only: true
48-
},
49-
mangle: {
50-
safari10: true
51-
}
52-
}
53-
if (!b._options.debug) {
54-
uglifyOpts.sourceMap = false
55-
}
62+
var uglifyOpts = makeUglifyOptions(b._options.debug)
5663
b.pipeline.get('pack').push(uglify(uglifyOpts))
5764
}
65+
66+
module.exports.applyToPipeline = function applyToPipeline (pipeline, opts) {
67+
opts = Object.assign({
68+
flat: true,
69+
debug: false,
70+
basedir: process.cwd()
71+
}, opts)
72+
73+
if (opts.flat) {
74+
pipeline.get('pack').splice(0, 1, packFlatStream({
75+
raw: true,
76+
debug: opts.debug,
77+
basedir: opts.basedir
78+
}))
79+
}
80+
81+
// Minify the final output.
82+
var uglifyOpts = makeUglifyOptions(opts.debug)
83+
pipeline.get('pack').push(uglify(uglifyOpts))
84+
}

0 commit comments

Comments
 (0)