-
Notifications
You must be signed in to change notification settings - Fork 36
/
stream-files.js
148 lines (143 loc) · 5.58 KB
/
stream-files.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
var gulp = require('gulp'),
through = require('through2'),
gif = require('gulp-if'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
rev = require('gulp-rev'),
streamify = require('gulp-streamify'),
cleanCSS = require('gulp-clean-css'),
gsourcemaps = require('gulp-sourcemaps'),
gutil = require('gulp-util'),
order = require('gulp-order'),
logger = require('./service/logger'),
using = require('./using'),
sourcemaps = require('./sourcemaps'),
isOptionEnabled = require('./is-option-enabled'),
addBundleResultsToFile = require('./results/add-bundle-results-to-file'),
BundleKeys = require('./model/bundle-keys'),
isMinEnabled = require('./is-min-enabled'),
defaults = require('lodash').defaults;
module.exports.handleTransformError = function (thisStream, isWatch, bundleName, bundleKey, err) {
logger.log(gutil.colors.red("ERROR in custom transforms for '" + bundleName + "." + bundleKey + "':"));
logger.log(err.toString());
if (isWatch) {
thisStream.emit('end');
} else {
process.exit(1);
}
};
module.exports.attachStreamOptions = function (file, opts) {
// add some useful options to file for possible inspection later in external custom gulp streams
file.bundleOptions = {
env: opts.env,
type: opts.type,
bundleName: opts.bundleName,
isWatch: opts.isWatch,
isBundleAll: opts.isBundleAll
};
};
module.exports.scripts = function (opts) {
var self = this,
concatOpts = defaults({
path: opts.bundleName + ((opts.isBundleAll && opts.env) ? '.' + opts.env : '') + '.js'
}, opts.bundleOptions.pluginOptions['gulp-concat']);
var relativeSources = [];
return gulp.src(opts.src, {base: opts.base})
.pipe(using.bundle(opts.bundleName, BundleKeys.SCRIPTS, opts.env, opts.isBundleAll))
.pipe(through.obj(function (file, enc, cb) {
relativeSources.push(file.relative);
self.attachStreamOptions(file, opts);
this.push(file);
cb();
}))
.pipe(gif(function (file) {
return sourcemaps.isEnabled(opts);
}, gsourcemaps.init(opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.SCRIPTS].init)
))
.pipe(opts.bundleOptions.transforms[BundleKeys.SCRIPTS]())
.on('error', function (e) {
self.handleTransformError(this, opts.isWatch, opts.bundleName, BundleKeys.SCRIPTS, e);
})
.pipe(gif(function (file) {
return sourcemaps.isEnabled(opts);
}, through.obj(sourcemaps.verify)
))
.pipe(gif(function (file) {
return isMinEnabled.js(file, opts);
},
gif(function (file) {
return file.isStream();
},
streamify(
uglify(opts.bundleOptions.pluginOptions['gulp-uglify'])),
uglify(opts.bundleOptions.pluginOptions['gulp-uglify'])
)))
.on('error', function (e) {
self.handleTransformError(this, opts.isWatch, opts.bundleName, BundleKeys.SCRIPTS, e);
})
.pipe(gif(function (file) {
return opts.bundleOptions.order && opts.bundleOptions.order.scripts;
}, order(opts.bundleOptions.order.scripts)
))
.pipe(gif(function (file) {
return file.isStream();
},
streamify(concat(concatOpts)),
concat(concatOpts)
))
.pipe(gif(isOptionEnabled(opts.bundleOptions.rev, opts.env),
gif(function (file) {
return file.isStream();
}, streamify(rev()), rev())
))
.pipe(gif(function (file) {
return sourcemaps.isEnabled(opts);
}, gsourcemaps.write(
opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.SCRIPTS].destPath,
opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.SCRIPTS].write
)
))
.pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.SCRIPTS, opts.bundleOptions.result, opts.env, opts.isBundleAll, opts.bundleOrder, relativeSources));
};
module.exports.styles = function (opts) {
var self = this;
var relativeSources = [];
return gulp.src(opts.src, {base: opts.base})
.pipe(using.bundle(opts.bundleName, BundleKeys.STYLES, opts.env, opts.isBundleAll))
.pipe(through.obj(function (file, enc, cb) {
relativeSources.push(file.relative);
self.attachStreamOptions(file, opts);
this.push(file);
cb();
}))
.pipe(
gif(function (file) {
return sourcemaps.isEnabled(opts);
}, gsourcemaps.init(opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.STYLES].init)
))
.pipe(opts.bundleOptions.transforms[BundleKeys.STYLES]())
.on('error', function (e) {
self.handleTransformError(this, opts.isWatch, opts.bundleName, BundleKeys.STYLES, e);
})
.pipe(gif(function (file) {
return sourcemaps.isEnabled(opts);
}, through.obj(sourcemaps.verify)
))
.pipe(gif(function (file) {
return isMinEnabled.css(file, opts);
}, cleanCSS(opts.bundleOptions.pluginOptions['gulp-clean-css'])))
.pipe(gif(function (file) {
return opts.bundleOptions.order && opts.bundleOptions.order.styles;
}, order(opts.bundleOptions.order.styles)
))
.pipe(concat(opts.bundleName + ((opts.isBundleAll && opts.env) ? '.' + opts.env : '') + '.css'))
.pipe(gif(isOptionEnabled(opts.bundleOptions.rev, opts.env), rev()))
.pipe(gif(function (file) {
return sourcemaps.isEnabled(opts);
}, gsourcemaps.write(
opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.STYLES].destPath,
opts.bundleOptions.pluginOptions['gulp-sourcemaps'][BundleKeys.STYLES].write
)
))
.pipe(addBundleResultsToFile(opts.bundleName, BundleKeys.STYLES, opts.bundleOptions.result, opts.env, opts.isBundleAll, opts.bundleOrder, relativeSources));
};