forked from zendesk/copenhagen_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
168 lines (141 loc) · 4.38 KB
/
gulpfile.babel.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
160
161
162
163
164
165
166
167
168
// config
import { config as gulpConfig } from './gulp.config';
import { config as webpackConfig } from './webpack.config';
import { viewports } from './tokens';
// plugins
import { readFile, readdir, writeFile } from 'fs/promises';
import junk from 'junk';
import gulp from 'gulp';
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import sassVars from 'gulp-sass-vars';
import imagemin from 'gulp-imagemin';
import postCss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import webpack from 'webpack';
/*
Tasks
*/
const manifestVariables = () => {
return readFile(gulpConfig.zendesk.manifest, 'utf8')
.then((data) => {
const parsedData = JSON.parse(data);
const { settings } = parsedData;
const variables = settings
.map((item) => {
return item.variables;
})
.flat();
const identifiers = variables.map((item) => {
return item.identifier;
});
return identifiers;
})
.catch((err) => console.error('Error while loadind manifest file!', err));
};
const assetsVariables = () => {
return readdir(gulpConfig.assets.input.path)
.then((filesList) => {
const fixedFilesList = filesList.filter(junk.not);
const variables = fixedFilesList.map((fileName) => {
const fixedFileName = fileName.replace(/[^a-z0-9\-_]+/g, '-');
return `assets-${fixedFileName}`;
});
return variables;
})
.catch((err) => console.error('Error while loading assets dir!', err));
};
const zendeskSassVars = async () => {
const variables = new Object();
await Promise.all([manifestVariables(), assetsVariables()]).then((values) => {
const flat = values.flat();
flat.forEach((value) => {
variables[value] = '\t' + '$' + value + '\t';
});
});
return variables;
};
const compileStyles = async () => {
const sassCompiler = gulpSass(dartSass);
const plugins = [autoprefixer()];
const customVars = await zendeskSassVars();
return gulp
.src(`${gulpConfig.sass.input.path}/${gulpConfig.sass.input.file}`)
.pipe(
sassVars(
{
...customVars,
viewports
},
{ verbose: false }
)
)
.pipe(sassCompiler({ outputStyle: 'compressed' }).on('error', sassCompiler.logError))
.pipe(postCss(plugins))
.pipe(gulp.dest(gulpConfig.sass.output.path));
};
const unescapeCss = () => {
return readFile(`${gulpConfig.sass.output.path}/${gulpConfig.sass.output.file}`, 'utf8')
.then((data) => {
const unescaped = data.replace(/zass-/gm, '').replace(/\"\t/g, '').replace(/\t\"/g, '');
writeUnescapedCss(unescaped);
})
.catch((err) => console.error('Error while unescaping compiled CSS!', err));
};
const writeUnescapedCss = (unescapedCss) => {
writeFile(`${gulpConfig.sass.output.path}/${gulpConfig.sass.output.file}`, unescapedCss)
.then((data) => {
console.log('Unescaped CSS written successfully!');
})
.catch((err) => console.error('Error while writing unescaped CSS!', err));
};
const compileScripts = () => {
return new Promise((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
return reject(err);
}
if (stats.hasErrors()) {
return reject(new Error(stats.compilation.errors.join('\n')));
}
resolve();
});
});
};
const compressImages = () => {
return gulp
.src(`${gulpConfig.assets.input.path}/*`)
.pipe(imagemin())
.pipe(gulp.dest(gulpConfig.assets.output.path));
};
const copyTemplates = () => {
return gulp
.src(`${gulpConfig.templates.input.path}/**`)
.pipe(gulp.dest(gulpConfig.templates.output.path));
};
const copySettings = () => {
return gulp
.src(`${gulpConfig.settings.input.path}/**`)
.pipe(gulp.dest(gulpConfig.settings.output.path));
};
/*
Watchers
*/
const watch = () => {
gulp.watch(`${gulpConfig.sass.input.path}/**/*.scss`, gulp.series(compileStyles, unescapeCss));
gulp.watch(`${gulpConfig.scripts.input.path}/**/*.js`, compileScripts);
gulp.watch(`${gulpConfig.templates.input.path}/*.hbs`, copyTemplates);
// gulp.watch(`${gulpConfig.sass.output.path}/${gulpConfig.sass.output.file}`, unescapeCss);
};
/*
Builders
*/
const build = gulp.series(
gulp.parallel(compileStyles, compileScripts, compressImages, copyTemplates, copySettings),
unescapeCss
);
/*
Exports
*/
exports.watch = gulp.series(build, watch);
exports.build = build;