-
Notifications
You must be signed in to change notification settings - Fork 21
/
gulpfile.js
108 lines (89 loc) · 2.5 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
/*
Tasks:
$ gulp --handle : Build the "handle" component
$ gulp watch : Starts a watch on all components.
Also starts a server at localhost:3000
*/
const { src, dest, watch, parallel, series } = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const clean = require('gulp-clean');
const cleancss = require('gulp-clean-css');
const typescript = require('gulp-typescript');
const replace = require('gulp-replace');
const fs = require('fs');
const connect = require('gulp-connect');
/** The webcomponent that is currently being build. */
let build = '';
const cleanup = directory => {
return src(`${directory}/${build}`, {
read: false,
allowEmpty: true
})
.pipe(clean());
}
const cleanupDist = () => {
return cleanup('dist');
}
const cleanupTemp = () => {
return cleanup('temp');
};
const css = () => {
return src(`src/${build}/index.scss`)
.pipe(sass().on('error', sass.logError))
.pipe(cleancss({
format: 'keep-breaks'
}))
.pipe(dest(`temp/${build}`));
};
const html = () => {
return src(`src/${build}/index.html`)
.pipe(dest(`temp/${build}`));
};
const js = () => {
return src( `src/${build}/**/*.ts`)
.pipe(
typescript({
target: 'es6',
module: 'es6'
})
)
.pipe(dest(`dist/${build}`));
};
const concat = () => {
const styles = fs.readFileSync(`temp/${build}/index.css`);
const html = fs.readFileSync(`temp/${build}/index.html`);
return src(`dist/${build}/index.js`)
.pipe(replace('<STYLE />', `<style>${styles}</style>`))
.pipe(replace('<HTML />', html))
.pipe(dest(`dist/${build}`));
};
const webcomponent = series(
cleanupDist,
parallel(html, css, js),
concat,
cleanupTemp
);
exports.default = async cb => {
build = process.argv[2];
if (build) {
build = build.replace('--', '');
}
try {
if (fs.statSync(`src/${build}`).isDirectory()) {
webcomponent()
}
} catch(err) {
console.log('\x1b[31m', `Webcomponent "${build}" not found.`, '\x1b[0m');
}
};
exports.watch = async cb => {
connect.server({
port: 3000
});
return watch('src/**/*')
.on('change', (path) => {
build = path.split('/')[1];
console.log('\x1b[32m', 'Change detected to "' + build + '" webcomponent.', '\x1b[0m');
webcomponent();
});
};