Skip to content

Commit 3bb330a

Browse files
authored
Add esbuild to pack bundler (#810)
1 parent 7c72fbc commit 3bb330a

File tree

7 files changed

+98
-19
lines changed

7 files changed

+98
-19
lines changed

cli/build.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
import type {Arguments} from 'yargs';
22
import {ConsoleLogger} from '../helpers/logging';
33
import type {Logger} from '../api_types';
4+
import * as esbuild from 'esbuild';
45
import fs from 'fs';
56
import os from 'os';
67
import path from 'path';
78
import webpack from 'webpack';
89

910
interface BuildArgs {
1011
manifestFile: string;
12+
compiler?: Compiler;
1113
}
1214

13-
export async function handleBuild({manifestFile}: Arguments<BuildArgs>) {
15+
enum Compiler {
16+
esbuild = 'esbuild',
17+
webpack = 'webpack',
18+
}
19+
20+
export async function handleBuild({manifestFile, compiler}: Arguments<BuildArgs>) {
21+
// TODO(alan): surface more helpful error messages when import manifestFile fails.
1422
const {manifest} = await import(manifestFile);
1523
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'coda-packs-'));
1624

1725
const bundleFilename = path.join(tempDir, `bundle-${manifest.id}-${manifest.version}.js`);
18-
await compilePackBundleWebpack(bundleFilename, manifestFile, new ConsoleLogger());
26+
const logger = new ConsoleLogger();
27+
28+
switch (compiler) {
29+
case Compiler.webpack:
30+
await compilePackBundleWebpack(bundleFilename, manifestFile, logger);
31+
return;
32+
case Compiler.esbuild:
33+
default:
34+
await compilePackBundleESBuild(bundleFilename, manifestFile);
35+
return;
36+
}
37+
}
38+
39+
export async function compilePackBundleESBuild(bundleFilename: string, entrypoint: string) {
40+
const options: esbuild.BuildOptions = {
41+
bundle: true,
42+
entryPoints: [entrypoint],
43+
outfile: bundleFilename,
44+
platform: 'node',
45+
external: ['canvas'],
46+
minify: true,
47+
};
48+
await esbuild.build(options);
1949
}
2050

2151
async function compilePackBundleWebpack(bundleFilename: string, entrypoint: string, logger: Logger): Promise<any> {

cli/coda.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,17 @@ if (require.main === module) {
206206
describe: 'Register API token to publish a pack',
207207
handler: handleRegister,
208208
})
209-
.command({
210-
command: 'build [manifestFile]',
211-
describe: 'Generate a webpack bundle for your pack',
212-
handler: handleBuild,
213-
})
209+
.command(
210+
'build [manifestFile]',
211+
'Generate a webpack bundle for your pack',
212+
y => {
213+
return y.option('compiler', {
214+
description: '`esbuild` or `webpack`',
215+
required: false,
216+
});
217+
},
218+
handleBuild,
219+
)
214220
.demandCommand()
215221
.strict()
216222
.help().argv;

dist/cli/build.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { Arguments } from 'yargs';
22
interface BuildArgs {
33
manifestFile: string;
4+
compiler?: Compiler;
45
}
5-
export declare function handleBuild({ manifestFile }: Arguments<BuildArgs>): Promise<void>;
6+
declare enum Compiler {
7+
esbuild = "esbuild",
8+
webpack = "webpack"
9+
}
10+
export declare function handleBuild({ manifestFile, compiler }: Arguments<BuildArgs>): Promise<void>;
11+
export declare function compilePackBundleESBuild(bundleFilename: string, entrypoint: string): Promise<void>;
612
export {};

