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

fix: add name and generator support to ISC #1732

Merged
merged 9 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/runtimes/node/in_source_config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type ISCValues = {
schedule?: string
methods?: string[]
trafficRules?: TrafficRules
name?: string
generator?: string
}

export interface StaticAnalysisResult extends ISCValues {
Expand Down Expand Up @@ -177,6 +179,14 @@ export const parseSource = (source: string, { functionName }: FindISCDeclaration
result.schedule = configExport.schedule
}

if (typeof configExport.name === 'string') {
result.name = configExport.name
}

if (typeof configExport.generator === 'string') {
result.generator = configExport.generator
}

if (configExport.method !== undefined) {
result.methods = normalizeMethods(configExport.method, functionName)
}
Expand Down
6 changes: 3 additions & 3 deletions src/runtimes/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const zipFunction: ZipFunction = async function ({
invocationMode = INVOCATION_MODE.Background
}

const { trafficRules } = staticAnalysisResult
const { trafficRules, generator: staticAnalysisGenerator, name: staticAnalysisName } = staticAnalysisResult

const outputModuleFormat =
extname(finalMainFile) === MODULE_FILE_EXTENSION.MJS ? MODULE_FORMAT.ESM : MODULE_FORMAT.COMMONJS
Expand All @@ -151,9 +151,9 @@ const zipFunction: ZipFunction = async function ({
bundler: bundlerName,
bundlerWarnings,
config,
displayName: config?.name,
displayName: staticAnalysisName || config?.name,
entryFilename: zipPath.entryFilename,
generator: config?.generator || getInternalValue(isInternal),
generator: staticAnalysisGenerator || config?.generator || getInternalValue(isInternal),
inputs,
includedFiles,
staticAnalysisResult,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"config": {
"name": "should not be respected, ISC takes precedence",
"generator": "should not be respected, ISC takes precedence"
},
"version": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default () => new Response('hello world')

export const config = {
name: 'SSR Function',
generator: 'next-runtime@1.2.3',
}
15 changes: 15 additions & 0 deletions tests/unit/runtimes/node/in_source_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,19 @@ describe('V2 API', () => {
expect(routes).toEqual([{ pattern: '/products', literal: '/products', methods: [], prefer_static: true }])
})
})

test('Understands name and generator', () => {
const source = `
export default async () => new Response("Hello!")
export const config = { name: "foo", generator: "bar@1.2.3" }`

const isc = parseSource(source, options)
expect(isc).toEqual({
inputModuleFormat: 'esm',
routes: [],
runtimeAPIVersion: 2,
name: 'foo',
generator: 'bar@1.2.3',
})
})
})
22 changes: 22 additions & 0 deletions tests/v2api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,26 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
expect(body).toBe('foo!bar')
})
})

test('Name and Generator are taken from ISC and take precedence over deploy config', async () => {
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
const manifestPath = join(tmpDir, 'manifest.json')

const fixtureName = 'v2-api-name-generator'
const pathInternal = join(fixtureName, '.netlify', 'functions-internal')

const {
files: [func],
} = await zipFixture(pathInternal, {
fixtureDir: FIXTURES_ESM_DIR,
length: 1,
opts: {
manifest: manifestPath,
configFileDirectories: [join(FIXTURES_ESM_DIR, fixtureName)],
},
})

expect(func.displayName).toBe('SSR Function')
expect(func.generator).toBe('next-runtime@1.2.3')
})
})
Loading