-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
175 lines (155 loc) · 4.57 KB
/
Copy pathgulpfile.js
File metadata and controls
175 lines (155 loc) · 4.57 KB
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
const gulp = require('gulp');
const shell = require('gulp-shell');
const filter = require('gulp-filter-each');
const fileInclude = require('gulp-file-include');
const through = require('through2');
const prettyHtml = require('gulp-pretty-html');
const sass = require('gulp-sass')(require('sass'));
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync').create();
const tailwindcss = require('tailwindcss');
const tailwindConfig = require('./tailwind.config');
const postcss = require('gulp-postcss');
const port = 3001;
gulp.task('build-template', async function () {
return new Promise((resolve) => {
gulp
.src('src/template/*.html')
.pipe(filter((content) => content.match(/<html>/gi)))
.pipe(
fileInclude({
prefix: '@@',
basepath: '@file',
})
)
.pipe(
through.obj(async function (file, enc, cb) {
let html = file.contents.toString();
// merge css link and scripts to head
const links = [];
const cssRegx = /<link .*\/>\n/g;
html = html.replace(cssRegx, (match) => {
if (links.indexOf(match) === -1) {
links.push(match);
}
return '';
});
const scriptRegx = /<script .*><\/script>\n/g;
html = html.replace(scriptRegx, (match) => {
if (links.indexOf(match) === -1) {
links.push(match);
}
return '';
});
html = html.replace(
'<!-- Please DO NOT remove this line, all link and script tags will be merged to here -->\n',
links.join('')
);
// remove comment
html = html.replace(/<!-- prettier-ignore -->/g, '');
// change path from absolute to relative
if (require('yargs').argv.relativePath) {
html = html.replace(/href="\/css\//g, 'href="./css/');
html = html.replace(
/--src:url\(\/assets\//g,
'--src:url(../assets/'
);
html = html.replace(/src="\/assets\//g, 'src="./assets/');
}
file.contents = Buffer.from(html);
this.push(file);
cb();
})
)
.pipe(prettyHtml())
.pipe(gulp.dest('./web'))
.on('end', resolve);
});
});
gulp.task('build-scss', async function () {
return new Promise((resolve) => {
gulp
.src('src/scss/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({ cascade: false }))
.pipe(gulp.dest('./web/css'))
.on('end', resolve);
});
});
gulp.task('build-tailwind', async function () {
return new Promise((resolve) => {
gulp
.src('src/scss/tailwind.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('src/css'))
.pipe(postcss([tailwindcss(tailwindConfig), require('autoprefixer')]))
.pipe(autoprefixer({ cascade: false }))
.pipe(gulp.dest('./web/css'))
.on('end', resolve);
});
});
gulp.task('copy-css', async function () {
return new Promise((resolve) => {
gulp
.src('src/css/*.css')
.pipe(autoprefixer({ cascade: false }))
.pipe(gulp.dest('./web/css'))
.on('end', resolve);
});
});
gulp.task('copy-js', async function () {
return new Promise((resolve) => {
gulp.src('src/js/**/*.*').pipe(gulp.dest('./web/js')).on('end', resolve);
});
});
gulp.task('copy-assets', async function () {
return new Promise((resolve) => {
gulp
.src('src/assets/*.*')
.pipe(gulp.dest('./web/assets'))
.on('end', resolve);
});
});
gulp.task('copy-fonts', async function () {
return new Promise((resolve) => {
gulp.src('src/fonts/*.*').pipe(gulp.dest('./web/fonts')).on('end', resolve);
});
});
gulp.task(
'build',
gulp.series(
'build-template',
'build-scss',
'copy-css',
'copy-js',
'copy-assets',
'copy-fonts',
'build-tailwind'
)
);
gulp.task('watch', function (resolve) {
const watchSrcFolders = [
'src/template/**',
'src/scss/**',
'src/css/**',
'src/js/**' /*, 'src/assets/**'*/,
];
gulp
.watch(watchSrcFolders, gulp.series('build'))
.on('change', browserSync.reload);
resolve();
});
gulp.task('server', function () {
browserSync.init({
ui: false,
server: {
port,
baseDir: './web',
directory: true,
},
});
});
// dev server
gulp.task('dev', gulp.series('build', 'watch', 'server'));
gulp.task('deploy-init', gulp.series(shell.task(['firebase init'])));
gulp.task('deploy', gulp.series('build', shell.task(['firebase deploy'])));