-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGulpfile.js
More file actions
135 lines (123 loc) · 4.97 KB
/
Gulpfile.js
File metadata and controls
135 lines (123 loc) · 4.97 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
/* jslint node: true */
/* eslint-env node*/
'use strict'
var gulp = require('gulp') // Base gulp package
var babelify = require('babelify') // Used to convert ES6 & JSX to ES5
var browserify = require('browserify') // Providers "require" support, CommonJS
var notify = require('gulp-notify') // Provides notification to both the console and Growel
var rename = require('gulp-rename') // Rename sources
var sourcemaps = require('gulp-sourcemaps') // Provide external sourcemap files
var livereload = require('gulp-livereload') // Livereload support for the browser
var gutil = require('gulp-util') // Provides gulp utilities, including logging and beep
var chalk = require('chalk') // Allows for coloring for logging
var source = require('vinyl-source-stream') // Vinyl stream support
var buffer = require('vinyl-buffer') // Vinyl stream support
var watchify = require('watchify') // Watchify for source changes
var merge = require('utils-merge') // Object merge tool
var duration = require('gulp-duration') // Time aspects of your gulp process
var connect = require('connect')
var serverStatic = require('serve-static')
var gulpSass = require('gulp-sass')
var depcheck = require('depcheck') // dependency check
const path = require('path')
// Configuration for Gulp
var config = {
js: {
src: './js/app.js',
watch: './js/**/*',
outputDir: './build/js/',
outputFile: 'bundle.js'
},
sass: {
src: './sass/main.scss',
watch: './sass/**/*',
outputDir: './build/css/',
outputFile: 'main.css'
}
}
// Error reporting function
function mapError (err) {
if (err.fileName) {
// Regular error
gutil.log(chalk.red(err.name) +
': ' + chalk.yellow(err.fileName.replace(path.join(__dirname, '/js/'), '')) +
': ' + 'Line ' + chalk.magenta(err.lineNumber) +
' & ' + 'Column ' + chalk.magenta(err.columnNumber || err.column) +
': ' + chalk.blue(err.description))
} else {
// Browserify error..
gutil.log(chalk.red(err.name) +
': ' +
chalk.yellow(err.message))
}
}
// Completes the final file outputs
function bundle (bundler) {
var bundleTimer = duration('Javascript bundle time')
bundler
.bundle()
.on('error', mapError) // Map error reporting
.pipe(source('app.js')) // Set source name
.pipe(buffer()) // Convert to gulp pipeline
.pipe(rename(config.js.outputFile)) // Rename the output file
.pipe(sourcemaps.init({loadMaps: true})) // Extract the inline sourcemaps
.pipe(sourcemaps.write('./map')) // Set folder for sourcemaps to output to
.pipe(gulp.dest(config.js.outputDir)) // Set the output folder
.pipe(notify({
message: 'Generated file: <%= file.relative %>'
})) // Output the file being created
.pipe(bundleTimer) // Output time timing of the file creation
.pipe(livereload()) // Reload the view in the browser
}
function basicBundler () {
var args = merge(watchify.args, { debug: true }) // Merge in default watchify args with browserify arguments
var bundler = browserify(config.js.src, args) // Browserify
.plugin(watchify, {ignoreWatch: ['**/node_modules/**', '**/bower_components/**'], poll: false}) // Watchify to watch source file changes
.transform(babelify, {presets: ['es2015', 'react']}) // Babel tranforms
return bundler
}
function depCheck () {
depcheck(__dirname, {}, function (unused) {
// depcheck('/home/fanick/gitrepos/fanick1.github.io',{}, function(unused){
if (unused.dependencies) {
console.log('unused dependencies:', unused.dependencies) // an array containing the unused dependencies
}
if (unused.devDependencies) {
console.log('unusued devDependencies:', unused.devDependencies) // an array containing the unused devDependencies
}
if (unused.invalidFiles) {
console.log('unused invalidFiles:', unused.invalidFiles) // files that cannot access or parse
}
if (unused.invalidDirs) {
console.log('unused invalidDirs', unused.invalidDirs) // directories that cannot access
}
})
}
gulp.task('styles', function () {
gulp.src(config.sass.src)
.pipe(gulpSass().on('error', gulpSass.logError))
.pipe(rename(config.sass.outputFile))
.pipe(gulp.dest(config.sass.outputDir))
.pipe(livereload())
})
// Gulp task for build -- run 'gulp' to start
gulp.task('default', ['styles'], function () {
livereload.listen() // Start livereload server
connect().use(serverStatic(__dirname)).listen(8000)
console.log('Started server on port 8000') // directories that cannot access
var bundler = basicBundler()
// TODO true ->add uglifyify dependency
if (true) { // TODO: replace with some proper environment checking
bundler = bundler
.transform({
global: true
}, 'uglifyify')
}
depCheck()
bundle(bundler) // Run the bundle the first time (required for Watchify to kick in)
bundler.on('update', function () {
bundle(bundler) // Re-run bundle on source updates
depCheck()
})
gulp.watch(config.sass.watch, ['styles']) // start watching scss folder, run styles upon change
})