-
Notifications
You must be signed in to change notification settings - Fork 22
/
gulpfile.js
277 lines (213 loc) · 6.97 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
/*
* ------------------------------------------------------------------------
* Requires
* ------------------------------------------------------------------------
*/
// Node JS
const fs = require('fs');
const mkdirp = require('mkdirp');
var path = require('path');
// Gulps
const babel = require('gulp-babel');
const connect = require("gulp-connect");
const debug = require('gulp-debug');
const del = require('del');
const exit = require("gulp-exit");
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require("gulp-util");
const pug = require('gulp-pug');
const run = require('gulp-run');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const yargs = require('yargs');
// Vinyl
const browserify = require("browserify");
const tsify = require("tsify");
const watchify = require("watchify");
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
// For Post-CSS
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
/**
* ------------------------------------------------------------------------
* Vars
* ------------------------------------------------------------------------
*/
const argv = yargs.argv;
const hostPort = 60571;
let minify = false; // True : Enable minify on all files
let maps = true; // True : Create source maps
// Prevents task to stop being watched on error
function swallowError(error) {
gutil.log(error)
this.emit('end')
}
/**
* ------------------------------------------------------------------------
* App tasks
* ------------------------------------------------------------------------
*/
// TODO: Fix tasks
const browserifyOptions = {
basedir: '.',
debug: maps,
entries: ['src/main.ts'],
cache: {},
packageCache: {}
};
const tsifyOptions = require('./tsconfig').compilerOptions;
const normalBrowserify = browserify(browserifyOptions)
.plugin(tsify, tsifyOptions);
const watchedBrowserify = watchify(browserify(browserifyOptions))
.plugin(tsify, tsifyOptions);
watchedBrowserify.on('log', gutil.log);
// Browserify Functiom, use
const bundle = (toBundle) => {
// Log "Starting 'Bundle Function'..." at the beginning
gutil.log('Starting \'' + gutil.colors.cyan('Bundle Function') + ' 📦\'...');
return toBundle
.bundle()
// Catch error
.on('error', swallowError)
// Start Pipe
.pipe(source('bundle.js'))
// Prepare maps
.pipe(buffer())
.pipe(gulpif(maps, sourcemaps.init({ loadMaps: true })))
// Babel
.pipe(gulpif(minify, babel({
presets: ['@babel/env']
})))
// Uglify
.pipe(gulpif(minify, uglify()))
// Write maps
.pipe(gulpif(maps, sourcemaps.write('./maps')))
// Save
.pipe(gulp.dest("dist"))
// Log done at end
.on('end', () => gutil.log('Finished \'' + gutil.colors.cyan('Bundle Function') + ' 📦\''))
}
gulp.task('pug', () => {
const options = {
pretty: !minify
};
return gulp.src('src/**/[^_]*.pug')
.pipe(pug(options))
.on('error', swallowError)
.pipe(gulp.dest('dist'));
});
gulp.task('sass', () => {
const options = {
outputStyle: minify ? 'compressed' : 'nested'
};
const plugins = [
autoprefixer({ browsers: ['chrome >= 36', 'firefox >= 45'] })
];
return gulp.src('src/**/*.scss')
// New sourcemaps, loading inline sourcemap from Sass' sourcemap.
.pipe(gulpif(maps, sourcemaps.init()))
// Compile SASS.
.pipe(sass(options).on('error', sass.logError))
// Run compiled CSS through autoprefixer.
.pipe(postcss(plugins))
// Write sourcemap to a separate file.
.pipe(gulpif(maps, sourcemaps.write('./maps')))
// Write CSS file to desitination path.
.pipe(gulp.dest('dist'));
});
gulp.task('i18n', () => {
var sourceFiles = [ 'src/i18n/**/*'];
var destination = 'dist/i18n/';
return gulp
.src(sourceFiles)
.pipe(gulp.dest(destination));
});
gulp.task('assets', () => {
var sourceFiles = [ 'src/assets/**/*'];
var destination = 'dist/assets/';
return gulp
.src(sourceFiles)
.pipe(gulp.dest(destination));
});
/**
* ------------------------------------------------------------------------
* Other tasks
* ------------------------------------------------------------------------
*/
gulp.task('connect', (cb) => {
connect.server({
root: ['dist'],
// fallback: 'dist/index.html', // Enable this if not using angular HashLocationStrategy
port: hostPort,
https: false
});
cb();
});
gulp.task('clean', () => {
return del([
'dist'
]);
});
/**
* ------------------------------------------------------------------------
* Terminal Tasks
* ------------------------------------------------------------------------
*/
// New component
// $ gulp newcomp --name your-component
// Tested in mac, not sure in linux
gulp.task('newcomp', function () {
var nameDashed = argv.name;
var nameCapitalized = ("-" + nameDashed).replace(/-([a-z])/g, function (g) { return g[1].toUpperCase() });
return gulp
.src('src/app')
.pipe(run('touch src/app/' + nameDashed + '.component.pug'))
.pipe(run('touch src/app/' + nameDashed + '.component.scss'))
.pipe(run('cp templates/new-component.component.ts.template src/app/' + nameDashed + '.component.ts'))
.pipe(run("sed -i '' 's/compNameDashed/" + nameDashed + "/g' src/app/" + nameDashed + ".component.ts"))
.pipe(run("sed -i '' 's/compNameCapitalized/" + nameCapitalized + "/g' src/app/" + nameDashed + ".component.ts"))
.pipe(gulp.dest('src/app'));
});
/**
* ------------------------------------------------------------------------
* Executor
* ------------------------------------------------------------------------
* $ gulp build : Build once
* $ gulp : Build and watch
*/
gulp.task('build', gulp.series('clean', 'pug', 'sass', 'i18n', 'assets', (done) => {
// gulp.series(
// 'pug',
// 'sass',
// 'i18n',
// 'assets'
// );
return bundle(normalBrowserify);
}));
gulp.task('build-prod', gulp.series('clean', (done) => {
minify = true;
maps = false;
done();
}, 'build'));
gulp.task('default', gulp.series('clean',
// Initials
'pug', 'sass', 'i18n', 'assets',
(cb) => {
// bundle(watchedBrowserify);
return bundle(watchedBrowserify);
},
'connect',
// More
(cb) => {
watchedBrowserify.on('update', () => bundle(watchedBrowserify))
gulp.watch('src/**/*.pug', gulp.series('pug'));
gulp.watch('src/**/*.scss', gulp.series('sass'));
gulp.watch('src/i18n/**/*.json', gulp.series('i18n'));
gulp.watch('src/assets/**/*.*', gulp.series('assets'));
cb();
})
);