-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
127 lines (115 loc) · 4.13 KB
/
gulpfile.js
File metadata and controls
127 lines (115 loc) · 4.13 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
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
const postcss = require('gulp-postcss');
const autoprefixerPlugin = require('autoprefixer');
const terser = require('gulp-terser');
const htmlmin = require('gulp-htmlmin');
const flatten = require('gulp-flatten');
const rename = require('gulp-rename');
const nodePath = require('path');
const fs = require('fs');
const archiver = require('archiver');
function compileSass(dest) {
const out = dest === 'dev' ? 'nested' : 'compressed';
return gulp.src('./src/chrome/scss/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: out }).on('error', sass.logError))
.pipe(postcss([autoprefixerPlugin()]))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`./build/${dest}/css`));
}
function buildDevWatch() {
const dist = './build/dev';
const images = './src/**/*.{png,jpg,jpeg,gif,svg}';
const fonts = './src/**/fonts/**/*.{woff,woff2,txt}';
const code = './src/**/*.{js,json,html}';
const pipe = (glob, destPath = dist, opt = null) => {
if (opt && opt.fonts) {
return gulp.src(glob, opt)
.pipe(rename((path) => {
const parts = path.dirname.split(nodePath.sep);
const fontsIndex = parts.indexOf('fonts');
if (fontsIndex !== -1) {
// Drop everything up to “fonts,” leaving the next subfolder
path.dirname = parts.slice(fontsIndex + 1).join(nodePath.sep);
}
}))
.pipe(gulp.dest(destPath));
}
return gulp.src(glob, opt)
.pipe(flatten())
.pipe(gulp.dest(destPath));
};
const watch = (glob, fn) => {
gulp.watch(glob, { ignoreInitial: false }, fn);
}
watch('./src/chrome/scss/*.scss', () => compileSass('dev'));
watch(code, () => pipe(code));
watch(images, () => pipe(images, `${dist}/img`, { encoding: false }));
watch(fonts, () => pipe(fonts, `${dist}/font`, { encoding: false, fonts: true }));
}
function buildDist() {
const src = './src';
const dist = './build/dist';
const process = (glob, transform, destPath = dist, opt = null) => {
let stream = gulp.src(`${src}/${glob}`, opt);
if (transform) {
stream = stream.pipe(transform);
}
if (opt && opt.fonts) {
return stream
.pipe(rename((path) => {
const parts = path.dirname.split(nodePath.sep);
const fontsIndex = parts.indexOf('fonts');
if (fontsIndex !== -1) {
// Drop everything up to “fonts,” leaving the next subfolder
path.dirname = parts.slice(fontsIndex + 1).join(nodePath.sep);
}
}))
.pipe(gulp.dest(destPath));
}
stream = stream.pipe(flatten());
return stream.pipe(gulp.dest(destPath));
};
return Promise.all([
compileSass('dist'),
process('**/*.js', terser({
compress: {
ecma: 2020,
passes: 2,
drop_console: true, // Keep console for debugging or set to true for production
// unsafe: true,
// unsafe_methods: true
},
mangle: {
properties: false // Don't rename properties as Chrome APIs use them
},
format: {
comments: false,
ecma: 2020,
ascii_only: true
}
})),
process('**/*.html', htmlmin({ collapseWhitespace: true })),
process('**/*.{png,jpg,jpeg,gif,svg}', null, `${dist}/img`, { encoding: false }),
process('**/fonts/**/*.{woff,woff2,txt}', null, `${dist}/font`, { encoding: false, fonts: true }),
process('**/*.json')
]);
}
function packDist() {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream('./release/adstxter.zip');
const archive = archiver('zip', {
zlib: { level: 5 } // lowest for fastest compression
});
output.on('close', resolve);
archive.on('error', reject);
archive.pipe(output);
archive.directory('./build/dist/', false);
archive.finalize();
});
}
exports.buildDevWatch = buildDevWatch;
exports.buildDist = buildDist;
exports.packDist = packDist;