Skip to content
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
46 changes: 38 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"@netlify/plugin-edge-handlers": "^1.11.6",
"@netlify/plugins-list": "^2.6.0",
"@netlify/traffic-mesh-agent": "^0.27.10",
"@netlify/zip-it-and-ship-it": "^3.1.0",
"@netlify/zip-it-and-ship-it": "^3.2.0",
"@oclif/command": "^1.6.1",
"@oclif/config": "^1.15.1",
"@oclif/errors": "^1.3.4",
Expand Down
1 change: 1 addition & 0 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class DevCommand extends Command {
}

await startFunctionsServer({
config,
settings,
site,
log,
Expand Down
71 changes: 71 additions & 0 deletions src/function-builder-detectors/zisi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const path = require('path')

const { zipFunction, zipFunctions } = require('@netlify/zip-it-and-ship-it')
const makeDir = require('make-dir')

const { getPathInProject } = require('../lib/settings')
const { NETLIFYDEVERR } = require('../utils/logo')

const bundleFunctions = ({ config, sourceDirectory, targetDirectory, updatedPath }) => {
// If `updatedPath` is truthy, it means we're running the build command due
// to an update to a file. If that's the case, we run `zipFunction` to bundle
// that specific function only.
if (updatedPath) {
return zipFunction(updatedPath, targetDirectory, {
archiveFormat: 'none',
config,
})
}

return zipFunctions(sourceDirectory, targetDirectory, {
archiveFormat: 'none',
config,
})
}

// The function configuration keys returned by @netlify/config are not an exact
// match to the properties that @netlify/zip-it-and-ship-it expects. We do that
// translation here.
const normalizeFunctionsConfig = (functionsConfig = {}) =>
Object.entries(functionsConfig).reduce(
(result, [pattern, config]) => ({
...result,
[pattern]: {
externalNodeModules: config.external_node_modules,
ignoredNodeModules: config.ignored_node_modules,
nodeBundler: config.node_bundler === 'esbuild' ? 'esbuild_zisi' : config.node_bundler,
},
}),
{},
)

const getTargetDirectory = async ({ errorExit }) => {
const targetDirectory = path.resolve(getPathInProject(['functions-serve']))

try {
await makeDir(targetDirectory)
} catch (error) {
errorExit(`${NETLIFYDEVERR} Could not create directory: ${targetDirectory}`)
}

return targetDirectory
}

module.exports = async function handler({ config, errorExit, functionsDirectory: sourceDirectory }) {
const functionsConfig = normalizeFunctionsConfig(config.functions)
const isUsingEsbuild = functionsConfig['*'] && functionsConfig['*'].nodeBundler === 'esbuild_zisi'

if (!isUsingEsbuild) {
return false
}

const targetDirectory = await getTargetDirectory({ errorExit })

return {
build: (updatedPath) => bundleFunctions({ config: functionsConfig, sourceDirectory, targetDirectory, updatedPath }),
builderName: 'zip-it-and-ship-it',
omitFileChangesLog: true,
src: sourceDirectory,
target: targetDirectory,
}
}
6 changes: 4 additions & 2 deletions src/utils/detect-functions-builder.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const fs = require('fs')
const path = require('path')

const detectFunctionsBuilder = async function (projectDir) {
const detectFunctionsBuilder = async function (parameters) {
const detectors = fs
.readdirSync(path.join(__dirname, '..', 'function-builder-detectors'))
// only accept .js detector files
.filter((filename) => filename.endsWith('.js'))
// Sorting by filename
.sort()
// eslint-disable-next-line node/global-require, import/no-dynamic-require
.map((det) => require(path.join(__dirname, '..', `function-builder-detectors/${det}`)))

for (const detector of detectors) {
// eslint-disable-next-line no-await-in-loop
const settings = await detector(projectDir)
const settings = await detector(parameters)
if (settings) {
return settings
}
Expand Down
Loading