Skip to content
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

feat: export function to bundle edge functions #4271

Merged
merged 12 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 4 additions & 1 deletion packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"version": "27.0.1",
"description": "Netlify build module",
"type": "module",
"exports": "./src/core/main.js",
"exports": {
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
".": "./src/core/main.js",
"./src/steps": "./src/steps/index.js"
},
"main": "./src/core/main.js",
"types": "types/index.d.ts",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion packages/build/src/plugins_core/edge_functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { parseManifest } from './lib/internal_manifest.js'
const DENO_CLI_CACHE_DIRECTORY = '.netlify/plugins/deno-cli'
const IMPORT_MAP_FILENAME = 'edge-functions-import-map.json'

const coreStep = async function ({
export const coreStep = async function ({
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
buildDir,
constants: {
EDGE_FUNCTIONS_DIST: distDirectory,
Expand Down
1 change: 1 addition & 0 deletions packages/build/src/steps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as runCoreStep } from './run_core_step.js'
209 changes: 209 additions & 0 deletions packages/build/src/steps/run_core_step.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/* eslint-disable max-lines */
import { getConfigOpts, loadConfig } from '../core/config.js'
import { getConstants } from '../core/constants.js'
import { normalizeFlags } from '../core/normalize_flags.js'
import { getSeverity } from '../core/severity.js'
import { handleBuildError } from '../error/handle.js'
import { getErrorInfo } from '../error/info.js'
import { startErrorMonitor } from '../error/monitor/start.js'
import { getBufferLogs } from '../log/logger.js'
import { logBuildStart } from '../log/messages/core.js'
import { bundleEdgeFunctions } from '../plugins_core/edge_functions/index.js'
import { reportStatuses } from '../status/report.js'

import { runSteps } from './run_steps.js'

/**
* Executes specified build steps for functions
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {object} [flags] - build configuration CLI flags
* @param {string} [flags.config] - Path to the configuration file
* @param {string} [flags.cwd] - Current directory. Used to retrieve the configuration file
* @param {string} [flags.repositoryRoot] - Git repository root directory. Used to retrieve the configuration file.
* @param {string} [flags.apiHost] - Netlify API endpoint
* @param {string} [flags.token] - Netlify API token for authentication
* @param {string} [flags.siteId] - Netlify Site ID
* @param {string} [flags.deployId] - Netlify Deploy ID
* @param {string} [flags.context] - Build context
* @param {string} [flags.branch] - Repository branch
* @param {boolean} [flags.dry=false] - Run in dry mode, i.e. printing steps without executing them
* @param {string} [flags.nodePath] - Path to the Node.js binary to use in the build command and plugins
* @param {boolean} [flags.buffer=false] - Buffer output instead of printing it
*
* @returns {object} buildResult
* @returns {boolean} buildResult.success - Whether build succeeded or failed
* @returns {number} buildResult.severityCode - Build success/failure status among:
* 0 (success), 1 (build cancelled), 2 (user error), 3 (plugin error), 4 (system error). Can be used as exit code.
* @returns {string[]} buildResult.logs - When using the `buffer` option, all log messages
*/
export default async function runCoreStep(buildSteps, flags = {}) {
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const { errorMonitor, mode, logs, debug, ...flagsA } = startBuild(flags)

jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const errorParams = { errorMonitor, mode, logs, debug }

try {
const { netlifyConfig: netlifyConfigA, configMutations } = await executeBuildStep({
...flagsA,
errorMonitor,
mode,
logs,
debug,
errorParams,
buildSteps,
})

jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const { success, severityCode } = getSeverity('success')

return { success, severityCode, netlifyConfig: netlifyConfigA, configMutations, logs }
} catch (error) {
const { severity } = await handleBuildError(error, errorParams)

jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const { success, severityCode } = getSeverity(severity)

return { success, severityCode, logs }
}
}

const startBuild = function (flags) {
const logs = getBufferLogs(flags)

logBuildStart(logs)

const { bugsnagKey, ...flagsA } = normalizeFlags(flags, logs)

jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const errorMonitor = startErrorMonitor({ flags: flagsA, logs, bugsnagKey })

return { ...flagsA, errorMonitor, logs }
}

const getBuildSteps = function (buildSteps) {
const allSteps = []

if (buildSteps.includes('edgeFunctionsBundling')) {
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line fp/no-mutating-methods
allSteps.push(bundleEdgeFunctions)
}

return allSteps
}

const executeBuildStep = async function ({
config,
defaultConfig,
cachedConfig,
debug,
nodePath,
functionsDistDir,
edgeFunctionsDistDir,
errorMonitor,
mode,
logs,
errorParams,
featureFlags,
buildSteps,
repositoryRoot,
}) {
const configOpts = getConfigOpts({
config,
defaultConfig,
featureFlags,
mode,
repositoryRoot,
})

jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const {
netlifyConfig,
buildDir,
siteInfo,
childEnv,
userNodeVersion,
repositoryRoot: repositoryRootA,
} = await loadConfig({
configOpts,
cachedConfig,
debug,
logs,
nodePath,
timers: [],
})

jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
const constants = await getConstants({
buildDir,
functionsDistDir,
edgeFunctionsDistDir,
netlifyConfig,
siteInfo,
mode,
})

// eslint-disable-next-line fp/no-mutating-assign
Object.assign(errorParams, { netlifyConfig, siteInfo, childEnv, userNodeVersion })

try {
const { netlifyConfig: netlifyConfigA, configMutations } = await runBuildStep({
netlifyConfig,
buildDir,
nodePath,
logs,
debug,
constants,
featureFlags,
childEnv,
buildSteps,
repositoryRoot: repositoryRootA,
})

return {
netlifyConfig: netlifyConfigA,
configMutations,
}
} catch (error) {
const [{ statuses }] = getErrorInfo(error)

await reportStatuses({
statuses,
childEnv,
mode,
netlifyConfig,
errorMonitor,
logs,
debug,
})

throw error
}
}

const runBuildStep = async function ({
netlifyConfig,
buildDir,
nodePath,
constants,
logs,
debug,
featureFlags,
childEnv,
buildSteps,
repositoryRoot,
}) {
try {
const { netlifyConfig: netlifyConfigA, configMutations } = await runSteps({
steps: getBuildSteps(buildSteps),
buildDir,
nodePath,
constants,
netlifyConfig,
logs,
debug,
timers: [],
featureFlags,
childEnv,
repositoryRoot,
})

return { netlifyConfig: netlifyConfigA, configMutations }
} catch (error) {
console.error(error)
}
}
/* eslint-enable max-lines */