forked from frend/frend.co
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
237 lines (226 loc) · 6.3 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
//----------------------------------------------------------------------
// Modules
//----------------------------------------------------------------------
// Files
var pkg = require('./package.json');
var config = require('./webpack.config.js');
// Global
var cache = require('gulp-cached');
var clean = require('gulp-clean');
var gulp = require('gulp');
var rename = require('gulp-rename');
var runSequence = require('run-sequence');
// CSS
var autoprefixer = require('autoprefixer');
var nano = require('gulp-cssnano');
var sass = require('gulp-sass');
var scsslint = require('gulp-scss-lint');
var sourcemaps = require('gulp-sourcemaps');
var postcss = require('gulp-postcss');
// JS
var concat = require('gulp-concat');
var eslint = require('gulp-eslint');
var uglify = require('gulp-uglify');
var webpack = require('webpack-stream');
// Components
var babelify = require('babelify');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var es = require('event-stream');
var glob = require('glob');
var source = require('vinyl-source-stream');
//----------------------------------------------------------------------
// Setup
//----------------------------------------------------------------------
var src = {
css: {
master: './css/master.scss',
project: [
'./css/**/*.scss',
'./css/master.scss',
'!./css/site/_generic.reset.scss',
'!./css/site/_components.syntax.scss',
'!./css/site/_tools.mq.scss'
],
output: 'global',
dist: './css/dist/'
},
js: {
initial: {
project: [
'./node_modules/fg-loadcss/src/loadCSS.js',
'./node_modules/fg-loadjs/loadJS.js',
'./js/modules/webfont-detector.js',
'./js/modules/feature-detection.js'
],
output: 'initial'
},
global: {
master: './js/master.js',
project: [
'./js/master.js',
'./js/modules/*.js',
'!./js/_vendor/*.js',
'!./js/dist/*.js'
],
output: 'global'
},
dist: './js/dist/'
},
component: {
prefix: 'Fr',
path: './_components/',
project: [
'./_components/**/*.js',
'!./_components/**/dist/*.js'
]
},
includes: './_includes/_assets/'
};
var options = {
sass: {
includePaths: ['./css/scss'],
errLogToConsole: true
},
autoprefixer: {
browsers: ['last 4 versions'],
flexbox: 'no-2009',
remove: false
},
babelify: {
presets: ['es2015'],
plugins: ['add-module-exports']
}
};
//----------------------------------------------------------------------
// Tasks
//----------------------------------------------------------------------
// Lint
//----------------------------------------------------------------------
gulp.task('lint:css', function () {
return gulp.src(src.css.project)
.pipe(cache('scsslint'))
.pipe(scsslint())
.pipe(scsslint.failReporter());
});
gulp.task('lint:js', function () {
return gulp.src(src.js.global.project)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('lint:component', function () {
return gulp.src(src.component.project)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// Clean
//----------------------------------------------------------------------
gulp.task('clean:css', function () {
return gulp.src(src.css.dist, { read: false }).pipe(clean());
});
gulp.task('clean:js', function () {
return gulp.src(src.js.dist, { read: false }).pipe(clean());
});
// Build
//----------------------------------------------------------------------
gulp.task('build:css', function () {
return gulp.src(src.css.master)
// compile & transform
.pipe(sass(options.sass))
.pipe(sourcemaps.init())
.pipe(postcss([
autoprefixer(options.autoprefixer)
]))
.pipe(sourcemaps.write())
// save minified output
.pipe(nano())
.pipe(rename(src.css.output + '.min.css'))
.pipe(gulp.dest(src.css.dist));
});
gulp.task('build:js:initial', function () {
return gulp.src(src.js.initial.project)
.pipe(concat(src.js.initial.output))
// save minified output
.pipe(uglify())
.pipe(rename(src.js.initial.output + '.min.js'))
.pipe(gulp.dest(src.js.dist))
.pipe(gulp.dest(src.includes));
});
gulp.task('build:js:global', function () {
return gulp.src(src.js.global.master)
// bundle modules
.pipe(webpack(config))
// save minified output
.pipe(uglify())
.pipe(rename(src.js.global.output + '.min.js'))
.pipe(gulp.dest(src.js.dist));
});
gulp.task('build:component', function (done) {
// use glob to catch file names/directories
glob(src.component.path + '*/*.js', function(err, files) {
// catch errors
if (err) done(err);
// create task per file
var tasks = files.map(function (entry) {
// get file props
var dir = entry.split(src.component.path)[1],
name = dir.split('/')[0];
// use this to expose standalone module name
return browserify({
entries: [entry],
debug: true, // includes sourcemaps
standalone: src.component.prefix + name
})
.transform(babelify, options.babelify)
.bundle()
.pipe(source(entry))
.pipe(buffer())
.pipe(uglify())
.pipe(rename({
dirname: '',
extname: '.min.js',
prefix: src.component.prefix.toLowerCase()
}))
.pipe(gulp.dest(src.component.path + name + '/dist'));
});
es.merge(tasks).on('end', done);
});
});
// Watch
//----------------------------------------------------------------------
gulp.task('watch', function () {
gulp.watch(src.css.project, ['lint:css', 'build:css']);
gulp.watch(src.js.global.project, ['lint:js', 'build:js:global']);
gulp.watch(src.component.project, ['lint:component', 'build:component']);
});
// Default tasks
//----------------------------------------------------------------------
// SITE
//---------------
// 'gulp' - Runs build
gulp.task('default', ['build']);
// 'gulp lint' - Lints CSS/JS project files
gulp.task('lint', ['lint:css', 'lint:js']);
// 'gulp clean' - Cleans CSS/JS dist directories
gulp.task('clean', ['clean:css', 'clean:js']);
// 'gulp build' - Lints, cleans, builds modernizr then builds CSS/JS output files, lint errors halt build
gulp.task('build', function (callback) {
runSequence(
['lint'],
['clean'],
['build:css', 'build:js:initial', 'build:js:global'],
callback
);
});
// COMPONENTS
//---------------
// 'gulp component' - Lints and builds JS output for components, lint errors halt build
gulp.task('component', function (callback) {
runSequence(
['lint:component'],
['build:component'],
callback
);
});