-
Notifications
You must be signed in to change notification settings - Fork 36
/
gulpfile.js
139 lines (126 loc) · 4.24 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
/*
AMDClean Build File
*/
var gulp = require('gulp'),
uglify = require('gulp-uglify'),
jshint = require('gulp-jshint'),
jasmine = require('gulp-jasmine'),
rename = require('gulp-rename'),
insert = require('gulp-insert'),
requirejs = require('requirejs'),
fs = require('fs'),
amdclean = require('./build/amdclean'),
packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')),
licenseText = '\n\n/*' + fs.readFileSync('./LICENSE.txt', 'utf8') + '\n*/\n\n',
currentDate = (function() {
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth() + 1,
yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = yyyy + '-' + mm + '-' + dd;
return today;
}()),
currentYear = (function() {
var today = new Date(),
yyyy = today.getFullYear();
return yyyy;
}()),
headerText = '/*! amdclean - v' + packageJson.version + ' - ' + currentDate +
'\n* http://gregfranko.com/amdclean' +
'\n* Copyright (c) ' + currentYear + ' Greg Franko */\n',
error = false,
cachedBuiltLibText = fs.readFileSync('./src/amdclean.js', 'utf8');
revertFile = function() {
fs.writeFileSync('./src/amdclean.js', cachedBuiltLibText);
};
gulp.task('build', function(cb) {
requirejs.optimize({
'findNestedDependencies': false,
'baseUrl': './src/modules/',
'optimize': 'none',
'paths': {
'amdclean': 'index'
},
'include': ['amdclean'],
'out': './src/amdclean.js',
'onModuleBundleComplete': function(data) {
var outputFile = data.path,
cleanedCode = (function() {
try {
return amdclean.clean({
'filePath': outputFile,
'transformAMDChecks': false,
'aggressiveOptimizations': true,
'ignoreModules': ['esprima', 'estraverse', 'escodegen', 'lodash', 'fs', 'sourcemap_to_ast'], // wtf? parsed name here?
'removeUseStricts': false,
'wrap': {
// All of the third party dependencies are hoisted here
// It's a hack, but it's not too painful
'start': ';(function(esprima, estraverse, escodegen, _, sourcemapToAst) {\n',
'end': '}(typeof esprima !== "undefined" ? esprima: null, typeof estraverse !== "undefined" ? estraverse: null, typeof escodegen !== "undefined" ? escodegen: null, typeof _ !== "undefined" ? _ : null, typeof sourcemapToAst !== "undefined" ? sourcemapToAst : null));'
},
'createAnonymousAMDModule': true
});
} catch (e) {
error = true;
revertFile();
return '' + e;
}
}()),
fullCode = headerText + licenseText + cleanedCode;
if (error) {
revertFile();
console.log('Looks like there was an error building, stopping the build... ' + cleanedCode);
return;
}
fs.writeFileSync(outputFile, fullCode);
}
}, function() {
if (!error) {
cb();
}
}, function(err) {
revertFile();
console.log('Looks like there was an error building, stopping the build... ');
return cb(err); // return error
});
});
gulp.task('lint', ['build'], function() {
gulp.src('src/amdclean.js')
.pipe(jshint({
'evil': true,
'loopfunc': true
}))
.pipe(jshint.reporter('default'));
});
gulp.task('test', ['build', 'lint'], function() {
gulp.src('test/specs/convert.js')
.pipe(jasmine());
});
gulp.task('test-only', function() {
gulp.src('test/specs/convert.js')
.pipe(jasmine());
});
gulp.task('minify', ['build', 'lint', 'test'], function() {
gulp.src(['src/amdclean.js'])
.pipe(gulp.dest('build/'))
.pipe(uglify())
.pipe(insert.prepend(headerText + licenseText))
.pipe(rename('amdclean.min.js'))
.pipe(gulp.dest('build/'));
});
// The default task (called when you run `gulp`)
gulp.task('default', ['build', 'lint', 'test', 'minify']);
// The watch task that runs the default task on any AMDclean module file changes
gulp.task('watch', function() {
var watcher = gulp.watch('src/modules/*.js', ['default']);
watcher.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
});