-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
116 lines (104 loc) · 3.18 KB
/
Copy pathgulpfile.js
File metadata and controls
116 lines (104 loc) · 3.18 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
/* eslint-disable no-undef */
/*jshint esversion:6 */
/**
* USE with
*
* gulp init - Concatenates Abrantes with the plugins in this Gulpfile
* gulp minify - Minifies Abrantes and the plugins
*
*
*/
// eslint-disable-next-line no-unused-vars
const { src, dest, series, watch } = require('gulp');
const concat = require('gulp-concat');
const terser = require('gulp-terser');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
/**
* List of files to be included in the build. The order is important as some plugins may depend on others.
* You can comment out plugins that you don't want to include in the build.
*/
const myBuild = [
'Abrantes.js', // Core Abrantes library
'plugins/ga4-gtag.js', // Google Analytics 4 (gtag.js)
// 'plugins/matomo.js',
'plugins/hotjar.js', // Hotjar
// 'plugins/clarity.js', // Microsoft Clarity
'plugins/formtrack.js', // Form tracking
'plugins/add2DL.js', // Add to Data Layer (Google Tag Manager)
'plugins/log.js', // Simple logging plugin
'plugins/seed.js', // Seed plugin for consistent random values (login in multiple devices or cookies disabled)
'plugins/hubspot.js', // HubSpot Enterprise tracking
];
/**
* It will concatenate Abrantes with the plugins in the src of this function
*/
function init() {
return src(myBuild)
.pipe(concat("AbrantesPlus.js"))
.pipe(dest('./'));
}
/**
* Minifies the file wih Abrantes and the plugins
*/
function minify() {
return src([
'AbrantesPlus.js',
])
.pipe(sourcemaps.init())
.pipe(terser())
.pipe(rename({ suffix: ".min" }))
.pipe(sourcemaps.write('./'))
.pipe(dest('./'));
}
/**
* Creates the ES6 module version (AbrantesPlusMod.js)
* Concatenates files and adds module export at the end
*/
function initMod() {
const { Transform } = require('stream');
// Custom transform to add ES6 export at the end
const addModuleExport = new Transform({
objectMode: true,
transform(file, encoding, callback) {
if (file.isBuffer()) {
const moduleExport = `
// ES6 Module export - also expose globally for backward compatibility
if (typeof window !== 'undefined') {
window.Abrantes = Abrantes;
}
export default Abrantes;
`;
file.contents = Buffer.concat([file.contents, Buffer.from(moduleExport)]);
}
callback(null, file);
}
});
return src(myBuild)
.pipe(concat("AbrantesPlusMod.js"))
.pipe(addModuleExport)
.pipe(dest('./'));
}
/**
* Minifies the ES6 module version
*/
function minifyMod() {
return src([
'AbrantesPlusMod.js',
])
.pipe(sourcemaps.init())
.pipe(terser({
module: true
}))
.pipe(rename({ suffix: ".min" }))
.pipe(sourcemaps.write('./'))
.pipe(dest('./'));
}
// Build all outputs in strict order to avoid sourcemap/minify races.
const build = series(init, minify, initMod, minifyMod);
exports.init = init;
exports.initMod = initMod;
exports.minify = minify;
exports.minifyMod = minifyMod;
exports.build = build;
exports.default = build;