Skip to content

Commit

Permalink
feat: add priority field to manifest (#1705)
Browse files Browse the repository at this point in the history
* feat: add `priority` field to manifest

* chore: fix test
  • Loading branch information
eduardoboucas committed Jan 31, 2024
1 parent 64ee032 commit f129ca1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/manifest.ts
Expand Up @@ -19,6 +19,7 @@ interface ManifestFunction {
displayName?: string
bundler?: string
generator?: string
priority?: number
}

export interface Manifest {
Expand Down Expand Up @@ -53,6 +54,7 @@ const formatFunctionForManifest = ({
mainFile,
name,
path,
priority,
routes,
runtime,
runtimeVersion,
Expand All @@ -67,6 +69,7 @@ const formatFunctionForManifest = ({
buildData: { runtimeAPIVersion },
mainFile,
name,
priority,
runtimeVersion,
path: resolve(path),
runtime,
Expand Down
4 changes: 4 additions & 0 deletions src/priority.ts
@@ -0,0 +1,4 @@
export enum Priority {
GeneratedFunction = 0,
UserFunction = 10,
}
3 changes: 3 additions & 0 deletions src/runtimes/node/index.ts
Expand Up @@ -3,6 +3,7 @@ import { extname, join } from 'path'
import { copyFile } from 'cp-file'

import { INVOCATION_MODE } from '../../function.js'
import { Priority } from '../../priority.js'
import getInternalValue from '../../utils/get_internal_value.js'
import { GetSrcFilesFunction, Runtime, RUNTIME, ZipFunction } from '../runtime.js'

Expand Down Expand Up @@ -142,6 +143,7 @@ const zipFunction: ZipFunction = async function ({

const outputModuleFormat =
extname(finalMainFile) === MODULE_FILE_EXTENSION.MJS ? MODULE_FORMAT.ESM : MODULE_FORMAT.COMMONJS
const priority = isInternal ? Priority.GeneratedFunction : Priority.UserFunction

return {
bundler: bundlerName,
Expand All @@ -157,6 +159,7 @@ const zipFunction: ZipFunction = async function ({
outputModuleFormat,
nativeNodeModules,
path: zipPath.path,
priority,
runtimeVersion:
runtimeAPIVersion === 2 ? getNodeRuntimeForV2(config.nodeVersion) : getNodeRuntime(config.nodeVersion),
}
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/runtime.ts
Expand Up @@ -53,6 +53,7 @@ export interface ZipFunctionResult {
outputModuleFormat?: ModuleFormat
nativeNodeModules?: object
path: string
priority?: number
runtimeVersion?: string
staticAnalysisResult?: StaticAnalysisResult
entryFilename: string
Expand Down
34 changes: 34 additions & 0 deletions tests/main.test.ts
Expand Up @@ -2864,3 +2864,37 @@ describe('zip-it-and-ship-it', () => {
expect(duplicates).toHaveLength(0)
})
})

test('Adds a `priority` field to the generated manifest file', async () => {
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
const fixtureName = 'multiple-src-directories'
const manifestPath = join(tmpDir, 'manifest.json')
const paths = {
generated: `${fixtureName}/.netlify/internal-functions`,
user: `${fixtureName}/netlify/functions`,
}

await zipNode([paths.generated, paths.user], {
length: 3,
opts: {
internalSrcFolder: join(FIXTURES_DIR, paths.generated),
manifest: manifestPath,
},
})

const manifest = JSON.parse(await readFile(manifestPath, 'utf-8'))

expect(manifest.version).toBe(1)
expect(manifest.system.arch).toBe(arch)
expect(manifest.system.platform).toBe(platform)
expect(manifest.timestamp).toBeTypeOf('number')

const userFunction1 = manifest.functions.find((fn) => fn.name === 'function_user')
expect(userFunction1.priority).toBe(10)

const userFunction2 = manifest.functions.find((fn) => fn.name === 'function')
expect(userFunction2.priority).toBe(10)

const generatedFunction1 = manifest.functions.find((fn) => fn.name === 'function_internal')
expect(generatedFunction1.priority).toBe(0)
})

1 comment on commit f129ca1

@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: 1.4s
  • largeDepsNft: 5.5s
  • largeDepsZisi: 10.4s

Please sign in to comment.