-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.babel.js
95 lines (80 loc) · 2.46 KB
/
gulpfile.babel.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
import gulp from "gulp";
import rollup from "rollup-stream";
import babel from "rollup-plugin-babel";
import source from "vinyl-source-stream";
import rename from "gulp-rename";
import uglify from "gulp-uglify";
import sourcemaps from "gulp-sourcemaps";
import buffer from "vinyl-buffer";
import karma from "karma";
import fs from "fs";
const paths = {
entry: "./src/main.js",
dist: "./dist/",
sourceFiles: "./src/**/*.js",
sourcemaps: ".",
testConfig: `${__dirname}/test/karma.conf.js`
};
const buildCaches = {};
const pkg = JSON.parse(fs.readFileSync("./package.json"));
function mapFile (filename) {
return filename.replace(".js", "");
}
function bundle (options, type, minify) {
const filename = `${pkg.name.toLowerCase()}${type.length ? `.${type}` : ""}${minify ? ".min" : ""}.js`;
if (buildCaches[filename] != null) { options.cache = buildCaches[filename]; }
options.entry = paths.entry;
let b = rollup(options)
.on("bundle", bundle => buildCaches[filename] = bundle) // cache bundle for re-bundles triggered by watch
.pipe(source(paths.entry))
.pipe(buffer())
.pipe(rename(filename))
if (minify) {
b = b
.pipe(uglify());
} else {
b = b
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write(paths.sourcemaps, { mapFile }));
}
return b.pipe(gulp.dest(paths.dist));
}
gulp.task("bundle:es6", function (done) {
let options = { format: "es" };
bundle(options, "es6");
done();
});
gulp.task("bundle:cjs", function (done) {
let options = {
format: "cjs",
plugins: [ babel() ]
}
bundle(options, "cjs", false);
bundle(options, "cjs", true);
done();
});
gulp.task("bundle:global", function (done) {
let options = {
format: "iife",
moduleName: pkg.name,
plugins: [ babel() ]
};
bundle(options, "", false);
bundle(options, "", true);
done();
});
gulp.task("watch", function () {
// only rebundle the global module for testing
gulp.watch(paths.sourceFiles, gulp.series("bundle:global"));
});
gulp.task("karma", function (done) {
new karma.Server({ configFile: paths.testConfig }, function () { done(); }).start();
});
var jsdoc = require('gulp-jsdoc3');
gulp.task('docs', function (cb) {
var config = require('./jsdoc.json');
gulp.src(['README.md', './src/**/*.js'], {read: false})
.pipe(jsdoc(config, cb));
});
gulp.task("build", gulp.parallel("bundle:es6", "bundle:cjs", "bundle:global" ));
gulp.task("test", gulp.series("bundle:global", gulp.parallel("watch", "karma")));