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

Commit

Permalink
fix: add name and generator support to ISC (#1732)
Browse files Browse the repository at this point in the history
* feat: add support to ISC parser

* feat: use parsed values

* chore: remove unneeded import

* chore: prettier

* fix: flip precedence and extract to own test

* fix: fmt

* fix: switch back to logical or
  • Loading branch information
Skn0tt authored May 7, 2024
1 parent 01f1c33 commit 7b03070
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
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')
})
})

1 comment on commit 7b03070

@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.2s
  • largeDepsNft: 5s
  • largeDepsZisi: 9.2s

Please sign in to comment.