@@ -314,6 +314,14 @@ class Esbuild {
314
314
315
315
async _buildProperties ( ) {
316
316
const defaultConfig = { bundle : true , minify : false , sourcemap : true }
317
+
318
+ const packageJson = await this . _readPackageJson ( )
319
+
320
+ // If the user explicitly set the type to "module" then we need to set the output format to ESM
321
+ if ( packageJson . type === 'module' ) {
322
+ defaultConfig . format = `esm`
323
+ }
324
+
317
325
if (
318
326
this . serverless . service . build &&
319
327
this . serverless . service . build !== 'esbuild' &&
@@ -356,12 +364,6 @@ class Esbuild {
356
364
this . serverless . service . build . esbuild ,
357
365
)
358
366
359
- const packageJson = await this . _readPackageJson ( )
360
-
361
- if ( packageJson . type === 'module' ) {
362
- mergedOptions . format = `esm`
363
- }
364
-
365
367
if ( this . serverless . service . build . esbuild . sourcemap === true ) {
366
368
mergedOptions . sourcemap = true
367
369
} else if ( this . serverless . service . build . esbuild . sourcemap === false ) {
@@ -711,6 +713,17 @@ class Esbuild {
711
713
'' ,
712
714
)
713
715
716
+ const packageJsonPath = path . join (
717
+ this . serverless . config . serviceDir ,
718
+ '.serverless' ,
719
+ 'build' ,
720
+ 'package.json' ,
721
+ )
722
+
723
+ if ( existsSync ( packageJsonPath ) ) {
724
+ zip . file ( packageJsonPath , { name : `package.json` } )
725
+ }
726
+
714
727
const handlerZipPath = path . join (
715
728
this . serverless . config . serviceDir ,
716
729
'.serverless' ,
@@ -719,6 +732,7 @@ class Esbuild {
719
732
)
720
733
721
734
zip . file ( handlerZipPath , { name : `${ handlerPath } .js` } )
735
+
722
736
if ( existsSync ( `${ handlerZipPath } .map` ) ) {
723
737
zip . file ( `${ handlerZipPath } .map` , {
724
738
name : `${ handlerPath } .js.map` ,
@@ -803,6 +817,17 @@ class Esbuild {
803
817
'' ,
804
818
)
805
819
820
+ const packageJsonPath = path . join (
821
+ this . serverless . config . serviceDir ,
822
+ '.serverless' ,
823
+ 'build' ,
824
+ 'package.json' ,
825
+ )
826
+
827
+ if ( existsSync ( packageJsonPath ) ) {
828
+ zip . file ( packageJsonPath , { name : `package.json` } )
829
+ }
830
+
806
831
const handlerZipPath = path . join (
807
832
this . serverless . config . serviceDir ,
808
833
'.serverless' ,
0 commit comments