Skip to content

Commit

Permalink
fix: start functions server if internal functions directory exists (#…
Browse files Browse the repository at this point in the history
…3071)

* fix: start functions server if internal functions directory exists

* refactor: use right order in functions directories

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] authored Aug 3, 2021
1 parent 38c635f commit 0dc4402
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/lib/functions/netlify-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const BACKGROUND_SUFFIX = '-background'
class NetlifyFunction {
constructor({
config,
directory,
errorExit,
functionsDirectory,
mainFile,
name,
projectRoot,
Expand All @@ -15,8 +15,8 @@ class NetlifyFunction {
timeoutSynchronous,
}) {
this.config = config
this.directory = directory
this.errorExit = errorExit
this.functionsDirectory = functionsDirectory
this.mainFile = mainFile
this.name = name
this.projectRoot = projectRoot
Expand All @@ -41,9 +41,9 @@ class NetlifyFunction {
async build({ cache }) {
const buildFunction = await this.runtime.getBuildFunction({
config: this.config,
directory: this.directory,
errorExit: this.errorExit,
func: this,
functionsDirectory: this.functionsDirectory,
projectRoot: this.projectRoot,
})

Expand Down
5 changes: 2 additions & 3 deletions src/lib/functions/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ const runtimes = require('./runtimes')
const { watchDebounced } = require('./watcher')

class FunctionsRegistry {
constructor({ capabilities, config, errorExit, functionsDirectory, projectRoot, timeouts, warn }) {
constructor({ capabilities, config, errorExit, projectRoot, timeouts, warn }) {
this.capabilities = capabilities
this.config = config
this.errorExit = errorExit
this.functionsDirectory = functionsDirectory
this.logger = {
log,
warn,
Expand Down Expand Up @@ -186,8 +185,8 @@ class FunctionsRegistry {

const func = new NetlifyFunction({
config: this.config,
directory: directories.find((directory) => mainFile.startsWith(directory)),
errorExit: this.errorExit,
functionsDirectory: this.functionsDirectory,
mainFile,
name,
projectRoot: this.projectRoot,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/functions/runtimes/js/builders/zisi.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const addFunctionsConfigDefaults = (config) => ({
},
})

const buildFunction = async ({ cache, config, func, functionsDirectory, projectRoot, targetDirectory }) => {
const buildFunction = async ({ cache, config, directory, func, projectRoot, targetDirectory }) => {
const zipOptions = {
archiveFormat: 'none',
basePath: projectRoot,
Expand All @@ -33,7 +33,7 @@ const buildFunction = async ({ cache, config, func, functionsDirectory, projectR
// the function. The exception is when the function is a file at the
// root of the functions directory (e.g. `functions/my-func.js`). In
// this case, we use `mainFile` as the function path of `zipFunction`.
const entryPath = functionDirectory === functionsDirectory ? func.mainFile : functionDirectory
const entryPath = functionDirectory === directory ? func.mainFile : functionDirectory
const { inputs, path: functionPath } = await memoizedBuild({
cache,
cacheKey: `zisi-${entryPath}`,
Expand Down Expand Up @@ -67,7 +67,7 @@ const getTargetDirectory = async ({ errorExit }) => {
return targetDirectory
}

module.exports = async ({ config, errorExit, func, functionsDirectory, projectRoot }) => {
module.exports = async ({ config, directory, errorExit, func, projectRoot }) => {
const isTSFunction = path.extname(func.mainFile) === '.ts'
const functionsConfig = addFunctionsConfigDefaults(
normalizeFunctionsConfig({ functionsConfig: config.functions, projectRoot }),
Expand All @@ -88,7 +88,7 @@ module.exports = async ({ config, errorExit, func, functionsDirectory, projectRo

return {
build: ({ cache = {} }) =>
buildFunction({ cache, config: functionsConfig, func, functionsDirectory, projectRoot, targetDirectory }),
buildFunction({ cache, config: functionsConfig, directory, func, projectRoot, targetDirectory }),
builderName: 'zip-it-and-ship-it',
target: targetDirectory,
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/functions/runtimes/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const detectNetlifyLambdaWithCache = () => {
return netlifyLambdaDetectorCache
}

const getBuildFunction = async ({ config, errorExit, func, functionsDirectory, projectRoot }) => {
const getBuildFunction = async ({ config, directory, errorExit, func, projectRoot }) => {
const netlifyLambdaBuilder = await detectNetlifyLambdaWithCache()

if (netlifyLambdaBuilder) {
return netlifyLambdaBuilder.build
}

const zisiBuilder = await detectZisiBuilder({ config, errorExit, func, functionsDirectory, projectRoot })
const zisiBuilder = await detectZisiBuilder({ config, directory, errorExit, func, projectRoot })

if (zisiBuilder) {
return zisiBuilder.build
Expand All @@ -45,7 +45,7 @@ const getBuildFunction = async ({ config, errorExit, func, functionsDirectory, p
// returns as `srcFiles` the function directory, if there is one, or its
// main file otherwise.
const functionDirectory = dirname(func.mainFile)
const srcFiles = functionDirectory === functionsDirectory ? [func.mainFile] : [functionDirectory]
const srcFiles = functionDirectory === directory ? [func.mainFile] : [functionDirectory]

return () => ({ srcFiles })
}
Expand Down
14 changes: 8 additions & 6 deletions src/lib/functions/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,23 @@ const startFunctionsServer = async ({
timeouts,
prefix = '',
}) => {
// serve functions from zip-it-and-ship-it
// env variables relies on `url`, careful moving this code
if (settings.functions) {
const internalFunctionsDir = await getInternalFunctionsDir({ base: site.root })

// The order of the function directories matters. Leftmost directories take
// precedence.
const functionsDirectories = [settings.functions, internalFunctionsDir].filter(Boolean)

if (functionsDirectories.length !== 0) {
const functionsRegistry = new FunctionsRegistry({
capabilities,
config,
errorExit,
functionsDirectory: settings.functions,
projectRoot: site.root,
timeouts,
warn,
})
const internalFunctionsDir = await getInternalFunctionsDir({ base: site.root })

await functionsRegistry.scan([settings.functions, internalFunctionsDir].filter(Boolean))
await functionsRegistry.scan(functionsDirectories)

const server = await getFunctionsServer({
functionsRegistry,
Expand Down

1 comment on commit 0dc4402

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 331 MB

Please sign in to comment.