-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgulpfile.mjs
More file actions
executable file
·262 lines (229 loc) · 7.68 KB
/
Copy pathgulpfile.mjs
File metadata and controls
executable file
·262 lines (229 loc) · 7.68 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
252
253
254
255
256
257
258
259
260
261
262
// gulpfile.mjs
import gulp from 'gulp';
import plumber from 'gulp-plumber';
import log from 'fancy-log';
import terser from 'gulp-terser';
import sourcemaps from 'gulp-sourcemaps';
import uglifycss from 'gulp-uglifycss';
import npmDist from 'gulp-npm-dist';
import { deleteAsync } from 'del';
import fs from 'fs/promises'; // Promise-based fs module
import fsSync from 'fs'; // For stream methods
import archiver from 'archiver'; // New zip library
import replace from 'gulp-replace';
import merge from 'merge-stream'; // Combine multiple streams
import rename from 'gulp-rename'; // For renaming files
let isFirefoxBuild = false;
// Error handling function
function onError(err) {
log.error(err);
}
// Clean up dist folder
gulp.task('clean-dist', gulp.series(async () => {
await deleteAsync(['dist']);
}));
// For compiling from ES6 and other JS latest code
gulp.task('js', gulp.series(() => {
log('Gulp js task executing');
return gulp
.src('app/scripts/**/*.js')
.pipe(sourcemaps.init())
.pipe(plumber({ errorHandler: onError }))
.pipe(terser()) // Use terser instead of uglify
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/scripts'));
}));
// Service workers (processing firebase config)
// NOTE: Replace the placeholder BEFORE initializing sourcemaps so the sourcemap reflects the real values.
gulp.task('process-firebase-config', gulp.series(async () => {
log('Processing Firebase configuration');
const firebaseConfig = JSON.parse(await fs.readFile('./app/config/firebase-config.json', 'utf8'));
return gulp
.src('app/background.js')
.pipe(plumber({ errorHandler: onError }))
// Perform the replacement BEFORE initializing sourcemaps.
.pipe(replace('%%FIREBASE_CONFIG%%', JSON.stringify(firebaseConfig)))
.pipe(sourcemaps.init())
.pipe(terser())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist'));
}));
// Copy html files into build for every update
gulp.task('copy-html', gulp.series(() => {
log('Gulp copy-html task executing');
return gulp
.src('app/html/*')
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest('dist/html'));
}));
// Uglify CSS and copy to build
gulp.task('copy-css', gulp.series(() => {
log('Gulp copy-css task executing');
return gulp
.src('app/styles/**/*.css')
.pipe(sourcemaps.init())
.pipe(plumber({ errorHandler: onError }))
.pipe(
uglifycss({
maxLineLen: 80,
uglifyComments: true
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/styles'));
}));
// Copy image files
gulp.task('copy-images', gulp.series(() => {
log('Gulp copy-images task executing');
return gulp
.src('app/images/**/*.+(png|jpg|gif|svg)', { encoding: false })
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest('dist/images'));
}));
// Copy default manifest for Chrome build
gulp.task('copy-manifest', gulp.series(() => {
log('Gulp copy-manifest task executing (default manifest)');
return gulp
.src('app/manifest.json')
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest('dist'));
}));
// NEW TASK: Copy firefox-specific manifest (manifest_firefox.json)
// Renames it to manifest.json for the build.
gulp.task('copy-firefox-manifest', gulp.series(() => {
log('Gulp copy-firefox-manifest task executing');
return gulp
.src('app/manifest_firefox.json')
.pipe(plumber({ errorHandler: onError }))
.pipe(rename('manifest.json'))
.pipe(gulp.dest('dist'));
}));
// Copy _locales
gulp.task('copy-locales', gulp.series(() => {
log('Gulp copy-locales task executing');
return gulp
.src('app/_locales/**')
.pipe(plumber({ errorHandler: onError }))
.pipe(gulp.dest('dist/_locales'));
}));
// Copy dependencies to ./dist/scripts/libs/
gulp.task('copy-libs', gulp.series(() => {
// npmDist returns paths relative to node_modules
const libs = npmDist();
// Remove any firebase files from the npmDist list.
const nonFirebaseLibs = libs.filter(file => !file.includes('firebase'));
// Stream for non‑Firebase libraries (preserve base paths)
const nonFirebaseStream = gulp.src(nonFirebaseLibs, { base: './node_modules' })
.pipe(gulp.dest('dist/scripts/libs'));
// Stream for Firebase: adjust paths if necessary.
const firebaseStream = gulp.src([
'node_modules/firebase/firebase-app-compat.js',
'node_modules/firebase/firebase-remote-config-compat.js'
])
.pipe(gulp.dest('dist/scripts/libs/firebase'));
// Merge the two streams so the task completes when both are done.
return merge(nonFirebaseStream, firebaseStream);
}));
// NEW TASK: Copy textfit dependency to its expected location
gulp.task('copy-textfit', gulp.series(() => {
return gulp.src('node_modules/textfit/textFit.min.js', { allowEmpty: true })
.pipe(gulp.dest('dist/scripts/libs/textfit'));
}));
// A gulp watcher for executing above tasks
gulp.task('watch', gulp.series(() => {
log('Gulp is watching your files :- )');
gulp.watch('app/styles/**/*.css', gulp.series('copy-css'));
gulp.watch('app/html/*', gulp.series('copy-html'));
gulp.watch('app/images/*', gulp.series('copy-images'));
gulp.watch('app/manifest.json', gulp.series('copy-manifest'));
gulp.watch('app/manifest_firefox.json', gulp.series('copy-firefox-manifest'));
gulp.watch('app/_locales/**', gulp.series('copy-locales'));
gulp.watch('app/scripts/**/*.js', gulp.series('js'));
}));
// Prepare zip using archiver
gulp.task('zip', gulp.series(async () => {
log('Gulp zip task executing');
// Read and parse manifest.json to get version info
const manifestContent = await fs.readFile('./dist/manifest.json', 'utf8');
const manifest = JSON.parse(manifestContent);
const zipFileName = isFirefoxBuild
? `Environment Marker-${manifest.version}-firefox.zip`
: `Environment Marker-${manifest.version}.zip`;
// Ensure the package directory exists
await fs.mkdir('package', { recursive: true });
return new Promise((resolve, reject) => {
const output = fsSync.createWriteStream(`package/${zipFileName}`);
const archive = archiver('zip', { zlib: { level: 9 } }); // Maximum compression
output.on('close', () => {
log(`Created zip file: ${zipFileName} (${archive.pointer()} total bytes)`);
resolve();
});
archive.on('error', (err) => {
reject(err);
});
archive.pipe(output);
// Append the entire 'dist' folder into the archive
archive.directory('dist/', false);
archive.finalize();
});
}));
// Standard build task (for default/Chrome build)
gulp.task('build', gulp.series(
'clean-dist',
'js',
'process-firebase-config',
'copy-html',
'copy-css',
'copy-images',
'copy-manifest', // Default manifest for Chrome
'copy-locales',
gulp.parallel('copy-libs', 'copy-textfit'),
(done) => {
done(); // Signal completion
}
));
// Build task for Firefox (using Firefox-specific manifest)
gulp.task('build:firefox', gulp.series(
'clean-dist',
'js',
'process-firebase-config',
'copy-html',
'copy-css',
'copy-images',
'copy-firefox-manifest', // Firefox-specific manifest
'copy-locales',
gulp.parallel('copy-libs', 'copy-textfit'),
(done) => {
done(); // Signal completion
}
));
// Package task for default build
gulp.task('package', gulp.series(
'build',
'zip',
(done) => {
done(); // Signal completion
}
));
// Task to set the Firefox build flag
gulp.task('set-firefox', (done) => {
isFirefoxBuild = true;
done();
});
// Package task for Firefox build
gulp.task('package:firefox', gulp.series(
'set-firefox',
'build:firefox',
'zip',
(done) => {
done(); // Signal completion
}
));
// Default task: build and then watch files
gulp.task('default', gulp.series(
'build',
'watch',
(done) => {
done(); // Signal completion
}
));