-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
94 lines (79 loc) · 2.27 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
'use strict';
const gulp = require('gulp');
const log = require('fancy-log');
const del = require('del');
const browserSync = require('browser-sync').create();
const portfinder = require('portfinder');
const {spawn} = require('child_process');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pixels-to-rem');
const eslint = require('gulp-eslint');
const config = require('./.buildconfig.json');
const csslit = require('gulp-csslit');
const rename = require('gulp-rename');
const BUILD_DIRECTORY = 'dist/';
const TMP_DIRECTORY = '.tmp/';
gulp.task('clean', () => del.sync([BUILD_DIRECTORY, TMP_DIRECTORY]));
const startServer = (port) => {
const polymerCliParams = ['serve', '--npm', '-p', port];
const polymerServe = spawn('polymer', polymerCliParams);
polymerServe.stdout.on('data', (data) => {
log(`${data}`);
browserSync.init(getServerConfig(port));
});
polymerServe.stderr.on('data', (data) => {
log(`Error: ${data}`);
});
polymerServe.on('close', (code) => {
log(`child process exited with code ${code}`);
});
};
const getServerConfig = (port) => {
return JSON.parse(
JSON.stringify(config.browsersync)
.replace(/{{port}}/g, port)
);
};
gulp.task('styles', () => {
return gulp.src(['src/**/*.css'])
.pipe(postcss([
pxtorem(config.pxtorem),
autoprefixer(config.autoprefixer),
]))
.pipe(csslit())
.pipe(rename({extname: '.css.js'}))
.pipe(gulp.dest(TMP_DIRECTORY));
});
gulp.task('eslint', () => {
return gulp.src(['src/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(gulp.dest(TMP_DIRECTORY));
});
gulp.task('build:dist', ['clean', 'styles', 'eslint'], () => {
return gulp.src(['.tmp/**/*.js'])
.pipe(gulp.dest(BUILD_DIRECTORY));
});
gulp.task('start-server', () => {
return portfinder.getPortPromise()
.then(startServer)
.catch((err) => log(err));
});
gulp.task('watch:sources', () => {
gulp.watch(['src/**/*'], ['styles', 'eslint', 'build:dist']);
});
gulp.task('watch:dist', () => {
gulp.watch([
'index.html',
'styles.css',
'dist/**/*',
'test/**/*',
]).on('change', browserSync.reload);
});
gulp.task('serve', [
'build:dist',
'start-server',
'watch:sources',
'watch:dist',
]);