forked from jakearchibald/svgomg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
159 lines (144 loc) · 4.35 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
const fs = require('fs/promises');
const path = require('path');
const sirv = require('sirv-cli');
const svgoPkg = require('svgo/package.json');
const sass = require('sass');
const gulp = require('gulp');
const gulpSourcemaps = require('gulp-sourcemaps');
const gulpSass = require('gulp-sass');
const gulpNunjucks = require('gulp-nunjucks');
const gulpHtmlmin = require('gulp-htmlmin');
const rollup = require('rollup');
const { nodeResolve: rollupResolve } = require('@rollup/plugin-node-resolve');
const rollupCommon = require('@rollup/plugin-commonjs');
const rollupReplace = require('@rollup/plugin-replace');
const { terser: rollupTerser } = require('rollup-plugin-terser');
const readJSON = async (filePath) => {
const content = await fs.readFile(filePath, 'utf-8');
return JSON.parse(content);
};
function css() {
const boundSass = gulpSass(sass);
return gulp
.src('src/css/*.scss')
.pipe(gulpSourcemaps.init())
.pipe(
boundSass
.sync({ outputStyle: 'compressed' })
.on('error', boundSass.logError),
)
.pipe(gulpSourcemaps.write('./'))
.pipe(gulp.dest('build/'));
}
async function html() {
const [config, changelog, headCSS] = await Promise.all([
readJSON(path.join(__dirname, 'src', 'config.json')),
readJSON(path.join(__dirname, 'src', 'changelog.json')),
fs.readFile(path.join(__dirname, 'build', 'head.css'), 'utf-8'),
]);
return gulp
.src('src/*.html')
.pipe(
gulpNunjucks.compile({
plugins: config.plugins,
headCSS,
changelog,
SVGO_VERSION: svgoPkg.version,
liveBaseUrl: 'https://jakearchibald.github.io/svgomg/',
title: `SVGOMG - SVGO's Missing GUI`,
description: 'Easy & visual compression of SVG images.',
iconPath: 'imgs/icon.png',
}),
)
.pipe(
gulpHtmlmin({
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
decodeEntities: true,
minifyCSS: false,
minifyJS: true,
removeAttributeQuotes: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
}),
)
.pipe(gulp.dest('build'));
}
const rollupCaches = new Map();
async function js(entry, outputPath) {
const name = path.basename(path.dirname(entry));
const changelog = await readJSON(
path.join(__dirname, 'src', 'changelog.json'),
);
const bundle = await rollup.rollup({
cache: rollupCaches.get(entry),
input: `src/${entry}`,
plugins: [
rollupReplace({
preventAssignment: true,
SVGOMG_VERSION: JSON.stringify(changelog[0].version),
}),
rollupResolve({ browser: true }),
rollupCommon({ include: /node_modules/ }),
rollupTerser(),
],
});
rollupCaches.set(entry, bundle.cache);
await bundle.write({
sourcemap: true,
format: 'iife',
file: `build/${outputPath}/${name}.js`,
});
}
const allJs = gulp.parallel(
js.bind(null, 'js/prism-worker/index.js', 'js/'),
js.bind(null, 'js/gzip-worker/index.js', 'js/'),
js.bind(null, 'js/svgo-worker/index.js', 'js/'),
js.bind(null, 'js/sw/index.js', ''),
js.bind(null, 'js/page/index.js', 'js/'),
);
function copy() {
return gulp
.src([
'src/{.well-known,imgs,test-svgs,fonts}/**',
// Exclude the test-svgs files except for `car-lite.svg`
// which is used in the demo
'!src/test-svgs/!(car-lite.svg)',
'src/*.json',
])
.pipe(gulp.dest('build'));
}
function clean() {
return fs.rm('build', { force: true, recursive: true });
}
const mainBuild = gulp.parallel(gulp.series(css, html), allJs, copy);
function watch() {
gulp.watch(['src/css/**/*.scss'], gulp.series(css, html));
gulp.watch(['src/js/**/*.js'], allJs);
gulp.watch(
['src/*.{html,json}', 'src/**/*.{svg,woff2}'],
gulp.parallel(html, copy, allJs),
);
}
function serve() {
sirv('build', {
host: 'localhost',
port: 8080,
dev: true,
clear: false,
});
}
exports.clean = clean;
exports.allJs = allJs;
exports.css = css;
exports.html = html;
exports.copy = copy;
exports.build = mainBuild;
exports['clean-build'] = gulp.series(clean, mainBuild);
exports.dev = gulp.series(clean, mainBuild, gulp.parallel(watch, serve));