-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
175 lines (150 loc) · 4.43 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
169
170
171
172
173
174
175
import gulp from 'gulp';
import browserSync from 'browser-sync';
import pug from 'gulp-pug';
import del from 'del';
import runSequence from 'run-sequence';
import gulpLoadPlugins from 'gulp-load-plugins';
import browserify from 'browserify';
import babelify from 'babelify';
import fs from 'fs';
import archieml from 'archieml';
// http://blog.revathskumar.com/2016/02/browserify-with-gulp.html
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
// Google apis
import { drive } from 'googleapis';
// Arquivo de config
import { Defaults, Paths, DocsGoogle } from './config';
// Loading Plugins
const $ = gulpLoadPlugins();
// Recebendo a variável de ambiente
const ENV = process.env.NODE_ENV || 'DEV';
// DEFAULT's
const { s3URL, qaURL } = Defaults;
// PATH's dos estáticos
const { images, styles, scripts, views } = Paths;
function initGoogleApi(resolve, reject) {
drive('v3').files.export({
auth: DocsGoogle['apiKey'],
fileId: DocsGoogle['idFile'],
mimeType: 'text/plain'
},
function(err, response) {
resolve(response);
reject(err);
});
}
let DATA = "";
// Retorna os dados da planilha
gulp.task('googledocssapi', () => {
if (DocsGoogle['enable']) {
return new Promise(initGoogleApi).then(function(file) {
DATA = archieml.load(file);
fs.writeFile('./dados.json', JSON.stringify(archieml.load(file)));
});
}
});
// Scripts
gulp.task('browserify', () => {
const bundler = browserify({ entries: './app/assets/js/main.js', debug: true });
return bundler
.transform(babelify)
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.uglify())
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./public/dev/js'))
.pipe(browserSync.stream());
});
// Images
gulp.task('images', () => {
gulp.src(images)
.pipe($.cache($.imagemin({ optimizationLevel: 5, progressive: true, interlaced: true })))
.pipe(gulp.dest('./public/dev/img'))
.pipe(browserSync.stream());
});
// Sass
gulp.task('sass', () => {
return gulp.src(styles)
.pipe($.plumber())
// Verifica o ambiente e add um valor a variavel $s3URL
.pipe($.sassVariables({ $s3URL : ( ENV === 'PROD' ? s3URL : ENV === 'QLT' ? qaURL : '' ) }))
.pipe($.sass())
.pipe($.sourcemaps.init())
.pipe($.cssnano({ autoprefixer: { browsers: 'last 5 versions', add: true } }))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./public/dev/css/'))
.pipe(browserSync.stream());
});
// Views
gulp.task('views', () => {
return gulp.src('./app/views/index.pug')
.pipe($.plumber())
.pipe(pug({
pretty: true,
data: {
url: '',
prod: false,
dadosDocs: DATA
}
}))
.pipe(gulp.dest('./public/dev/'))
.pipe(browserSync.stream());
});
// Views Build
gulp.task('views:build', () => {
return gulp.src('./app/views/index.build.pug')
.pipe(pug({
pretty: true,
data: {
url: ENV === 'PROD' ? s3URL : qaURL,
prod: true,
dadosDocs: DATA
}
}))
.pipe($.concat('index.html'))
.pipe(gulp.dest('./public/build'));
});
// Watch
gulp.task('watch', () => {
gulp.watch(scripts, ['browserify']);
gulp.watch(images, ['images']);
gulp.watch(styles, ['sass']);
gulp.watch(views, ['views']);
}).on('change', browserSync.reload);
// Live browser
gulp.task('browserLive', () => {
browserSync.init({
port: 8000,
server: {
baseDir: [
'./public/dev'
]
}
});
});
// Copy
gulp.task('copy',() => {
gulp.src([
'./public/dev/**',
'!./public/dev/*.html',
'!./public/dev/**/*.map',
'!./public/dev/fonts/**',
'!./public/dev/fonts'
], {
dot: true
}).pipe(gulp.dest('./public/build'))
});
// Clean BUILD
gulp.task('clean', () => del(['./public/build/**'], { dot: true }) );
// // Task's
// gulp.task('default', ['googledocssapi'], () => runSequence(['browserify', 'sass','images','watch','views','browserLive']));
// /* O Sass só esta aqui para forçar a troca da variable de $s3URL, que será "append" dinamicamente. */
// gulp.task('build', ['clean', 'sass' ,'googledocssapi'], () => runSequence(['views:build','copy']));
// Task's
gulp.task('default', () => runSequence(['browserify', 'sass','images','watch','views','browserLive']));
/* O Sass só esta aqui para forçar a troca da variable de $s3URL, que será "append" dinamicamente. */
gulp.task('build', ['clean', 'sass'], () => runSequence(['views:build','copy']));