dist/cli/build.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3131
return (mod && mod.__esModule) ? mod : { "default": mod };
3232
};
3333
Object.defineProperty(exports, "__esModule", { value: true });
34-
exports.handleBuild = void 0;
34+
exports.compilePackBundleESBuild = exports.handleBuild = void 0;
3535
const logging_1 = require("../helpers/logging");
36+
const esbuild = __importStar(require("esbuild"));
3637
const fs_1 = __importDefault(require("fs"));
3738
const os_1 = __importDefault(require("os"));
3839
const path_1 = __importDefault(require("path"));
3940
const webpack_1 = __importDefault(require("webpack"));
40-
function handleBuild({ manifestFile }) {
41+
var Compiler;
42+
(function (Compiler) {
43+
Compiler["esbuild"] = "esbuild";
44+
Compiler["webpack"] = "webpack";
45+
})(Compiler || (Compiler = {}));
46+
function handleBuild({ manifestFile, compiler }) {
4147
return __awaiter(this, void 0, void 0, function* () {
48+
// TODO(alan): surface more helpful error messages when import manifestFile fails.
4249
const { manifest } = yield Promise.resolve().then(() => __importStar(require(manifestFile)));
4350
const tempDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'coda-packs-'));
4451
const bundleFilename = path_1.default.join(tempDir, `bundle-${manifest.id}-${manifest.version}.js`);
45-
yield compilePackBundleWebpack(bundleFilename, manifestFile, new logging_1.ConsoleLogger());
52+
const logger = new logging_1.ConsoleLogger();
53+
switch (compiler) {
54+
case Compiler.webpack:
55+
yield compilePackBundleWebpack(bundleFilename, manifestFile, logger);
56+
return;
57+
case Compiler.esbuild:
58+
default:
59+
yield compilePackBundleESBuild(bundleFilename, manifestFile);
60+
return;
61+
}
4662
});
4763
}
4864
exports.handleBuild = handleBuild;
65+
function compilePackBundleESBuild(bundleFilename, entrypoint) {
66+
return __awaiter(this, void 0, void 0, function* () {
67+
const options = {
68+
bundle: true,
69+
entryPoints: [entrypoint],
70+
outfile: bundleFilename,
71+
platform: 'node',
72+
external: ['canvas'],
73+
minify: true,
74+
};
75+
yield esbuild.build(options);
76+
});
77+
}
78+
exports.compilePackBundleESBuild = compilePackBundleESBuild;
4979
function compilePackBundleWebpack(bundleFilename, entrypoint, logger) {
5080
return __awaiter(this, void 0, void 0, function* () {
5181
logger.info(`... Bundle -> ${bundleFilename}`);

dist/cli/coda.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,12 @@ if (require.main === module) {
211211
describe: 'Register API token to publish a pack',
212212
handler: register_1.handleRegister,
213213
})
214-
.command({
215-
command: 'build [manifestFile]',
216-
describe: 'Generate a webpack bundle for your pack',
217-
handler: build_1.handleBuild,
218-
})
214+
.command('build [manifestFile]', 'Generate a webpack bundle for your pack', y => {
215+
return y.option('compiler', {
216+
description: '`esbuild` or `webpack`',
217+
required: false,
218+
});
219+
}, build_1.handleBuild)
219220
.demandCommand()
220221
.strict()
221222
.help().argv;

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@types/sinon": "^9.0.10",
2020
"client-oauth2": "^4.3.3",
2121
"clone": "^2.1.2",
22+
"esbuild": "^0.8.49",
2223
"express": "4.17.1",
2324
"open": "^7.4.1",
2425
"pascalcase": "^1.0.0",
@@ -37,9 +38,6 @@
3738
"devDependencies": {
3839
"@types/chai": "^4.2.15",
3940
"@types/clone": "^2.1.0",
40-
"@typescript-eslint/eslint-plugin": "4.15.1",
41-
"@typescript-eslint/experimental-utils": "4.15.1",
42-
"@typescript-eslint/parser": "4.15.1",
4341
"@types/express": "^4.17.11",
4442
"@types/mocha": "^8.2.0",
4543
"@types/mock-fs": "^4.13.0",
@@ -54,6 +52,9 @@
5452
"@types/webpack": "^4.41.26",
5553
"@types/xml2js": "^0.4.8",
5654
"@types/yargs": "^16.0.0",
55+
"@typescript-eslint/eslint-plugin": "4.15.1",
56+
"@typescript-eslint/experimental-utils": "4.15.1",
57+
"@typescript-eslint/parser": "4.15.1",
5758
"chai": "^4.3.0",
5859
"eslint": "7.20.0",
5960
"eslint-plugin-ban": "^1.5.2",

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,11 @@ es-module-lexer@^0.3.26:
11251125
resolved "https://registry.yarnpkg.com/es-module-lexer/-/es-module-lexer-0.3.26.tgz#7b507044e97d5b03b01d4392c74ffeb9c177a83b"
11261126
integrity sha512-Va0Q/xqtrss45hWzP8CZJwzGSZJjDM5/MJRE3IXXnUCcVLElR9BRaE9F62BopysASyc4nM3uwhSW7FFB9nlWAA==
11271127

1128+
esbuild@^0.8.49:
1129+
version "0.8.49"
1130+
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.49.tgz#3d33f71b3966611f822cf4c838115f3fbd16def2"
1131+
integrity sha512-itiFVYv5UZz4NooO7/Y0bRGVDGz/M/cxKbl6zyNI5pnKaz1mZjvZXAFhhDVz6rGCmcdTKj5oag6rh8DaaSSmfQ==
1132+
11281133
escalade@^3.1.1:
11291134
version "3.1.1"
11301135
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"

0 commit comments

Comments
 (0)