Skip to content

Commit ca3a017

Browse files
committed
feat: more esbuild options from cli
1 parent 4726630 commit ca3a017

File tree

2 files changed

+61
-40
lines changed

2 files changed

+61
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-aws-lambda",
3-
"version": "6.0.0-beta.2",
3+
"version": "6.0.0-beta.3",
44
"description": "AWS Application Load Balancer and API Gateway - Lambda dev tool. Supports packaging, local invoking with ALB, APG, S3, SNS, SQS, DynamoDB Stream server mocking.",
55
"author": "Inqnuam",
66
"license": "MIT",

src/cli.ts

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import path from "node:path";
66
import { pathToFileURL } from "node:url";
77
import { run, type ILambdaFunction } from "./standalone" with { external: "true" };
88
import { log } from "./lib/utils/colorize";
9+
import { version, name } from "../package.json";
910

1011
function printHelpAndExit() {
1112
log.setDebug(true);
@@ -18,10 +19,19 @@ function printHelpAndExit() {
1819
for (const [optionName, value] of Object.entries(options)) {
1920
let printableName = optionName;
2021

22+
if (value.skip) {
23+
continue;
24+
}
25+
2126
if (value.short) {
2227
printableName += `, -${value.short}`;
2328
}
2429

30+
if (value.skipFullPrint) {
31+
log.CYAN(`\t --${printableName}\n`);
32+
continue;
33+
}
34+
2535
let content = `\t\ttype: ${value.type}`;
2636
if (value.description) {
2737
content += `\n\t\tdescription: ${value.description}`;
@@ -40,6 +50,13 @@ function printHelpAndExit() {
4050
log.GREY(content);
4151
}
4252

53+
log.CYAN(`\t --esbuild-[option]`);
54+
log.GREY(`\n\t\tdescription: where 'options' is one of esbuild build options`);
55+
log.GREY(
56+
`\n\t\texample: aws-lambda --esbuild-external="@aws-sdk/*" --esbuild-external=react --esbuild-format=esm --esbuild-outExtension..js=.mjs --esbuild-outExtension..css=.CSS`
57+
);
58+
59+
console.log(name, `v${version}`);
4360
process.exit(0);
4461
}
4562

@@ -145,6 +162,8 @@ interface ICliOptions {
145162
default?: string | boolean | string[] | boolean[] | undefined;
146163
description?: string;
147164
example?: string;
165+
skipFullPrint?: boolean;
166+
skip?: boolean;
148167
}
149168

150169
const options: Record<string, ICliOptions> = {
@@ -155,7 +174,7 @@ const options: Record<string, ICliOptions> = {
155174
timeout: { type: "string", short: "t", default: "3", description: "Set default timeout." },
156175
definitions: { type: "string", short: "d", description: "Path to .json, .mjs, .cjs file with Lambda function definitions." },
157176
functions: { type: "string", short: "f", multiple: true, description: "Glob pattern to automatically find and define Lambda handlers." },
158-
exclude: { type: "string", short: "x", default: "\.(test|spec)\.", description: "RegExp string to exclude found enteries from --functions." },
177+
exclude: { type: "string", short: "x", default: "\.(test|spec)\.", description: "RegExp string to exclude found enteries from --functions." }, // maybe no default ? ot show it as example
159178
handlerName: { type: "string", default: "handler", description: "Handler function name. To be used with --functions." },
160179
env: {
161180
type: "string",
@@ -165,20 +184,17 @@ const options: Record<string, ICliOptions> = {
165184
description: "Environment variables to be injected into Lambdas. All existing AWS_* are automatically injected.",
166185
example: "-e API_KEY=supersecret -e API_URL=https://website.com",
167186
},
168-
"esbuild-outdir": {
169-
type: "string",
170-
description: "Set esbuild outdir",
171-
},
172-
"esbuild-format": {
173-
type: "string",
174-
description: "Set esbuild format (cjs|esm)",
175-
},
176-
"esbuild-out-ext": {
177-
type: "string",
178-
description: "Set esbuild outExtension",
179-
example: "aws-lambda --esbuild-out-ext .mjs --esbuild-format esm",
180-
},
181-
help: { type: "boolean", short: "h" },
187+
"optimize-build": { type: "boolean", default: true, description: "externalize dependencies and other stuff during dev" },
188+
"shim-require": { type: "boolean", default: false, description: "shim 'require()', '__dirname' and '__filename' when bundeling Lambdas with ESM format" },
189+
help: { type: "boolean", short: "h", skipFullPrint: true },
190+
version: { type: "boolean", short: "v", skipFullPrint: true },
191+
"esbuild-external": { type: "string", multiple: true, skip: true },
192+
"esbuild-resolveExtensions": { type: "string", multiple: true, skip: true },
193+
"esbuild-mainFields": { type: "string", multiple: true, skip: true },
194+
"esbuild-conditions": { type: "string", multiple: true, skip: true },
195+
"esbuild-entryPoints": { type: "string", multiple: true, skip: true },
196+
"esbuild-inject": { type: "string", multiple: true, skip: true },
197+
"esbuild-nodePaths": { type: "string", multiple: true, skip: true },
182198
};
183199

184200
const { values } = parseArgs({
@@ -187,11 +203,11 @@ const { values } = parseArgs({
187203
});
188204

189205
const { port, config, debug, help, runtime, definitions, timeout, functions, handlerName, exclude, env } = values;
190-
const esbuildFormat = values["esbuild-format"];
191-
const esbuildOutExtension = values["esbuild-out-ext"];
192-
const esbuildOutdir = values["esbuild-outdir"];
193206

194-
if (help) {
207+
if (values.version) {
208+
console.log(version);
209+
process.exit(0);
210+
} else if (help) {
195211
printHelpAndExit();
196212
}
197213

@@ -202,39 +218,44 @@ if (definitions && functions) {
202218
// @ts-ignore
203219
const functionDefs = functions ? await getFromGlob(new RegExp(exclude), handlerName, functions as string[]) : await getFunctionsDefinitionFromFile(definitions as string);
204220

205-
let esbuildOptions = {};
221+
let esbuildOptions: Record<string, any> = {};
206222

207-
if (typeof esbuildFormat == "string") {
208-
// @ts-ignore
209-
esbuildOptions.format = esbuildFormat;
210-
}
223+
for (const opt of Object.keys(values).filter((x) => x.startsWith("esbuild-"))) {
224+
const optionRawName = opt.split("esbuild-")[1];
211225

212-
if (typeof esbuildOutdir == "string") {
213-
// @ts-ignore
214-
esbuildOptions.outdir = esbuildOutdir;
215-
}
226+
if (optionRawName.includes(".")) {
227+
const dotIndex = optionRawName.indexOf(".");
228+
const topLevelName = optionRawName.slice(0, dotIndex);
229+
const childName = optionRawName.slice(dotIndex + 1);
216230

217-
if (typeof esbuildOutExtension == "string") {
218-
// @ts-ignore
219-
esbuildOptions.outExtension = {
220-
".js": esbuildOutExtension,
221-
};
231+
if (!esbuildOptions[topLevelName]) {
232+
esbuildOptions[topLevelName] = {};
233+
}
234+
235+
esbuildOptions[topLevelName][childName] = values[opt];
236+
} else {
237+
esbuildOptions[optionRawName] = values[opt];
238+
}
222239
}
223240

224-
run({
225-
// @ts-ignore
241+
const optimizeBuild = values["optimize-build"];
242+
const shimRequire = values["shim-require"];
243+
244+
const opt = {
226245
debug,
227-
// @ts-ignore
228246
configPath: config,
229247
port: getNumberOrDefault(port, 0),
230248
functions: functionDefs,
231-
// @ts-ignore
232249
esbuild: esbuildOptions,
250+
optimizeBuild,
251+
shimRequire,
233252
defaults: {
234253
// @ts-ignore
235254
environment: getDefaultEnvs(env),
236-
// @ts-ignore
237255
runtime,
238256
timeout: getNumberOrDefault(timeout, 3),
239257
},
240-
});
258+
};
259+
260+
// @ts-ignore
261+
run(opt);

0 commit comments

Comments
 (0)