forked from grapheco/InteractiveGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
263 lines (221 loc) · 7.32 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var fs = require('fs');
var gulp = require('gulp');
var gutil = require('gulp-util');
var concat = require('gulp-concat');
var cleanCSS = require('gulp-clean-css');
var rename = require("gulp-rename");
var webpack = require('webpack');
var uglify = require('uglify-js');
var rimraf = require('rimraf');
var replace = require('gulp-replace-pro');
var ts = require('gulp-typescript');
var tsp = ts.createProject('tsconfig.json');
const zip = require('gulp-zip');
const sass = require('gulp-sass');
var typedoc = require("gulp-typedoc");
var war = require("gulp-war");
var PRODUCT_NAME = 'interactive-graph';
var VERSIONED_PRODUCT_NAME = PRODUCT_NAME + '-0.1.0';
var ENTRY = __dirname + '/src/exports.js';
var RELEASE_DIR = __dirname + '/dist';
var SOURCE_EXAMPLES_DIR = __dirname + '/src/test/webapp';
var BUILD_PACK_DIR = __dirname + '/build';
var BUILD_OBJ_DIR = BUILD_PACK_DIR + '/obj';
var API_DOC_DIR = RELEASE_DIR + '/api';
var LIB_ZIP_NAME = 'igraph.zip';
var API_DOC_ZIP_NAME = 'api-doc.zip';
var EXAMPLES_ZIP_NAME = 'examples.zip';
var EXAMPLES_WAR_NAME = 'igraph.war'
var RELEASE_LIB_DIR = RELEASE_DIR + '/igraph';
var RELEASE_EXAMPLES_DIR = RELEASE_DIR + '/examples';
var LIB_DIR_IN_RELEASE_EXAMPLES = RELEASE_EXAMPLES_DIR + '/lib/' + VERSIONED_PRODUCT_NAME;
var OUTPUT_JS = PRODUCT_NAME + '.js';
var OUTPUT_MAP = PRODUCT_NAME + '.map';
var OUTPUT_MIN_JS = PRODUCT_NAME + '.min.js';
var OUTPUT_CSS = PRODUCT_NAME + '.css';
var OUTPUT_MIN_CSS = PRODUCT_NAME + '.min.css';
var TYPESCRIPT_SOURCE = './src/main/typescript/**/*.ts';
var CSS_SOURCE = './src/main/resources/**/*.css';
var SASS_SOURCE = './src/main/resources/styles/*.scss';
var SASS_OUTPUT = './src/main/resources/styles/';
var WEBPACK_LIBRARY = 'igraph';
var RELEASE_REPLACE_1 = '../../../build/' + PRODUCT_NAME;
var RELEASE_REPLACE_2 = './lib/' + VERSIONED_PRODUCT_NAME + '/' + PRODUCT_NAME + '.min';
var webpackModule = {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
cacheDirectory: true, // use cache to improve speed
babelrc: true // use the .baberc file
}
}],
// exclude requires of moment.js language files
wrappedContextRegExp: /$^/
};
var webpackConfig = {
entry: ENTRY,
output: {
library: WEBPACK_LIBRARY,
libraryTarget: 'umd',
path: BUILD_PACK_DIR,
filename: OUTPUT_JS,
sourcePrefix: ' '
},
module: webpackModule,
plugins: [],
cache: true,
};
var uglifyConfig = {
outSourceMap: OUTPUT_MAP,
output: {
comments: /@license/
}
};
// create a single instance of the compiler to allow caching
var compiler = webpack(webpackConfig);
function handleCompilerCallback(err, stats) {
if (err) {
gutil.log(err.toString());
}
if (stats && stats.compilation && stats.compilation.errors) {
// output soft errors
stats.compilation.errors.forEach(function (err) {
gutil.log(err.toString());
});
if (err || stats.compilation.errors.length > 0) {
gutil.beep(); // TODO: this does not work on my system
}
}
}
gulp.task('clean-build', function (cb) {
rimraf(BUILD_PACK_DIR + '/*', cb);
});
//.ts --> .js
gulp.task('build-ts', ['clean-build'], function (cb) {
return gulp.src(TYPESCRIPT_SOURCE)
.pipe(tsp({
error: (error, typescript) => {
console.info("failed to compile:");
console.error(error)
},
finish: (results) => {
console.info("successed to compile:");
console.info(results);
}
}))
.pipe(gulp.dest(BUILD_OBJ_DIR));
cb();
});
//pack js files into a single file
gulp.task('bundle-js', ['build-ts'], function (cb) {
compiler.run(function (err, stats) {
handleCompilerCallback(err, stats);
cb();
});
});
// sass
gulp.task('bundle-scss', function () {
gulp.src(SASS_SOURCE)
.pipe(sass({
outputStyle: 'expanded'
}))
.pipe(gulp.dest(SASS_OUTPUT));
});
// bundle and minify css
gulp.task('bundle-css', ['clean-build'], function () {
return gulp.src(CSS_SOURCE)
.pipe(concat(OUTPUT_CSS))
.pipe(gulp.dest(BUILD_PACK_DIR));
});
gulp.task('clean-dist', function (cb) {
rimraf(RELEASE_DIR, cb);
});
gulp.task('bundle', ['bundle-js','bundle-scss', 'bundle-css']);
/////////gulp build///////////
gulp.task('build', ['clean-build', 'build-ts', 'bundle']);
gulp.task('minify-css', ['bundle'], function () {
return gulp.src(BUILD_PACK_DIR + "/" + OUTPUT_CSS)
.pipe(cleanCSS())
.pipe(rename(OUTPUT_MIN_CSS))
.pipe(gulp.dest(BUILD_PACK_DIR));
});
gulp.task('minify-js', ['bundle'], function (cb) {
var result = uglify.minify([BUILD_PACK_DIR + '/' + OUTPUT_JS], uglifyConfig);
fs.writeFileSync(BUILD_PACK_DIR + '/' + OUTPUT_MIN_JS, result.code + '\n');
fs.writeFileSync(BUILD_PACK_DIR + '/' + OUTPUT_MAP, result.map.replace(/"\.\/dist\//g, '"'));
cb();
});
gulp.task('copy-build-to-lib', ['minify-js', 'minify-css', 'clean-dist'], function (cb) {
var stream = gulp.src(BUILD_PACK_DIR + '/*.min.*')
.pipe(gulp.dest(RELEASE_LIB_DIR));
return stream;
});
gulp.task('copy-examples-to-release', ['clean-dist'], function (cb) {
var stream = gulp.src(SOURCE_EXAMPLES_DIR + '/**/*')
.pipe(gulp.dest(RELEASE_EXAMPLES_DIR));
return stream;
});
gulp.task('copy-lib-to-release', ['copy-build-to-lib', 'copy-examples-to-release'], function (cb) {
var stream = gulp.src(RELEASE_LIB_DIR + '/**/*')
.pipe(gulp.dest(LIB_DIR_IN_RELEASE_EXAMPLES));
return stream;
});
gulp.task('ts-doc', ['clean-dist'], function (cb) {
return gulp
.src([TYPESCRIPT_SOURCE])
.pipe(typedoc({
// TypeScript options (see typescript docs)
module: "umd",
target: "es6",
includeDeclarations: false,
excludeExternals: true,
// Output options (see typedoc docs)
out: API_DOC_DIR,
//json: "./docs/api/file.json",
//mode: "modules",
mode: "file",
// TypeDoc options (see typedoc docs)
name: "InteractiveGraph API",
ignoreCompilerErrors: true,
version: true,
readme: 'none',
hideGenerator: true,
excludePrivate: true
}));
});
gulp.task('update-release-html', ['copy-examples-to-release'], function () {
gulp.src(RELEASE_EXAMPLES_DIR + '/**/*.html')
.pipe(replace(RELEASE_REPLACE_1, RELEASE_REPLACE_2))
.pipe(gulp.dest(RELEASE_EXAMPLES_DIR));
});
gulp.task('zip-lib', ['copy-lib-to-release'], function () {
return gulp.src(RELEASE_LIB_DIR + '/**/*.*')
.pipe(zip(LIB_ZIP_NAME))
.pipe(gulp.dest(RELEASE_DIR));
});
gulp.task('zip-api-doc', ['ts-doc'], function () {
return gulp.src(API_DOC_DIR + '/**/*.*')
.pipe(zip(API_DOC_ZIP_NAME))
.pipe(gulp.dest(RELEASE_DIR));
});
gulp.task('zip-examples', ['update-release-html', 'copy-lib-to-release'], function () {
return gulp.src(RELEASE_EXAMPLES_DIR + '/**/*.*')
.pipe(zip(EXAMPLES_ZIP_NAME))
.pipe(gulp.dest(RELEASE_DIR));
});
gulp.task('war-examples', ['update-release-html', 'copy-lib-to-release'], function () {
return gulp.src(RELEASE_EXAMPLES_DIR + '/**/*.*')
.pipe(war({}))
.pipe(zip(EXAMPLES_WAR_NAME))
.pipe(gulp.dest(RELEASE_DIR));
});
/////////gulp release///////////
gulp.task('release', ['build', 'copy-build-to-lib', 'copy-examples-to-release', 'copy-lib-to-release',
'ts-doc', 'update-release-html', 'zip-examples', 'war-examples', 'zip-lib', 'zip-api-doc'
]);
/////////gulp watch///////////
gulp.task('watch', function(){
gulp.watch(SASS_SOURCE, ['bundle-scss']);
});