forked from 24ways/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
210 lines (181 loc) · 5.29 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
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
// --------------------------------------------------------
// Dependencies
// --------------------------------------------------------
// Utils
const fs = require('fs');
const del = require('del');
const gulp = require('gulp');
// JavaScript
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
// CSS
const postcss = require('gulp-postcss');
const apply = require('postcss-apply');
const assets = require('postcss-assets');
const autoprefixer = require('autoprefixer');
const calc = require('postcss-calc');
const colorFunction = require('postcss-color-function');
const customMedia = require('postcss-custom-media');
const importer = require('postcss-easy-import');
const mapper = require('postcss-map');
const mediaMinMax = require('postcss-media-minmax');
const nano = require('cssnano');
const nested = require('postcss-nested');
const responsiveType = require('postcss-responsive-type');
const simpleVars = require('postcss-simple-vars');
// Misc
const ghPages = require('gulp-gh-pages');
const imagemin = require('gulp-imagemin');
const sourcemaps = require('gulp-sourcemaps');
// Fractal
const pkg = require('./package.json');
const fractal = require('./fractal.js');
const logger = fractal.cli.console;
// --------------------------------------------------------
// Configuration
// --------------------------------------------------------
// Paths
const paths = {
build: `${__dirname}/www`,
dest: `${__dirname}/tmp`,
src: `${__dirname}/src`,
modules: `${__dirname}/node_modules`
};
// PostCSS plugins
const processors = [
importer({
glob: true
}),
mapper({
maps: [
`${paths.src}/tokens/borders.json`,
`${paths.src}/tokens/breakpoints.json`,
`${paths.src}/tokens/colors.json`,
`${paths.src}/tokens/fonts.json`,
`${paths.src}/tokens/layers.json`,
`${paths.src}/tokens/sizes.json`,
`${paths.src}/tokens/spaces.json`
]
}),
assets({
loadPaths: [`${paths.src}/assets/vectors`]
}),
simpleVars,
apply,
calc,
customMedia,
colorFunction,
mediaMinMax,
nested,
responsiveType,
autoprefixer,
nano
];
// JavaScript files
const modules = [
`${paths.modules}/fontfaceobserver/fontfaceobserver.js`,
`${paths.modules}/prismjs/prism.js`,
`${paths.src}/assets/scripts/utils/selection.js`,
`${paths.src}/assets/scripts/utils/focusing.js`,
`${paths.src}/assets/scripts/app.js`,
`${paths.src}/components/**/*.js`
];
// --------------------------------------------------------
// Tasks
// --------------------------------------------------------
// Build static site
function build() {
const builder = fractal.web.builder();
builder.on('progress', (completed, total) => logger.update(`Exported ${completed} of ${total} items`, 'info'));
builder.on('error', err => logger.error(err.message));
return builder.build().then(() => {
logger.success('Fractal build completed!');
});
}
// Serve dynamic site
function serve() {
const server = fractal.web.server({
sync: true
});
server.on('error', err => logger.error(err.message));
return server.start().then(() => {
logger.success(`Fractal server is now running at ${server.url}`);
});
}
// Clean
function clean() {
return del(`${paths.dest}/assets/`);
}
// Deploy to GitHub pages
function deploy() {
// Generate CNAME file from `homepage` value in package.json
const cname = pkg.homepage.replace(/.*?:\/\//g, '');
fs.writeFileSync(`${paths.build}/CNAME`, cname);
// Push contents of build folder to `gh-pages` branch
return gulp.src(`${paths.build}/**/*`)
.pipe(ghPages({
force: true
}));
}
// Meta
function meta() {
return gulp.src(`${paths.src}/*.{txt,json}`)
.pipe(gulp.dest(paths.dest));
}
// Icons
function icons() {
return gulp.src(`${paths.src}/assets/icons/**/*`)
.pipe(imagemin())
.pipe(gulp.dest(`${paths.dest}/assets/icons`));
}
// Images
function images() {
return gulp.src(`${paths.src}/assets/images/**/*`)
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest(`${paths.dest}/assets/images`));
}
// Vectors
function vectors() {
return gulp.src(`${paths.src}/assets/vectors/**/*`)
.pipe(gulp.dest(`${paths.dest}/assets/vectors`));
}
// Scripts
function scripts() {
return gulp.src(modules)
.pipe(sourcemaps.init())
.pipe(babel({
presets: [['es2015', {
modules: false
}]]
}))
.pipe(concat('app.js'))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(`${paths.dest}/assets/scripts`));
}
// Styles
function styles() {
return gulp.src(`${paths.src}/assets/styles/*.css`)
.pipe(sourcemaps.init())
.pipe(postcss(processors))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(`${paths.dest}/assets/styles`));
}
// Watch
function watch() {
serve();
gulp.watch(`${paths.src}/assets/icons`, icons);
gulp.watch(`${paths.src}/assets/images`, images);
gulp.watch(`${paths.src}/assets/vectors`, images);
gulp.watch(`${paths.src}/**/*.js`, scripts);
gulp.watch(`${paths.src}/**/*.css`, styles);
}
// Task sets
const compile = gulp.series(clean, gulp.parallel(meta, icons, images, vectors, scripts, styles));
gulp.task('start', gulp.series(compile, serve));
gulp.task('build', gulp.series(compile, build));
gulp.task('dev', gulp.series(compile, watch));
gulp.task('publish', gulp.series(build, deploy));