-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgulpfile.js
More file actions
251 lines (219 loc) · 6.33 KB
/
gulpfile.js
File metadata and controls
251 lines (219 loc) · 6.33 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import log from 'fancy-log';
import _ from 'lodash';
import yargs from 'yargs';
import del from 'del';
import connect from 'gulp-connect';
import karma from 'karma';
import gulp from 'gulp';
import eslint from 'gulp-eslint-new';
import gulpif from 'gulp-if';
import header from 'gulp-header';
import mocha from 'gulp-mocha';
import uglify from 'gulp-uglify';
import gv from 'genversion';
import isDocker from 'is-docker';
import { readFile } from 'fs/promises';
// Rollup specific stuff
import { rollup } from 'rollup';
import babel from '@rollup/plugin-babel';
import nodeResolve from '@rollup/plugin-node-resolve';
import { Readable, Transform } from 'node:stream';
import File from 'vinyl';
import path from "path";
const id5Api = JSON.parse(await readFile('package.json'));
const port = 9998;
const argv = yargs.argv;
function lint(done) {
if (argv.nolint) {
return done();
}
return gulp.src([
'src/**/*.js',
'lib/**/*.js',
'test/**/*.js',
'integration/**/*.js',
])
.pipe(eslint({ fix: true, configType: 'flat' })) // Lint files, create fixes.
.pipe(eslint.fix()) // Fix files if necessary.
.pipe(eslint.format('stylish')) // Output lint results to the console.
.pipe(eslint.failAfterError()); // Exit with an error if problems are found.
}
// Watch Task with Live Reload
function watch(done) {
var mainWatcher = gulp.watch([
'src/**/*.js',
'lib/**/*.js',
'test/spec/**/*.js',
'!test/spec/loaders/**/*.js',
'package.json'
]);
connect.server({
https: argv.https,
port: port,
root: './',
livereload: true
});
mainWatcher.on('all', gulp.series('clean', 'generate', gulp.parallel(lint, 'build-bundle-dev', test)));
done();
}
var banner = `/**
* ${id5Api.name}
* @version v${id5Api.version}
* @link ${id5Api.homepage}
* @license ${id5Api.license}
*/
`;
const bundles = [
{ entry: 'src/index.js', output: 'id5-api.js' },
{ entry: 'src/api-lite.js', output: 'id5-api-lite.js' },
{ entry: 'src/esp.js', output: 'esp.js' },
{ entry: 'src/id5PrebidModule.js', output: 'id5PrebidModule.js' },
];
function rollupConfig(input, outputFileName) {
return {
input,
output: {
file: outputFileName,
format: 'iife',
sourcemap: true,
},
plugins: [
babel({ babelHelpers: 'bundled' }),
nodeResolve(),
]
};
}
// Builds an object stream containing all the bundle output chunks
function buildRollupBaseStream() {
const result = new Readable({
// stub _read() as it's not available on Readable stream, needed by gulp et al
read: () => { },
objectMode: true,
});
const build = async () => {
for(const bundleDef of bundles) {
const options = rollupConfig(bundleDef.entry, bundleDef.output);
const bundle = await rollup(options);
result.emit('bundle', bundle);
const { output } = await bundle.generate(options.output);
for (const chunk of output) {
result.push(chunk);
}
}
};
build().then(() => {
// signal end of write
result.push(null);
}).catch((error) => {
result.emit('error', error);
});
return result;
}
// Adapts the Rollup bundle stream we obtained before to a Vinyl Files stream
const buildRollupTransformer = () => new Transform({ objectMode: true,
transform(chunk, encoding, callback) {
const makeFile = (content, filename) => {
const contents = Buffer.from(content, encoding);
return new File({
path: chunk.fileName,
contents,
});
}
let file = null, error = null;
switch(chunk.type) {
case 'asset':
file = makeFile(chunk.source, chunk.fileName);
break;
case 'chunk':
file = makeFile(chunk.code, chunk.fileName);
break;
default:
error = new Error(`Unexpected chunk type: ${chunk.type}`);
}
callback(error, file);
}
});
const isNotMap = file => !file.path.endsWith('.map');
function bundleDev() {
return buildRollupBaseStream()
.pipe(buildRollupTransformer())
.pipe(gulpif(isNotMap, header(banner)))
.pipe(gulp.dest('build/dev'));
}
function bundleProd() {
return buildRollupBaseStream()
.pipe(buildRollupTransformer())
.pipe(gulpif(isNotMap, uglify()))
.pipe(gulpif(isNotMap, header(banner)))
.pipe(gulp.dest('build/dist'))
}
function karmaCallback(done) {
return function(exitCode) {
if (exitCode) {
done(new Error('Karma tests failed with exit code ' + exitCode));
} else {
done();
}
}
}
// Run the unit tests in headless chrome.
// If --continuous is given, the task will re-run unit tests whenever the source code changes
function test(done) {
let karmaConfig = karma.config.parseConfig(path.resolve() + '/karma.conf.cjs',
{
singleRun: !argv.continuous
},
{
promiseConfig: false,
throwErrors: true
});
new karma.Server(karmaConfig, done).start();
}
// support tasks
gulp.task(lint);
gulp.task(watch);
gulp.task('clean', () => del(['build', 'generated']));
gulp.task('info', (done) => {
log(`Running gulp on node ${process.version}`);
log(`Building ID5 API version ${id5Api.version}`);
done();
});
gulp.task('generate', (done) => {
gv.generate('generated/version.js', { useEs6Syntax: true }, done);
});
gulp.task('build-bundle-dev', bundleDev);
gulp.task('build-bundle-prod', bundleProd);
gulp.task('build-all', gulp.parallel('build-bundle-dev', 'build-bundle-prod'));
gulp.task('inttest', () => {
// `gulp-mocha` needs filepaths so you can't have any plugins before it
const grepIndex = process.argv.indexOf('-g');
const grepPattern = grepIndex > -1 ? process.argv[grepIndex + 1] : null;
const mochaOptions = {
reporter: isDocker() ? 'spec' : 'nyan',
inlineDiffs: true,
};
if (grepPattern) {
mochaOptions.grep = grepPattern;
}
return gulp.src('integration/**/*.spec.js', {read: false})
.pipe(mocha(mochaOptions));
});
// public tasks (dependencies are needed for each task since they can be ran on their own)
gulp.task('test', gulp.series('clean', 'generate', lint, test));
gulp.task('build', gulp.series(
'info',
'clean',
'generate',
'lint',
test,
'build-all',
'inttest'
));
gulp.task('serve', gulp.series(
'info',
'clean',
'generate',
lint,
gulp.parallel('build-bundle-dev', watch, test)
));
gulp.task('npm-prepare-release', gulp.series('info', 'clean', 'generate', 'build-bundle-prod'))