forked from avontd2868/PowerBI-visuals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
397 lines (315 loc) · 12.8 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Power BI Visualizations
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the ""Software""), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
"use strict";
var gulp = require("gulp"),
runSequence = require("run-sequence").use(gulp),
path = require("path"),
gutil = require("gulp-util"),
webpack = require("webpack"),
utils = require("./build/utils.js"),
options = require("./build/options"),
karma = require("karma"),
opn = require("opn"),
WebpackDevServer = require("webpack-dev-server"),
uglify = require("gulp-uglify"),
gulpMinifyCSS = require("gulp-minify-css"),
cssjanus = require("gulp-cssjanus"),
merge = require("merge2"),
clone = require("gulp-clone"),
Q = require("q"),
packageBuilder = require("./build/packageBuilder.js"),
rename = require("gulp-rename");
var uglifyOptions = require("./build/webpack/uglifyOptions.js").uglifyOptions;
var nonminOptions = require("./build/webpack/uglifyOptions.js").nonminOptions;
require("./build/symlinkToDirectoryLib.js");
var externalsJs = [
"src/Clients/Externals/ThirdPartyIP/JQuery/2.1.3/jquery.min.js",
"src/Clients/Externals/ThirdPartyIP/jqueryui/1.11.4/jquery-ui.min.js",
"src/Clients/Externals/ThirdPartyIP/D3/d3.min.js",
"src/Clients/Externals/ThirdPartyIP/LoDash/lodash.min.js",
"src/Clients/Externals/ThirdPartyIP/GlobalizeJS/globalize.min.js",
"src/Clients/Externals/ThirdPartyIP/GlobalizeJS/globalize.culture.en-US.js"
];
var sandDanceExternalsJs = [
"src/Clients/Externals/ThirdPartyIP/hammer/hammer-206.js",
"src/Clients/Externals/ThirdPartyIP/mat4x/mat4x.js",
"src/Clients/Externals/ThirdPartyIP/vuePlotCore/vuePlotCore.js"
];
var externalsDts = [
"src/Clients/Typedefs/jquery/jquery.d.ts",
"src/Clients/Typedefs/microsoftMaps/Microsoft.Maps.d.ts",
"src/Clients/Typedefs/quill/quill.d.ts",
"src/Clients/Typedefs/lodash/lodash.d.ts",
"src/Clients/Typedefs/d3/d3.d.ts"
];
function getWebPackConfig(watchMode, path) {
var pathToConf = path || "./webpack.config.js";
var webpackConfig = require(pathToConf);
if (!options.generateMaps) {
webpackConfig.devtool = undefined;
webpackConfig.ts.compilerOptions.sourceMap = false;
}
if (watchMode === true) {
// enable watcher
webpackConfig.watch = true;
if (!options.tslintOnChange) {
webpackConfig.module.preLoaders = [];
}
} else {
if (!options.tslintOnBuild) {
webpackConfig.module.preLoaders = [];
}
}
return webpackConfig;
}
gulp.task("build:copy-externals", function () {
var dest = "./lib";
return Q.all([
utils.copy({
cwd: __dirname,
source: externalsJs,
destination: dest,
join: "powerbi-visuals-externals.min.js"
}),
utils.copy({
cwd: __dirname,
source: externalsDts,
destination: dest,
join: "powerbi-externals.d.ts",
newLine: "\n"
}),
utils.copy({
cwd: __dirname,
source: sandDanceExternalsJs,
destination: dest,
join: "powerbi-customVisuals-externals.min.js"
})
]);
});
gulp.task("build:projects", ["build:copy-externals"], function () {
// build only Visuals projects
return runBuild(true).then(function () {
// run post build task
return Q.nfcall(runSequence, "postBuild");
});
});
gulp.task("build", ["build:copy-externals"], function () {
// build Visuals projects and tests together
return runBuild().then(function () {
// run post build task
return Q.nfcall(runSequence, "postBuild");
});
});
function runBuild(withoutTests) {
var deferred = Q.defer();
// disable building tests during gulp build task.
// TODO: consider using other way to disable build of tests
options.buildWithoutTests = withoutTests !== undefined ? !!withoutTests : options.buildWithoutTests;
webpack(getWebPackConfig(), function (err, stats) {
if (err) throw new gutil.PluginError("webpack", err);
traceBuildStats(stats, false /* isWatch*/);
if (stats.compilation.errors.length) {
deferred.reject("Build task failure [err count: " + stats.compilation.errors.length + "]");
} else {
deferred.resolve();
}
});
return deferred.promise;
}
gulp.task("postBuild", function (cb) {
var uglifyTask = "postBuild:uglify",
nonminTask = "postBuild:nonmin",
cssMinTask = "postBuild:css-min",
createSymlinkTask = "postBuild:create-symlink";
var tasks = [uglifyTask, cssMinTask, createSymlinkTask];
if (options.nonminJs) {
tasks.push(nonminTask);
gulp.task(nonminTask, getMinTask(nonminOptions, ".nonmin.js", options.uglifyJs));
}
gulp.task(uglifyTask, getMinTask(uglifyOptions, ".min.js", options.uglifyJs));
gulp.task(cssMinTask, processCss);
return runSequence(tasks, cb);
function getMinTask(options, ext, enabled) {
return function () {
// TODO: consider using `changed` module
return gulp.src(["./lib/*.js", "!./lib/*.min.js", "!./lib/*.nonmin.js"])
.pipe(enabled ? uglify(options) : gutil.noop())
.pipe(rename({ extname: ext }))
.pipe(gulp.dest("./lib/"));
}
}
});
/**
* Produce rtl css and minify all css files in lib folder;
*/
function processCss() {
var dest = "./lib/";
// pick up all css files except min and rtl
var cssStream = gulp.src(["./lib/*.css", "!./lib/*.rtl.css", "!./lib/*.min.css"])
// workaround to produce pausable stream (new mode)
.pipe(gutil.noop());
// produce rtl files from plain css
var cssjanusResult = cssStream
.pipe(clone())
.pipe(cssjanus({ swapLtrRtlInUrl: true }))
.pipe(rename({ extname: ".rtl.css" }))
.pipe(gulp.dest(dest));
// TODO: consider using `changed` module
// minify and save all css
return merge([cssStream, cssjanusResult])
.pipe(options.minifyCss ? gulpMinifyCSS() : gutil.noop())
.pipe(rename({ extname: ".min.css" }))
.pipe(gulp.dest(dest));
}
gulp.task("watch", ["build:copy-externals"], function (callback) {
webpack(getWebPackConfig(true), webPackWatcherCallback);
});
gulp.task("watch:projects", ["build:copy-externals"], function (callback) {
//disable watching tests during gulp watch task.
options.buildWithoutTests = true;
webpack(getWebPackConfig(true), webPackWatcherCallback);
});
gulp.task("test", function (done) {
runSequence("build", "run:tests", done);
});
gulp.task("run:tests", function (done) {
var spec = options.spec;
var watch = options.watch;
var karmaConfig = {
configFile: path.join(__dirname, "./src/Clients/PowerBIVisualsTests/karma.conf.js")
};
if (options.debug) {
// run in browser
karmaConfig.browsers = ["Chrome"];
// don"t close browser at the end so that you can do F12
karmaConfig.singleRun = false;
}
if (spec) {
karmaConfig.client = {
args: ["--grep", spec],
}
}
if (watch) {
karmaConfig.autoWatch = true;
karmaConfig.singleRun = false;
karmaConfig.autoWatchBatchDelay = 1000; // delay to batch multiple changes into a single run
}
new karma.Server(karmaConfig, function (karmaExitCode) {
console.log("Karma exited with code ", karmaExitCode);
done(karmaExitCode ? new Error("Karma exited with code " + karmaExitCode) : null);
//https://github.com/karma-runner/karma/issues/1035
process.exit(karmaExitCode);
}).start();
});
gulp.task("build:tests", function (callback) {
var webpackConfig = getWebPackConfig(false, "./src/Clients/PowerBIVisualsTests/webpack.config.js");
webpack(webpackConfig, function (err, stats) {
if (err) throw new gutil.PluginError("webpack", err);
traceBuildStats(stats, false /* isWatch*/);
callback(stats.compilation.errors.length ? "Failure in 'build:tests' task." : undefined);
});
});
gulp.task("watch:tests", function (callback) {
webpack(getWebPackConfig(true, "./src/Clients/PowerBIVisualsTests/webpack.config.js"), webPackWatcherCallback);
});
gulp.task("playground", ["build:copy-externals"], function (callback) {
//disable building tests
options.buildWithoutTests = true;
var webpackConfig = getWebPackConfig();
webpackConfig.debug = true;
webpackConfig.stats = false;
webpackConfig.output.publicPath = "/assets/";
webpackConfig.entry.CustomVisuals.unshift("webpack-dev-server/client?http://localhost:3333/");
webpackConfig.entry.Visuals.unshift("webpack-dev-server/client?http://localhost:3333/");
webpackConfig.entry.PowerBIVisualsPlayground.unshift("webpack-dev-server/client?http://localhost:3333/");
// Start a webpack-dev-server
new WebpackDevServer(webpack(webpackConfig, function (err, stats) {
if (err)
throw new gutil.PluginError("webpack", err);
var url = "http://localhost:3333/src/Clients/PowerBIVisualsPlayground/";
// open Playground in default browser
gutil.log(gutil.colors.green("Open Playground URL: "), gutil.colors.white(url));
opn(url);
}), {
inline: true,
publicPath: webpackConfig.output.publicPath,
// hot: true,
stats: false
}).listen("3333", "localhost", function (err) {
if (err) {
throw new gutil.PluginError("webpack-dev-server", err);
}
gutil.log("[webpack-dev-server]", "http://localhost:3333/");
});
});
function webPackWatcherCallback(err, stats) {
if (err) throw new gutil.PluginError("webpack", err);
traceBuildStats(stats, true /* isWatch */);
// Check if some css asset is emitted
var needUpdCss = Object.keys(stats.compilation.assets).some(function (assetkey) {
return stats.compilation.assets[assetkey].emitted === true && path.extname(assetkey) === ".css";
});
// produce css related artifacts - rtl.css, min.css, min.rtl,css
// TODO: get rid of such logic when main projects will use only one visual asset/bundle.
if (needUpdCss) {
processCss();
}
gutil.log(gutil.colors.magenta("waiting for changes ..."));
}
function traceBuildStats(stats, isWatch) {
var traceLine = stats.toString(stats.compilation.options.stats || {});
// trace could be empty if there is no errors/warnings and assets=false
if (traceLine) {
// there is new line sign so as to keep webpack message formatting
gutil.log("[webpack]\n", traceLine);
}
if (isWatch) {
var jsonStats = stats.toJson();
var emitted = Object.keys(stats.compilation.assets).filter(function(asset){
return stats.compilation.assets[asset].emitted;
}).length;
var emittedMsg = emitted ? gutil.colors.white(" Assets emitted: ") + gutil.colors.cyan(emitted) : gutil.colors.white(" No assets emitted");
gutil.log("Build took " + gutil.colors["cyan"]((parseInt(jsonStats.time) / 1000) + "s") + emittedMsg);
}
}
gulp.task("package", function (callback) {
gulp.task("package:all", function (callback) {
packageBuilder.buildAllPackages(callback);
});
gulp.task("package:all:addToDrop", function (callback) {
packageBuilder.copyPackagesToDrop(callback);
});
var packageName = options.visual,
addToDrop = options.addToDrop;
if (packageName) {
var pb = new packageBuilder(packageName);
pb.build(callback);
} else if (addToDrop) {
runSequence("package:all", "package:all:addToDrop", callback);
} else {
runSequence("package:all", callback);
}
});