This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
153 lines (135 loc) · 4.75 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
// @ts-check
const sourcemaps = require("gulp-sourcemaps")
const ts = require("gulp-typescript")
const del = require("del")
const gulp = require("gulp")
const debug = require("gulp-debug")
const rollup = require("gulp-better-rollup")
const newer = require("gulp-newer")
const changed = require("gulp-changed")
const { terser } = require("rollup-plugin-terser")
const json = require("rollup-plugin-json")
const resolve = require("rollup-plugin-node-resolve")
const babel = require("rollup-plugin-babel")
function compress(scripts, out){
const executor = () => gulp.src(scripts)
.pipe(debug({ title: "compressing" }))
.pipe(rollup({
plugins: [
json(),
resolve({
preferBuiltins: true
}),
babel({
exclude: "node_modules/**",
presets: ["@babel/preset-env"],
}),
terser()
],
}, {
format: "cjs",
exports: "named",
browser: false
}))
.pipe(gulp.dest(out))
executor.displayName = `compressing sources in ${scripts} to ${out}`
return executor
}
function build(config, override, dist) {
const executor = (done) => buildTypescriptSources(config, override).pipe(gulp.dest(dist))
executor.displayName = `build typescript sources from ${config}`
return executor
}
function project({
paths: {
tsconfig: tsconfig = "./tsconfig.json",
src: src = "./src",
dist: dist = "./dist",
out: out = "./out",
scripts: scripts = ["dist/**/*.js"],
/**
* Assumed to be copied from {@link src}.
*/
static: static = [],
/**
* Copied as is to {@link dist} and {@link out}.
*/
vendor: vendor = []
}
}) {
function copy(title, a, base) {
return function (destination) {
function copyTask(done) {
if (a && a.length <= 0) {
done()
} else {
return gulp.src(a, { base: base })
.pipe(changed(destination))
.pipe(debug({ title: `${title} -> ${destination}` }))
.pipe(gulp.dest(destination))
}
}
copyTask.displayName = `copy sources from ${base} matching '${a}' to ${destination}`
return copyTask
}
}
const copyStatic = copy("copying static assets", static, src)
const copyVendor = copy("copying vendor assets", vendor, ".")
function localBuild(override) {
/**
* @type {string[]}
*/
const configs = Array.isArray(tsconfig) ? tsconfig : [tsconfig];
const tasks = configs.map(config => build(config, override, dist))
return (done) => gulp.parallel(...tasks)(done)
}
function cleanFolder(folder){
const cleanTask = () => del(folder)
cleanTask.displayName = "Removing all files in " + folder
return cleanTask
}
const copyStaticSourcesToDestination = gulp.parallel(copyStatic(dist), copyVendor(dist))
const copyStaticSourcesToCompressed = gulp.parallel(copyStatic(out), copyVendor(out))
const clean = gulp.parallel(cleanFolder(dist), cleanFolder(out))
const bundle = gulp.parallel(copyStaticSourcesToCompressed, compress(scripts, out))
function package(done) {
try {
const args = ["./package.json", '--out-path', dist]
const { exec } = require("pkg")
return exec(args)
.catch(done)
.then(done)
} catch (e) {
done(e)
}
}
return {
copy: copyStaticSourcesToDestination,
build: gulp.parallel(copyStaticSourcesToDestination, localBuild({})),
buildFast: gulp.parallel(copyStaticSourcesToDestination, localBuild({ isolatedModules: true })),
clean: clean,
compress: compress(scripts, out),
bundle: bundle,
package: gulp.series(bundle, package)
}
}
/**
* @param {string} config Path to a tsconfig.json.
* @param {any} [settings]
* @returns A stream that can be `gulp.pipe`'d to a destination.
*/
function buildTypescriptSources(config, settings) {
const project = ts.createProject(config, settings)
return project.src()
.pipe(debug({ title: `compiling typescript source (${config})` }))
.pipe(newer({
dest: project.projectDirectory,
ext: ".js"
}))
.pipe(sourcemaps.init())
.pipe(project())
.pipe(sourcemaps.write('.', { sourceRoot: "./", includeContent: false }))
.pipe(debug({ title: `compiled typescript source (${config})` }))
}
exports.build = buildTypescriptSources;
exports.project = project;