-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.babel.js
226 lines (204 loc) · 6.61 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
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
const gulp = require("gulp");
const rollup = require("rollup");
const babel = require("rollup-plugin-babel");
const multiEntry = require("rollup-plugin-multi-entry");
const nodeResolve = require("rollup-plugin-node-resolve");
const commonjs = require("rollup-plugin-commonjs");
const cleanup = require("rollup-plugin-cleanup");
const uglify = require("rollup-plugin-uglify").uglify;
const utils = require("./tasks/utils");
const template = require("lodash.template");
const path = require("path");
const del = require("del");
const browserSync = require("browser-sync");
//////////////////////////////////////////////////////////////
// CONSTANTS
//////////////////////////////////////////////////////////////
const base = utils.base;
const pkg = utils.pkg;
const version = utils.env.isProduction ? pkg.version : "NEXT";
// the order of the libs here is also the order that they will be bundled in combined
const libs = ["core", "tween", "easel", "sound", "preload"];
// the lib that is using this process, read as @createjs/(lib)
const lib = pkg.name.split("/")[1];
// quickrefs
const paths = {
LICENSE: "./assets/LICENSE",
BANNER: "./assets/BANNER",
dist: path.join(base, "dist"),
main: path.join(base, "src/main.js"),
plugins: path.join(base, "src/plugins"),
watch: {
js: "src/**/*.js",
plugins: "src/plugins/**/*.js",
examples: "examples/**/*",
extras: "extras/**/*",
tutorials: "tutorials/**/*",
spikes: "spikes/**/*"
}
};
//////////////////////////////////////////////////////////////
// BUNDLING
//////////////////////////////////////////////////////////////
// stores bundle caches for rebundling with rollup
const buildCaches = {};
// instantiate rollup plugins only once
const plugins = {
babel: babel({
babelrc: false,
configFile: "./babel.config.js"
}),
multiEntry: multiEntry(),
nodeResolve: nodeResolve(),
commonjs: commonjs(),
uglify: uglify({
output: {
// preserves the @license banner
comments: (node, comment) => comment.line === 1
},
compress: {
global_defs: {
// drop debug statements
DEBUG: false
}
}
}),
cleanup: cleanup({
comments: "srcmaps"
})
};
// default the build formats
const formats = (utils.env.options.format || "cjs,iife").split(",");
// returns an async function for bundling
const bundle = (format) => async () => {
const minify = utils.env.isProduction;
const filename = utils.generateBuildFilename(lib, format, minify);
const bundleOptions = {
// plugins are added below as-needed
plugins: [],
// rollup is faster if we pass in the previous bundle on a re-bundle
cache: buildCaches[filename],
};
const writeOptions = {
format,
file: path.join(paths.dist, filename),
name: "createjs",
exports: "named",
extend: true,
// min files are prepended with LICENSE, non-min with BANNER
banner: template(await utils.readFile(paths[minify ? "LICENSE" : "BANNER"]))({ name: utils.prettyName(lib) }),
// only dev builds get sourcemaps
sourcemap: !minify
};
const versionExports = {};
if (utils.env.isCombined) {
bundleOptions.plugins.push(plugins.multiEntry);
// combined bundle imports all dependencies
bundleOptions.external = () => false;
bundleOptions.input = libs.map(lib => {
const dir = `../${utils.prettyName(lib).toLowerCase()}`;
versionExports[lib] = require(path.join(dir, "package.json")).version;
// TODO: if version is "NEXT", append the git commit hash `NEXT@hash`
return path.join(dir, "src/main.js");
});
} else {
writeOptions.globals = {};
// don't globalize core lib
libs.slice(1).forEach(lib => writeOptions.globals[`@createjs/${lib}js`] = "this.createjs");
// cross-library dependencies must remain externalized for individual bundles
bundleOptions.external = id => /@createjs\/(?!core)/.test(id);
bundleOptions.input = paths.main;
versionExports[lib] = version;
}
writeOptions.outro = utils.parseVersionExport(format, versionExports);
bundleOptions.plugins.push(
plugins.nodeResolve,
plugins.commonjs,
plugins.babel
);
// only minify iife bundles
if (minify && format === "iife") {
bundleOptions.plugins.push(plugins.uglify);
} else {
bundleOptions.plugins.push(plugins.cleanup);
}
return rollup.rollup(bundleOptions).then(b => {
buildCaches[filename] = b.cache;
return b.write(writeOptions);
});
};
gulp.task("bundle:cjs", bundle("cjs"));
gulp.task("bundle:iife", bundle("iife"));
gulp.task("build", gulp.parallel.apply(gulp, formats.map(format => `bundle:${format}`)));
const bundlePlugins = (format) => async done => {
// read plugin directory, bailing if it's not found
let plugins = await utils.readDir(paths.plugins);
if (!plugins) { done(); }
// filter plugins if files flag is present
const files = utils.env.options.files;
if (files) { plugins = plugins.filter(dir => files.includes(path.basename(dir, ".js"))); }
const banner = template(await utils.readFile(paths.BANNER))({ name: utils.prettyName(lib) });
return Promise.all(
plugins.map(plugin => {
const bundleOptions = {
input: path.join(paths.plugins, plugin),
plugins: [
plugins.babel,
plugins.cleanup
],
};
const writeOptions = {
format,
file: path.join(paths.dist, "plugins", plugin),
name: "createjs",
exports: "named",
extend: true,
sourcemap: true,
banner
};
return rollup.rollup(bundleOptions).then(b => b.write(writeOptions));
})
);
};
gulp.task("plugins:cjs", bundlePlugins("cjs"));
gulp.task("plugins:iife", bundlePlugins("iife"));
gulp.task("plugins", gulp.parallel.apply(gulp, formats.map(format => `plugins:${format}`)));
//////////////////////////////////////////////////////////////
// DEV
//////////////////////////////////////////////////////////////
// browser-sync instance for dev
const browser = browserSync.create();
// serve the lib root for easy examples/extras access
gulp.task("serve", () => {
browser.init({
host: "localhost",
port: 3000,
server: {
baseDir: base,
directory: true
},
ghostMode: false,
logLevel: "info",
logPrefix: utils.prettyName(lib)
});
});
gulp.task("reload", cb => {
browser.reload();
cb();
});
gulp.task("watch", () => {
const w = paths.watch;
utils.watch([w.js, `!${w.plugins}`], gulp.series("build", "reload"));
utils.watch(w.plugins, gulp.series("plugins", "reload"));
utils.watch([w.examples, w.extras, w.tutorials, w.spikes], gulp.series("reload"));
});
gulp.task("dev", gulp.series("build", gulp.parallel("serve", "watch")));
//////////////////////////////////////////////////////////////
// LINT
//////////////////////////////////////////////////////////////
gulp.task("lint", () => {
// fail after error to break the travis build
return gulp.src(paths.src)
.pipe(eslint())
.pipe(eslint.format("codeframe"));
});