Skip to content

Commit a7d04bd

Browse files
authored
fix(esbuild): include package.json in artifact if type is module (#12872)
1 parent 549a984 commit a7d04bd

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

lib/plugins/esbuild/index.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ class Esbuild {
314314

315315
async _buildProperties() {
316316
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+
317325
if (
318326
this.serverless.service.build &&
319327
this.serverless.service.build !== 'esbuild' &&
@@ -356,12 +364,6 @@ class Esbuild {
356364
this.serverless.service.build.esbuild,
357365
)
358366

359-
const packageJson = await this._readPackageJson()
360-
361-
if (packageJson.type === 'module') {
362-
mergedOptions.format = `esm`
363-
}
364-
365367
if (this.serverless.service.build.esbuild.sourcemap === true) {
366368
mergedOptions.sourcemap = true
367369
} else if (this.serverless.service.build.esbuild.sourcemap === false) {
@@ -711,6 +713,17 @@ class Esbuild {
711713
'',
712714
)
713715

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+
714727
const handlerZipPath = path.join(
715728
this.serverless.config.serviceDir,
716729
'.serverless',
@@ -719,6 +732,7 @@ class Esbuild {
719732
)
720733

721734
zip.file(handlerZipPath, { name: `${handlerPath}.js` })
735+
722736
if (existsSync(`${handlerZipPath}.map`)) {
723737
zip.file(`${handlerZipPath}.map`, {
724738
name: `${handlerPath}.js.map`,
@@ -803,6 +817,17 @@ class Esbuild {
803817
'',
804818
)
805819

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+
806831
const handlerZipPath = path.join(
807832
this.serverless.config.serviceDir,
808833
'.serverless',

0 commit comments

Comments
 (0)