-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
173 lines (146 loc) · 4.56 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
const es = require('event-stream')
const gulp = require('gulp')
const minimist = require('minimist')
const fs = require('fs')
const _ = require('lodash')
const path = require('path')
const mergeStream = require('merge-stream')
const stripAnsi = require('strip-ansi')
const plugins = require('gulp-load-plugins')();
const {
concat,
uglify,
zip,
plumber,
babel,
notify,
stripComments: strip
} = plugins
const gulpif = plugins.if
const publicScripts = require('./public/app.scripts.json')
const source = {
public: {
src: [
// bootstrap angular
'public/modules/main.js',
// main module
'public/modules/app.js',
// module files
'public/modules/**/module.js',
// other js files [controllers, services, etc.]
'public/modules/**/!(module)*.js'
]
}
}
const destinations = {
public: {
js: 'public/build'
}
}
const appWasBroken = {
public: false
}
gulp.task('public-js', buildApp('public'))
// creates task to concat angular files
function buildApp(fileset) {
debugger;
return function () {
let isBroken = false
gulp.src(source[fileset].src)
.pipe(plumber({
errorHandler: error => {
isBroken = true
appWasBroken[fileset] = true
const strippedError = Object.create(error)
strippedError.stack = stripAnsi(error.stack)
return notify.onError("<%= error.stack %>")(strippedError)
}
}))
.pipe(babel({
presets: ['es2015']
}))
// .pipe(uglify())
.pipe(concat('app.js'))
.pipe(gulp.dest(destinations[fileset].js))
.pipe(gulpif(
file => {
if (!appWasBroken[fileset] || isBroken) return false
appWasBroken[fileset] = false
return true
},
notify(`${fileset} app fixed!`)
))
}
}
gulp.task('watch', ['public-js'], function () {
gulp.watch(source.public.src, ['public-js'])
})
// builds vendor files listed in app.scripts.json
gulp.task('vendor-public', buildPublicVendor)
function buildPublicVendor(scripts, dest) {
return buildVendor(publicScripts, destinations.public.js)
}
function buildAdminVendor(scripts, dest) {
return buildVendor(public, destinations.admin.js)
}
function buildVendor(scripts, dest) {
debugger;
let tasks = []
_.forIn(scripts.chunks, function (chunkScripts, chunkName) {
let paths = []
chunkScripts.forEach(function (script) {
let scriptFileName = scripts.paths[script]
let scriptPath = path.join(__dirname, scriptFileName)
if (!fs.existsSync(scriptPath)) {
throw console.error(`Required path doesn't exist: ${scriptPath}`, script)
}
paths.push(scriptFileName)
})
tasks.push(gulp.src(paths)
.pipe(concat(chunkName + '.js'))
.on('error', swallowError)
.pipe(strip())
.pipe(uglify())
.pipe(gulp.dest(dest)))
})
return mergeStream(tasks)
}
gulp.task('default', ['dev'])
gulp.task('dev', ['vendor-public', 'public-js', 'watch'])
const knownOptions = {
string: 'packageName',
string: 'packagePath',
default: {
packageName: 'Package.zip',
packagePath: path.join(__dirname, '_package')
}
}
const options = minimist(process.argv.slice(2), knownOptions)
// This task is specifically setup for deploying to AZURE.
gulp.task('prod', ['vendor-public', 'public-js'])
function buildProdPackage() {
let packagePaths = ['**',
'!**/_package/**',
'!**/typings/**',
'!typings',
'!_package',
'!gulpfile.js',
'!**/client/libs/**'
]
// add exclusion patterns for all dev dependencies
let packageJSON = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'))
let devDeps = packageJSON.devDependencies
for (let propName in devDeps) {
let excludePattern1 = '!**/node_modules/' + propName + '/**'
let excludePattern2 = '!**/node_modules/' + propName
packagePaths.push(excludePattern1)
packagePaths.push(excludePattern2)
}
return gulp.src(packagePaths)
.pipe(zip(options.packageName))
.pipe(gulp.dest(options.packagePath))
}
const swallowError = function (src, task, error) {
console.log(error.toString())
this.emit('end')
}