@@ -6,6 +6,7 @@ import path from "node:path";
6
6
import { pathToFileURL } from "node:url" ;
7
7
import { run , type ILambdaFunction } from "./standalone" with { external : "true" } ;
8
8
import { log } from "./lib/utils/colorize" ;
9
+ import { version , name } from "../package.json" ;
9
10
10
11
function printHelpAndExit ( ) {
11
12
log . setDebug ( true ) ;
@@ -18,10 +19,19 @@ function printHelpAndExit() {
18
19
for ( const [ optionName , value ] of Object . entries ( options ) ) {
19
20
let printableName = optionName ;
20
21
22
+ if ( value . skip ) {
23
+ continue ;
24
+ }
25
+
21
26
if ( value . short ) {
22
27
printableName += `, -${ value . short } ` ;
23
28
}
24
29
30
+ if ( value . skipFullPrint ) {
31
+ log . CYAN ( `\t --${ printableName } \n` ) ;
32
+ continue ;
33
+ }
34
+
25
35
let content = `\t\ttype: ${ value . type } ` ;
26
36
if ( value . description ) {
27
37
content += `\n\t\tdescription: ${ value . description } ` ;
@@ -40,6 +50,13 @@ function printHelpAndExit() {
40
50
log . GREY ( content ) ;
41
51
}
42
52
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 } ` ) ;
43
60
process . exit ( 0 ) ;
44
61
}
45
62
@@ -145,6 +162,8 @@ interface ICliOptions {
145
162
default ?: string | boolean | string [ ] | boolean [ ] | undefined ;
146
163
description ?: string ;
147
164
example ?: string ;
165
+ skipFullPrint ?: boolean ;
166
+ skip ?: boolean ;
148
167
}
149
168
150
169
const options : Record < string , ICliOptions > = {
@@ -155,7 +174,7 @@ const options: Record<string, ICliOptions> = {
155
174
timeout : { type : "string" , short : "t" , default : "3" , description : "Set default timeout." } ,
156
175
definitions : { type : "string" , short : "d" , description : "Path to .json, .mjs, .cjs file with Lambda function definitions." } ,
157
176
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
159
178
handlerName : { type : "string" , default : "handler" , description : "Handler function name. To be used with --functions." } ,
160
179
env : {
161
180
type : "string" ,
@@ -165,20 +184,17 @@ const options: Record<string, ICliOptions> = {
165
184
description : "Environment variables to be injected into Lambdas. All existing AWS_* are automatically injected." ,
166
185
example : "-e API_KEY=supersecret -e API_URL=https://website.com" ,
167
186
} ,
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 } ,
182
198
} ;
183
199
184
200
const { values } = parseArgs ( {
@@ -187,11 +203,11 @@ const { values } = parseArgs({
187
203
} ) ;
188
204
189
205
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" ] ;
193
206
194
- if ( help ) {
207
+ if ( values . version ) {
208
+ console . log ( version ) ;
209
+ process . exit ( 0 ) ;
210
+ } else if ( help ) {
195
211
printHelpAndExit ( ) ;
196
212
}
197
213
@@ -202,39 +218,44 @@ if (definitions && functions) {
202
218
// @ts -ignore
203
219
const functionDefs = functions ? await getFromGlob ( new RegExp ( exclude ) , handlerName , functions as string [ ] ) : await getFunctionsDefinitionFromFile ( definitions as string ) ;
204
220
205
- let esbuildOptions = { } ;
221
+ let esbuildOptions : Record < string , any > = { } ;
206
222
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 ] ;
211
225
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 ) ;
216
230
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
+ }
222
239
}
223
240
224
- run ( {
225
- // @ts -ignore
241
+ const optimizeBuild = values [ "optimize-build" ] ;
242
+ const shimRequire = values [ "shim-require" ] ;
243
+
244
+ const opt = {
226
245
debug,
227
- // @ts -ignore
228
246
configPath : config ,
229
247
port : getNumberOrDefault ( port , 0 ) ,
230
248
functions : functionDefs ,
231
- // @ts -ignore
232
249
esbuild : esbuildOptions ,
250
+ optimizeBuild,
251
+ shimRequire,
233
252
defaults : {
234
253
// @ts -ignore
235
254
environment : getDefaultEnvs ( env ) ,
236
- // @ts -ignore
237
255
runtime,
238
256
timeout : getNumberOrDefault ( timeout , 3 ) ,
239
257
} ,
240
- } ) ;
258
+ } ;
259
+
260
+ // @ts -ignore
261
+ run ( opt ) ;
0 commit comments