Skip to content

Instantly share code, notes, and snippets.

@revelt
Created April 3, 2021 11:54
Show Gist options
  • Save revelt/2be5920c7b483be605b91a5b9e1f3cad to your computer and use it in GitHub Desktop.
Save revelt/2be5920c7b483be605b91a5b9e1f3cad to your computer and use it in GitHub Desktop.
working esbuild runner script, to DRY all esbuild calls within lerna monorepo, program sources are in typescript
const path = require("path");
const camelCase = require("lodash.camelcase");
const mode = process.env.MODE;
const name = path.basename(path.resolve("./"));
const pkg = require(path.join(path.resolve("./"), `package.json`));
// pure here means they can be removed if unused (if minifying is on, of course)
const pure =
mode === "dev"
? []
: ["console.log", "console.time", "console.timeEnd", "console.timeLog"];
// bundle, but set dependencies as external
const external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
];
const banner = {
js: `/**
* @name ${name}
* @fileoverview ${pkg.description}
* @version ${pkg.version}
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/${name}/}
*/
`,
};
// CJS
if (pkg.main) {
require("esbuild").buildSync({
entryPoints: [path.join(path.resolve("./"), "src/main.ts")],
format: "cjs",
bundle: true,
// minify: !mode,
minify: true,
sourcemap: false,
target: ["node10.4"],
outfile: path.join(path.resolve("./"), `dist/${name}.cjs.js`),
pure,
banner,
external,
});
}
// ESM
if (pkg.module) {
require("esbuild").buildSync({
entryPoints: [path.join(path.resolve("./"), "src/main.ts")],
format: "esm",
bundle: true,
minify: !mode,
sourcemap: false,
target: ["esnext"],
outfile: path.join(path.resolve("./"), `dist/${name}.esm.js`),
pure,
banner,
external,
});
}
// dev IIFE
if (pkg.browser) {
require("esbuild").buildSync({
entryPoints: [path.join(path.resolve("./"), "src/main.ts")],
format: "iife",
globalName: camelCase(name),
bundle: true,
minify: !mode,
sourcemap: false,
target: ["chrome58"],
outfile: path.join(path.resolve("./"), `dist/${name}.dev.umd.js`),
pure,
banner,
// no "external" - bundle everything
});
}
const path = require("path");
const camelCase = require("lodash.camelcase");
const mode = process.env.MODE;
const name = path.basename(path.resolve("./"));
const pkg = require(path.join(path.resolve("./"), `package.json`));
// pure here means they can be removed if unused (if minifying is on, of course)
const pure =
mode === "dev"
? []
: ["console.log", "console.time", "console.timeEnd", "console.timeLog"];
// bundle, but set dependencies as external
const external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
];
const banner = {
js: `/**
* @name ${name}
* @fileoverview ${pkg.description}
* @version ${pkg.version}
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/${name}/}
*/
`,
};
// CJS
if (pkg.main) {
require("esbuild").buildSync({
entryPoints: [path.join(path.resolve("./"), "src/main.ts")],
format: "cjs",
bundle: true,
// minify: !mode,
minify: true,
sourcemap: false,
target: ["node10.4"],
outfile: path.join(path.resolve("./"), `dist/${name}.cjs.js`),
pure,
banner,
external,
});
}
// ESM
if (pkg.module) {
require("esbuild").buildSync({
entryPoints: [path.join(path.resolve("./"), "src/main.ts")],
format: "esm",
bundle: true,
minify: !mode,
sourcemap: false,
target: ["esnext"],
outfile: path.join(path.resolve("./"), `dist/${name}.esm.js`),
pure,
banner,
external,
});
}
// dev IIFE
if (pkg.browser) {
require("esbuild").buildSync({
entryPoints: [path.join(path.resolve("./"), "src/main.ts")],
format: "iife",
globalName: camelCase(name),
bundle: true,
minify: !mode,
sourcemap: false,
target: ["chrome58"],
outfile: path.join(path.resolve("./"), `dist/${name}.dev.umd.js`),
pure,
banner,
// no "external" - bundle everything
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment