This repository was archived by the owner on Apr 1, 2024. It is now read-only.
forked from dashed/javascript-seed-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
149 lines (106 loc) · 3.08 KB
/
Copy pathgulpfile.js
File metadata and controls
149 lines (106 loc) · 3.08 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
/*jshint node:true */
/* gulpfile.js - https://github.com/gulpjs/gulp */
var
/** node.js **/
path = require('path'),
fs = require('fs'),
/** Gulp and plugins **/
gulp = require('gulp'),
gutil = require('gulp-util'),
watch = require('gulp-watch'),
plumber = require('gulp-plumber'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
mocha = require('gulp-mocha'),
/** utility **/
through = require('through2').obj,
/** webpack **/
webpack = require('webpack'),
webpackconfig = require('./webpack.config'),
/** Config **/
distDir = './dist/',
distTargetFile = 'myawesomeproject.js',
/** Environment Vars **/
R = 0,
ENV_SWITCH = void 0,
// Env list
DEV_ENV = R++,
PROD_ENV = R++;
/** Utility Functions **/
// Transformation function on gulp.src depending on env
var getGlob = function(glob_target) {
var src = gulp.src(glob_target);
switch(ENV_SWITCH) {
case DEV_ENV:
// watch files and re-emit them downstream on change (or some file event)
opts.glob = glob_target;
return watch(opts)
.pipe(plumber())
.pipe(through());
case PROD_ENV:
return src.pipe(plumber())
.pipe(gutil.noop());
default:
throw new Error('Invalid Env');
}
};
/* Sub-tasks */
gulp.task('set-dev', function() {
ENV_SWITCH = DEV_ENV;
});
gulp.task('set-prod', function() {
ENV_SWITCH = PROD_ENV;
});
gulp.task('mocha', function() {
var mocha_opts = {};
try {
var opts = fs.readFileSync('test/mocha.opts', 'utf8')
.trim()
.split(/\s+/);
opts.forEach(function(val, indx, arry) {
if (/^-.+?/.test(val)) {
val = val.replace(/^-+(.+?)/, "$1");
mocha_opts[val] = arry[indx + 1];
}
});
} catch (err) {
// ignore
}
return watch({ glob: 'test/**/*.js', read:false }, function(files) {
files
.pipe(mocha(mocha_opts))
.on('error', function(err) {
if (!/tests? failed/.test(err.stack)) {
console.log(err.stack);
}
});
});
});
gulp.task('dist-minify', function() {
return getGlob(distDir + '/' + distTargetFile)
.pipe(rename({ suffix: '.min'}))
.pipe(uglify({ outSourceMap: true }))
.pipe(gulp.dest(distDir));
});
gulp.task('webpack', function(callback) {
webpack(webpackconfig, function(err, stats) {
if(err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
// output options
}));
});
callback();
});
/* High-level tasks */
/* Compose sub-tasks to orchestrate something to be done */
/* Development task */
gulp.task('dev', ['set-dev', 'webpack', 'mocha'], function() {
// Run webpack based on config from webpack.config.js
});
gulp.task('prod', ['set-prod', 'dist-minify'], function() {
// Minify file and generate dist source map file.
});
// The default task (called when you run `gulp`)
gulp.task('default', ['dev'], function() {
// Run dev task by default
});