-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgulpfile.js
121 lines (105 loc) · 3.5 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
var path = require("path");
var browserify = require("browserify");
var gulp = require("gulp");
var buffer = require("gulp-buffer");
var sourcemaps = require("gulp-sourcemaps");
var tap = require("gulp-tap");
var uglify = require("gulp-uglify");
var namespace = "MbedCloudSDK";
var foundationNamespace = "Mbed.Cloud";
// Source
var srcDir = "src";
// Node
var nodeDir = "lib";
var bundleFiles = nodeDir + "/legacy/**/index.js";
var foundationFiles = nodeDir + "/foundation/**/index.js";
var sdkDirectory = nodeDir + "/sdk.js";
// Browser bundles
var bundleDir = "bundles";
// Browserify helper function
function bundle(srcFiles, destDir, optionsFn) {
return gulp.src(srcFiles, {
read: false
})
.pipe(tap(function(file) {
var options = {};
if (optionsFn) options = optionsFn(file);
// strip legacy from filename to preserve old bundles
var fileName = options.fileName || path.basename(file.path);
if (options.standalone)
console.log(`Creating ${options.standalone} in ${destDir}/${fileName}`);
else
console.log(`Creating ${destDir}/${fileName}`);
file.contents = browserify(file.path, options)
.ignore("buffer")
.ignore("dotenv")
.bundle()
.on("error", function (err) {
console.log(err);
});
file.path = path.join(file.base, fileName);
}))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
.pipe(uglify({
preserveComments: function(node, comment) {
return comment.value.includes("Copyright Arm");
}
}))
.pipe(sourcemaps.write(".", {
sourceRoot: path.relative(destDir, nodeDir)
}))
.pipe(gulp.dest(destDir));
}
function camelToKebab(camel) {
return camel.replace(/([A-Z]+)/g, function (match) {
return `-${match.toLowerCase()}`;
});
}
function camelToPascal(camel) {
return camel.charAt(0).toUpperCase() + camel.slice(1);
};
// Build CommonJS modules into browser bundles
gulp.task("bundleLegacy", function() {
return bundle(bundleFiles, bundleDir, function(file) {
var name = path.dirname(file.relative);
if (name === ".") {
return {
fileName: "index.min.js",
standalone: namespace
};
}
return {
fileName: `${camelToKebab(name)}.min${path.extname(file.relative)}`,
standalone: `${namespace}.${name.charAt(0).toUpperCase()}${name.slice(1)}Api`
};
});
});
gulp.task("bundleFoundation", function () {
return bundle(foundationFiles, bundleDir, function (file) {
var groupPath = path.dirname(file.relative);
var name = groupPath.substring(groupPath.indexOf("/") + 1);
var pascalName = camelToPascal(name);
if (name === ".") {
return {
fileName: "foundation/index.min.js",
standalone: `${foundationNamespace}.Foundation`
};
}
return {
fileName: `foundation/${camelToKebab(name)}.min${path.extname(file.relative)}`,
standalone: `${foundationNamespace}.Foundation.${pascalName}`
};
});
});
gulp.task("bundleSdk", function () {
return bundle(sdkDirectory, bundleDir, function (file) {
return {
fileName: "sdk.min.js",
standalone: foundationNamespace
};
});
});
gulp.task("default", ["bundleLegacy", "bundleFoundation", "bundleSdk"]);