-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
191 lines (152 loc) · 5.26 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
/* general libraries */
var fs = require('fs');
var _ = require('lodash');
/* specific task libraries */
var gulp = require('gulp');
var gutil = require('gulp-util');
var jshint = require('gulp-jshint');
var webpack = require('webpack');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var karma = require('gulp-karma');
var bump = require('gulp-bump');
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function externalModule(modName, varName) {
if (!varName) { return modName }
var obj = {};
obj[modName] = { root: varName, commonjs2: modName, commonjs: modName, amd: modName };
return obj;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var INTERNAL_LIBRARIES = [];
var EXTERNAL_LIBRARIES = [];
var APPLICATIONS = [];
fs.readdirSync('./modules')
.map(function (filename) { return fs.readFileSync('./modules/'+filename) })
.map(JSON.parse)
.forEach(function (mod) {
if (mod.type === 'external-library') {
mod.webpackExternal = externalModule(mod.name, mod.var);
EXTERNAL_LIBRARIES.push(mod);
} else if (mod.type === 'internal-library') {
INTERNAL_LIBRARIES.push(mod);
} else if (mod.type === 'application') {
APPLICATIONS.push(mod);
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* tell Webpack where to find specific external library files */
var WEBPACK_ALIAS = {};
EXTERNAL_LIBRARIES.forEach(function (mod) {
WEBPACK_ALIAS[mod.name] = mod.dir + '/' + mod.file;
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
gulp.task('lint', function () {
return gulp.src('src/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
INTERNAL_LIBRARIES.concat(APPLICATIONS).forEach(function (m) {
/* Webpack configuration shared for both apps and libraries */
var commonConfig = {
devtool: 'inline-source-map',
module: {
loaders: [
{ test: /\.scss$/, loader: "style!css!autoprefixer!sass" },
{ test: /\.css$/, loader: "style!css!autoprefixer" },
{ test: /\.js/, loader: "traceur?script" },
{ test: /\.(png|jpg)/, loader: "file" }
]
}
};
/* the webpack task for the internal module */
gulp.task('webpack:' + m.name, function (callback) {
// output after Webpack does its thing
function webpackCallback(err, stats) {
if (err) { throw new gutil.PluginError('webpack', err) }
gutil.log(stats.toString({ colors: true }));
callback();
}
if (m.type === 'internal-library') {
webpack(_.defaults({
entry: './src/' + m.file,
externals: EXTERNAL_LIBRARIES.map(function (lib) { return lib.webpackExternal }),
output: {
path: './dist',
filename: m.file,
library: m.var,
libraryTarget: 'umd',
sourceMapFilename: m.file+'.map'
}
}, commonConfig), webpackCallback);
} else if (m.type === 'application') {
webpack(_.defaults({
entry: './src/' + m.dir + '/' + m.file,
output: {
path: './dist/' + m.dir,
filename: m.file,
sourceMapFilename: m.file+'.map'
},
resolve: {
modulesDirectories: ['node_modules', 'bower_components'],
alias: WEBPACK_ALIAS
},
target: 'web'
}, commonConfig), webpackCallback);
}
});
/* the build-task for internal libraries */
if (m.type === 'internal-library') {
gulp.task('uglify:' + m.name, ['webpack:' + m.name], function () {
return gulp.src('dist/**/' + m.file)
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('dist'));
});
gulp.task('build:' + m.name, ['webpack:' + m.name, 'uglify:' + m.name]);
}
/* the build-task for applications */
if (m.type === 'application') {
gulp.task('copy-html:' + m.name, function () {
return gulp.src(['src/' + m.dir + '/*.html'])
.pipe(gulp.dest('dist/' + m.dir));
});
gulp.task('build:' + m.name, ['webpack:' + m.name, 'copy-html:' + m.name]);
}
});
/* 'build everything' task */
gulp.task('build',
INTERNAL_LIBRARIES.concat(APPLICATIONS).map(function (mod) {
return 'build:'+mod.name
}));
/* test task */
gulp.task('test', ['build'], function () {
return gulp.src(EXTERNAL_LIBRARIES.map(function (lib) {
return lib.dir + '/' + lib.file;
}).concat([
'dist/**/*.js',
'!dist/**/*.min.js',
'',
'test/**/*.js'
])).pipe(karma({ configFile: 'karma.conf.js' }));
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
gulp.task('watch', function () {
gulp.watch([
'src/**/*.js',
'src/**/*.scss',
'src/**/*.css',
'src/**/*.html'
], ['lint', 'build']);
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
['major', 'minor', 'patch', 'prerelease'].forEach(function (type) {
gulp.task('bump:'+type, function () {
return gulp.src(['package.json', 'bower.json'])
.pipe(bump({ type: type }))
.pipe(gulp.dest('./'));
});
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
gulp.task('default', ['lint', 'build', 'watch']);