Skip to content

Commit

Permalink
fix: generate entry file on naming conflicts (#1310)
Browse files Browse the repository at this point in the history
Co-authored-by: Karin Hendrikse <30577427+khendrikse@users.noreply.github.com>
  • Loading branch information
danez and khendrikse committed Jan 12, 2023
1 parent ea4008e commit e74cb49
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/runtimes/node/utils/zip.ts
Expand Up @@ -108,10 +108,6 @@ const createZipArchive = async function ({
const destPath = join(destFolder, `${basename(filename, extension)}.zip`)
const { archive, output } = startZip(destPath)

// We don't need an entry file if it would end up with the same path as the
// function's main file.
const needsEntryFile = !isNamedLikeEntryFile(mainFile, { basePath, filename })

// There is a naming conflict with the entry file if one of the supporting
// files (i.e. not the main file) has the path that the entry file needs to
// take.
Expand All @@ -121,6 +117,10 @@ const createZipArchive = async function ({
mainFile,
})

// We don't need an entry file if it would end up with the same path as the
// function's main file. Unless we have a file conflict and need to move everything into a subfolder
const needsEntryFile = hasEntryFileConflict || !isNamedLikeEntryFile(mainFile, { basePath, filename })

// If there is a naming conflict, we move all user files (everything other
// than the entry file) to its own sub-directory.
const userNamespace = hasEntryFileConflict ? DEFAULT_USER_SUBDIRECTORY : ''
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/naming_conflict/func1/func1.js
@@ -0,0 +1,3 @@
exports.handler = function handler (event, context) {
return import('./func1.mjs').then(m => m.handler(event, context))
}
1 change: 1 addition & 0 deletions tests/fixtures/naming_conflict/func1/func1.mjs
@@ -0,0 +1 @@
export const handler = () => true
22 changes: 22 additions & 0 deletions tests/main.test.ts
Expand Up @@ -1383,6 +1383,28 @@ describe('zip-it-and-ship-it', () => {
},
)

testMany(
'Generates a entry file if no entry file needed but naming conflict occurs',
['bundler_default', 'bundler_nft'],
async (options) => {
const fixtureName = 'naming_conflict'
const opts = merge(options, {
basePath: join(FIXTURES_DIR, fixtureName),
})
const { tmpDir } = await zipNode(fixtureName, {
opts,
length: 1,
})

const function2Entry = await importFunctionFile(`${tmpDir}/func1.js`)

expect(await function2Entry.handler()).toBe(true)

await expect(`${tmpDir}/src/func1.js`).toPathExist()
await expect(`${tmpDir}/src/func1.mjs`).toPathExist()
},
)

testMany(
'Bundles functions from multiple directories when the first argument of `zipFunctions()` is an array',
['bundler_esbuild', 'bundler_default', 'bundler_nft'],
Expand Down

1 comment on commit e74cb49

@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.1s
  • largeDepsNft: 8.2s
  • largeDepsZisi: 16s

Please sign in to comment.