-
Notifications
You must be signed in to change notification settings - Fork 92
feat: support config property in default export
#6991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eduardoboucas
wants to merge
3
commits into
main
Choose a base branch
from
feat/config-default-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−12
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ import { ExtendedRoute, Route, getRoutes } from '../../../utils/routes.js' | |
| import { RUNTIME } from '../../runtime.js' | ||
| import type { BindingMethod } from '../parser/bindings.js' | ||
| import { createBindingsMethod } from '../parser/bindings.js' | ||
| import { traverseNodes } from '../parser/exports.js' | ||
| import { parseObject, traverseNodes } from '../parser/exports.js' | ||
| import { getImports } from '../parser/imports.js' | ||
| import { safelyParseSource, safelyReadSource } from '../parser/index.js' | ||
| import type { ModuleFormat } from '../utils/module_format.js' | ||
|
|
@@ -87,25 +87,38 @@ export const inSourceConfig = functionConfig | |
| export type InSourceConfig = z.infer<typeof inSourceConfig> | ||
|
|
||
| /** | ||
| * Extracts event subscription slugs from the default export expression, | ||
| * if it's an object whose property names match known event handlers. | ||
| * Resolves the default export expression to an ObjectExpression if possible, | ||
| * following identifier bindings when needed. | ||
| */ | ||
| const getEventSubscriptions = ( | ||
| const resolveObjectExpression = ( | ||
| expression: Expression | Declaration | undefined, | ||
| getAllBindings: BindingMethod, | ||
| ): string[] => { | ||
| let objectExpression: ObjectExpression | undefined | ||
|
|
||
| ): ObjectExpression | undefined => { | ||
| if (expression?.type === 'ObjectExpression') { | ||
| objectExpression = expression | ||
| } else if (expression?.type === 'Identifier') { | ||
| return expression | ||
| } | ||
|
|
||
| if (expression?.type === 'Identifier') { | ||
| const binding = getAllBindings().get(expression.name) | ||
|
|
||
| if (binding?.type === 'ObjectExpression') { | ||
| objectExpression = binding | ||
| return binding | ||
| } | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * Extracts event subscription slugs from the default export expression, | ||
| * if it's an object whose property names match known event handlers. | ||
| */ | ||
| const getEventSubscriptions = ( | ||
| expression: Expression | Declaration | undefined, | ||
| getAllBindings: BindingMethod, | ||
| ): string[] => { | ||
| const objectExpression = resolveObjectExpression(expression, getAllBindings) | ||
|
|
||
| if (!objectExpression) { | ||
| return [] | ||
| } | ||
|
|
@@ -130,6 +143,41 @@ const getEventSubscriptions = ( | |
| return events | ||
| } | ||
|
|
||
| /** | ||
| * Extracts a `config` property from the default export object expression, | ||
| * returning it as a plain object. This supports patterns like: | ||
| * | ||
| * ```js | ||
| * export default { | ||
| * fetch() { ... }, | ||
| * config: { path: "/hello" } | ||
| * } | ||
| * ``` | ||
| */ | ||
| const getConfigFromDefaultExport = ( | ||
| expression: Expression | Declaration | undefined, | ||
| getAllBindings: BindingMethod, | ||
| ): Record<string, unknown> | undefined => { | ||
| const objectExpression = resolveObjectExpression(expression, getAllBindings) | ||
|
|
||
| if (!objectExpression) { | ||
| return undefined | ||
| } | ||
|
|
||
| for (const property of objectExpression.properties) { | ||
| if ( | ||
| property.type === 'ObjectProperty' && | ||
| property.key.type === 'Identifier' && | ||
| property.key.name === 'config' && | ||
| property.value.type === 'ObjectExpression' | ||
| ) { | ||
| return parseObject(property.value) | ||
| } | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| const validateScheduleFunction = (functionFound: boolean, scheduleFound: boolean, functionName: string): void => { | ||
| if (!functionFound) { | ||
| throw new FunctionBundlingUserError( | ||
|
|
@@ -205,7 +253,11 @@ export const parseSource = (source: string, { functionName }: FindISCDeclaration | |
| result.eventSubscriptions = eventSubscriptions | ||
| } | ||
|
|
||
| const { data, error, success } = inSourceConfig.safeParse(configExport) | ||
| // Config from the default export object's `config` property is used as a | ||
| // fallback when no separate `export const config` exists. | ||
| const inlineConfig = getConfigFromDefaultExport(defaultExportExpression, getAllBindings) | ||
| const mergedConfigExport = Object.keys(configExport).length > 0 ? configExport : (inlineConfig ?? {}) | ||
| const { data, error, success } = inSourceConfig.safeParse(mergedConfigExport) | ||
|
Comment on lines
+256
to
+260
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicit named Line 259 uses key-count as an existence check. That makes 💡 Proposed fixdiff --git a/packages/zip-it-and-ship-it/src/runtimes/node/parser/exports.ts b/packages/zip-it-and-ship-it/src/runtimes/node/parser/exports.ts
@@
export const traverseNodes = (nodes: Statement[], getAllBindings: BindingMethod) => {
const handlerExports: ISCExport[] = []
let configExport: Record<string, unknown> = {}
+ let hasConfigExport = false
@@
if (esmConfigExports.length !== 0 && esmConfigExports[0].type === 'object-expression') {
configExport = esmConfigExports[0].object
+ hasConfigExport = true
}
@@
if (esmConfig !== undefined) {
configExport = esmConfig
+ hasConfigExport = true
return
}
@@
if (cjsConfigExports.length !== 0 && cjsConfigExports[0].type === 'object-expression') {
configExport = cjsConfigExports[0].object
+ hasConfigExport = true
}
})
- return { configExport, handlerExports, hasDefaultExport, defaultExportExpression, inputModuleFormat }
+ return { configExport, hasConfigExport, handlerExports, hasDefaultExport, defaultExportExpression, inputModuleFormat }
}
diff --git a/packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts b/packages/zip-it-and-ship-it/src/runtimes/node/in_source_config/index.ts
@@
- const { configExport, handlerExports, hasDefaultExport, defaultExportExpression, inputModuleFormat } = traverseNodes(
+ const { configExport, hasConfigExport, handlerExports, hasDefaultExport, defaultExportExpression, inputModuleFormat } = traverseNodes(
ast.body,
getAllBindings,
)
@@
- const mergedConfigExport = Object.keys(configExport).length > 0 ? configExport : (inlineConfig ?? {})
+ const mergedConfigExport = hasConfigExport ? configExport : (inlineConfig ?? {})🤖 Prompt for AI Agents |
||
|
|
||
| if (success) { | ||
| result.config = data | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"config"string-literal keys are currently ignored.getConfigFromDefaultExport(...)only matchesconfigwhen the key is an Identifier. It should also acceptStringLiteralkeys to handleexport default { "config": { ... } }.💡 Proposed fix
for (const property of objectExpression.properties) { if ( property.type === 'ObjectProperty' && - property.key.type === 'Identifier' && - property.key.name === 'config' && + ((property.key.type === 'Identifier' && property.key.name === 'config') || + (property.key.type === 'StringLiteral' && property.key.value === 'config')) && property.value.type === 'ObjectExpression' ) { return parseObject(property.value) } }🤖 Prompt for AI Agents