forked from prebid/prebid-universal-creative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
206 lines (173 loc) · 5.52 KB
/
gulpfile.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
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
const _ = require('lodash');
const gulp = require('gulp');
const argv = require('yargs').argv;
const opens = require('opn');
const header = require('gulp-header');
const connect = require('gulp-connect');
const creative = require('./package.json');
const uglify = require('gulp-uglify');
const gulpClean = require('gulp-clean');
const webpackStream = require('webpack-stream');
const webpackConfig = require('./webpack.conf');
const inject = require('gulp-inject');
const rename = require('gulp-rename');
const KarmaServer = require('karma').Server;
const karmaConfMaker = require('./karma.conf.maker');
const execa = require('execa');
const path = require('path');
const dateString = 'Updated : ' + (new Date()).toISOString().substring(0, 10);
const banner = '/* <%= creative.name %> v<%= creative.version %>\n' + dateString + ' */\n';
const port = 9990;
function clean() {
return gulp.src(['dist/', 'build/'], {
read: false,
allowEmpty: true
})
.pipe(gulpClean());
}
function buildDev() {
return gulp.src(['src/creative.js'])
.pipe(webpackStream(webpackConfig))
.pipe(gulp.dest('build'));
}
function buildNativeDev() {
var cloned = _.cloneDeep(webpackConfig);
cloned.output.filename = 'native-trk.js';
return gulp.src(['src/nativeTrackers.js'])
.pipe(webpackStream(cloned))
.pipe(gulp.dest('build'));
}
function buildCookieSync() {
let cloned = _.cloneDeep(webpackConfig);
delete cloned.devtool;
let target = gulp.src('resources/load-cookie.html');
let sources = gulp.src(['src/cookieSync.js'])
.pipe(webpackStream(cloned))
.pipe(uglify());
return target.pipe(inject(sources, {
starttag: '// cookie-sync start',
endtag: '// end',
transform: function (filePath, file) {
return file.contents.toString('utf8')
}
}))
.pipe(gulp.dest('dist'));
}
function buildUidDev() {
var cloned = _.cloneDeep(webpackConfig);
delete cloned.devtool;
cloned.output.filename = 'uid.js';
return gulp.src(['src/ssp-userids/uid.js'])
.pipe(webpackStream(cloned))
.pipe(gulp.dest('build'));
}
function buildProd() {
let cloned = _.cloneDeep(webpackConfig);
delete cloned.devtool;
return gulp.src(['src/creative.js'])
.pipe(webpackStream(cloned))
.pipe(rename({ extname: '.max.js' }))
.pipe(gulp.dest('dist'))
.pipe(uglify())
.pipe(header(banner, { creative: creative }))
.pipe(rename({
basename: 'creative',
extname: '.js'
}))
.pipe(gulp.dest('dist'));
}
function buildNative() {
var cloned = _.cloneDeep(webpackConfig);
delete cloned.devtool;
cloned.output.filename = 'native-trk.js';
return gulp.src(['src/nativeTrackers.js'])
.pipe(webpackStream(cloned))
.pipe(uglify())
.pipe(header('/* v<%= creative.version %>\n' + dateString + ' */\n', { creative: creative }))
.pipe(gulp.dest('dist'));
}
function buildUid() {
var cloned = _.cloneDeep(webpackConfig);
delete cloned.devtool;
cloned.output.filename = 'uid.js';
return gulp.src(['src/ssp-userids/uid.js'])
.pipe(webpackStream(cloned))
.pipe(uglify())
.pipe(header('/* v<%= creative.version %>\n' + dateString + ' */\n', { creative: creative }))
.pipe(gulp.dest('dist'));
}
// Run the unit tests.
//
// By default, this runs in headless chrome.
//
// If --watch is given, the task will open the karma debug window
// If --browserstack is given, it will run the full suite of currently supported browsers.
// If --e2e is given, it will run test defined in ./test/e2e/specs in browserstack
function test(done) {
if (argv.e2e) {
let wdioCmd = path.join(__dirname, 'node_modules/.bin/wdio');
let wdioConf = path.join(__dirname, 'wdio.conf.js');
let wdioOpts = [
wdioConf
];
return execa(wdioCmd, wdioOpts, { stdio: 'inherit' });
} else {
let karmaConf = karmaConfMaker(false, argv.browserstack, argv.watch);
new KarmaServer(karmaConf, newKarmaCallback(done)).start();
}
}
function newKarmaCallback(done) {
return function(exitCode) {
if (exitCode) {
done(new Error('Karma tests failed with exit code' + exitCode));
if (argv.browserstack) {
process.exit(exitCode);
}
} else {
done();
if (argv.browserstack) {
process.exit(exitCode);
}
}
}
}
function setupE2E(done) {
argv.e2e = true;
done();
}
gulp.task('test', gulp.series(clean, test));
gulp.task('e2e-test', gulp.series(clean, setupE2E, gulp.parallel(buildDev, buildCookieSync, buildNativeDev, buildUidDev, watch), test));
function watch(done) {
const mainWatcher = gulp.watch([
'src/**/*.js',
'test/**/*.js'
]);
connect.server({
https: argv.https,
livereload: true,
port,
root: './'
});
mainWatcher.on('all', gulp.series(clean, gulp.parallel(buildDev, buildNativeDev, buildCookieSync, buildUidDev), test));
done();
}
function openWebPage() {
return opens(`${(argv.https) ? 'https' : 'http'}://localhost:${port}`);
}
gulp.task('serve', gulp.series(clean, gulp.parallel(buildDev, buildNativeDev, buildCookieSync, buildUidDev, watch, test), openWebPage));
gulp.task('build', gulp.parallel(buildProd, buildCookieSync, buildNative, buildUid));
gulp.task('test-coverage', (done) => {
new KarmaServer(karmaConfMaker(true, false, false), newKarmaCallback(done)).start();
});
gulp.task('view-coverage', (done) => {
const coveragePort = 1999;
const localhost = (argv.host) ? argv.host : 'localhost';
connect.server({
port: coveragePort,
root: 'build/coverage/karma_html',
livereload: false
});
opens('http://' + localhost + ':' + coveragePort);
done();
});