-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
176 lines (151 loc) · 4.87 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
/**
* Created by Dennis Espinoza on 10/04/16.
*/
var gulp = require('gulp');
var gutil = require('gulp-util');
var chalk = require('chalk');
var fs = require('fs');
var browserify = require('browserify');
var watchify = require('watchify');
var source = require("vinyl-source-stream");
var babelify = require('babelify');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var babel = require("gulp-babel");
var buffer = require('vinyl-buffer'); // Vinyl stream support
var rename = require('gulp-rename'); // Rename sources
var notify = require('gulp-notify'); // Provides notification to both the console and Growel
gulp.task('browser-sync', function () {
browserSync.init({
server: {
baseDir: './app',
routes: {
"/usuarios": "./app",
"/usuarios/usuario_a": "./app",
"/usuarios/usuario_b": "./app"
}
}
})
});
var files = [
{
input : ['./app/js/UsersForms.js'],
output : 'UsersFormsBuilt.js',
destination: './app/js/dist'
}
];
var Defer = function(max, callback) {
this.max = max;
this.count = 0;
this.callback = callback;
this.exec = function() {
if (this.max === ++this.count) {
this.callback();
}
};
};
function mapError(err) {
if (err.fileName) {
gutil.log(chalk.red(err.name)
+ ': ' + chalk.yellow(err.fileName.replace(__dirname + '/src/js/', ''))
+ ': ' + 'Line ' + chalk.magenta(err.lineNumber)
+ ' & ' + 'Column ' + chalk.magenta(err.columnNumber || err.column)
+ ': ' + chalk.blue(err.description));
} else {
gutil.log(chalk.red(err.name)
+ ': '
+ chalk.yellow(err.message));
}
}
var bundle = function(bundler, options) {
startTime = new Date().getTime();
return bundler.bundle()
.on('error', mapError)
.pipe(source('main.jsx'))
.pipe(buffer())
.pipe(rename(options.output))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./map'))
.pipe(gulp.dest(options.destination))
.pipe(notify({
message: 'Generated file: <%= file.relative %>'
}))
.pipe(browserSync.stream())
.on('end', function () {
time = (new Date().getTime() - startTime) / 1000;
console.log(options.output + ' was browserified: ' + time + 's');
});
};
var createBundleProp = function(b, options) {
var bundler = b;
var i = 0;
for (i; i < options.input.length; i++) {
if (options.require) {
bundler.require(options.input[i].require, {
expose: options.input[i].expose
});
} else {
bundler.add(options.input[i]);
}
};
return bundler;
};
var createBundle = function(options, d) {
var bundler = browserify({
cache: {},
packageCache: {},
fullPaths: false,
debug: true
})
.transform(babelify, {presets: ['es2015', 'react'], plugins : ["transform-object-rest-spread"]});
bundler = createBundleProp(bundler, options);
bundler = watchify(bundler);
bundler.on('update', function () {
bundle(bundler, options);
});
return bundle(bundler, options);
};
gulp.task("babel", function () {
return gulp.src("app/js/**/*.js")
.pipe(sourcemaps.init())
.pipe(babel())
.on('error', function (err) {
gutil.log(gutil.colors.red(err.message));
browserSync.notify("Babel Error!");
this.emit('end');
})
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("dist"))
.pipe(browserSync.stream());
});
var createBundles = function(bundles, defer) {
bundles.forEach(function (bundle) {
createBundle(bundle).on('end', function () {
defer.exec();
});
});
};
// copy styles of each component to dist
gulp.task('copy-styles', function () {
return gulp.src(['src/**/*.*css'], {base: './src/'})
.pipe(gulp.dest('scss/'))
.pipe(browserSync.stream());
});
// configure the sass task
gulp.task('sass', function () {
return gulp.src('app/scss/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('app/css'))
.pipe(browserSync.stream());
});
gulp.task('watch', ['browser-sync'], function () {
// gulp.watch('src/**/*.*css', ['copy-styles']);
gulp.watch(['app/scss/**/*.scss'], ['sass']);
gulp.watch("app/*.html").on('change', browserSync.reload);
});
gulp.task('browserify', function (done) {
var d = new Defer(files.length, done);
createBundles(files, d);
});
gulp.task('default', ['watch', 'browser-sync'/*, 'babel', 'copy-styles'*/, 'sass', 'browserify']);