Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: extend package.json logic to archiveFormat: "none" (#1516)
Browse files Browse the repository at this point in the history
* feat: extend `package.json` logic to `archiveFormat: "none"`

* refactor: reuse common logic
  • Loading branch information
eduardoboucas committed Aug 3, 2023
1 parent 4bc75ba commit 7d2dad5
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/runtimes/node/utils/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const createDirectory = async function ({
filename,
mainFile,
moduleFormat,
name,
repositoryRoot,
rewrites = new Map(),
runtimeAPIVersion,
srcFiles,
Expand Down Expand Up @@ -124,6 +126,22 @@ const createDirectory = async function ({
{ concurrency: COPY_FILE_CONCURRENCY },
)

if (tsExtensions.has(extension) && moduleFormat === MODULE_FORMAT.ESM && runtimeAPIVersion === 2) {
const packageJSON = await ensurePackageJSONWithModuleType({
basePath,
mainFile,
name,
repositoryRoot,
userNamespace: DEFAULT_USER_SUBDIRECTORY,
})

if (packageJSON) {
const absoluteDestPath = join(functionFolder, packageJSON.path)

await writeFile(absoluteDestPath, packageJSON.contents)
}
}

return { path: functionFolder, entryFilename }
}

Expand Down Expand Up @@ -190,9 +208,19 @@ const createZipArchive = async function ({

if (runtimeAPIVersion === 2) {
addBootstrapFile(srcFiles, aliases)
}

if (tsExtensions.has(extension) && moduleFormat === MODULE_FORMAT.ESM && runtimeAPIVersion === 2) {
const packageJSON = await ensurePackageJSONWithModuleType({
basePath,
mainFile,
name,
repositoryRoot,
userNamespace,
})

if (tsExtensions.has(extension) && moduleFormat === MODULE_FORMAT.ESM) {
await ensurePackageJSONWithModuleType({ archive, basePath, mainFile, name, repositoryRoot, userNamespace })
if (packageJSON) {
addZipContent(archive, packageJSON.contents, packageJSON.path)
}
}

Expand Down Expand Up @@ -241,14 +269,12 @@ const addStat = async function (cache: RuntimeCache, srcFile: string) {
}

const ensurePackageJSONWithModuleType = async function ({
archive,
basePath,
mainFile,
name,
repositoryRoot,
userNamespace,
}: {
archive: ZipArchive
basePath: string
mainFile: string
name: string
Expand Down Expand Up @@ -291,7 +317,10 @@ const ensurePackageJSONWithModuleType = async function ({
const normalizedPath = normalizeFilePath({ commonPrefix: basePath, path, userNamespace })
const contents = JSON.stringify({ type: 'module' })

addZipContent(archive, contents, normalizedPath)
return {
contents,
path: normalizedPath,
}
}

const zipJsFile = function ({
Expand Down

1 comment on commit 7d2dad5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

  • largeDepsEsbuild: 2.5s
  • largeDepsNft: 7.7s
  • largeDepsZisi: 14.6s

Please sign in to comment.