-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Gulpfile.js
135 lines (119 loc) · 3.71 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
"use strict";
const plumber = require("gulp-plumber");
const through = require("through2");
const chalk = require("chalk");
const newer = require("gulp-newer");
const babel = require("gulp-babel");
const gulpWatch = require("gulp-watch");
const fancyLog = require("fancy-log");
const gulp = require("gulp");
const path = require("path");
const rollup = require("rollup").rollup;
const rollupBabel = require("@rollup/plugin-babel").babel;
const rollupNodeResolve = require("@rollup/plugin-node-resolve").nodeResolve;
const rollupJson = require("@rollup/plugin-json");
const esmBundles = [
{ name: "babel-helper-define-polyfill-provider", target: "node" },
{ name: "babel-helper-define-polyfill-provider", target: "browser" },
{ name: "babel-plugin-polyfill-corejs2" },
{ name: "babel-plugin-polyfill-corejs3" },
{ name: "babel-plugin-polyfill-es-shims" },
{ name: "babel-plugin-polyfill-regenerator" },
];
function swapSrcWithLib(srcPath) {
const parts = srcPath.split(path.sep);
parts[1] = "lib";
return parts.join(path.sep);
}
function compilationLogger() {
return through.obj(function (file, enc, callback) {
fancyLog(`Compiling '${chalk.cyan(file.relative)}'...`);
callback(null, file);
});
}
function errorsLogger() {
return plumber({
errorHandler(err) {
fancyLog(err.stack);
},
});
}
function rename(fn) {
return through.obj(function (file, enc, callback) {
file.path = fn(file);
callback(null, file);
});
}
function build() {
const base = path.join(__dirname, "packages");
return gulp
.src("./packages/*/src/**/*.{js,ts}", { base: base })
.pipe(errorsLogger())
.pipe(newer({ dest: base, map: swapSrcWithLib }))
.pipe(compilationLogger())
.pipe(babel())
.pipe(
// Passing 'file.relative' because newer() above uses a relative
// path and this keeps it consistent.
rename(file => path.resolve(file.base, swapSrcWithLib(file.relative)))
)
.pipe(gulp.dest(base));
}
async function buildRollup() {
const base = path.join(__dirname, "packages");
return Promise.all(
esmBundles.map(async ({ name, target }) => {
const dir = `${base}/${name}`;
const pkg = require(`${dir}/package.json`);
const external = specifier => {
if (specifier.includes("/core-js-compat/")) return true;
if (specifier.includes("/data/polyfills")) return true;
if (specifier.startsWith("/")) return false;
const [name] = /^(?:@[^/]+\/[^/]+|[^/]+)/.exec(specifier);
if (name === "@babel/core") return true;
return Object.hasOwnProperty.call(pkg.dependencies || {}, name);
};
const bundle = await rollup({
input: `${dir}/src/index.ts`,
external,
plugins: [
rollupJson(),
rollupBabel({
envName: "esm",
babelrc: false,
babelHelpers: "bundled",
extends: "./babel.config.json",
extensions: [".ts", ".js", ".mjs", ".cjs"],
}),
rollupNodeResolve({
extensions: [".ts", ".js", ".mjs", ".cjs", ".json"],
browser: target === "browser",
preferBuiltins: true,
}),
],
});
const outputFile = target
? `${dir}/esm/index.${target}.mjs`
: `${dir}/esm/index.mjs`;
await bundle.write({
file: outputFile,
format: "es",
sourcemap: true,
exports: "named",
});
})
);
}
gulp.task("build", () => build());
gulp.task("bundle", () => buildRollup());
gulp.task("default", gulp.series("build"));
gulp.task(
"watch",
gulp.series("build", function watch() {
gulpWatch(
"./packages/*/src/**/*.{js,ts}",
{ debounceDelay: 200 },
gulp.task("build")
);
})
);