@netlify/build 27.20.4 → 27.20.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/lib/core/bin.js +42 -59
  2. package/lib/core/build.js +333 -536
  3. package/lib/core/config.js +94 -159
  4. package/lib/core/constants.js +95 -135
  5. package/lib/core/dev.js +26 -30
  6. package/lib/core/dry.js +18 -36
  7. package/lib/core/feature_flags.js +13 -16
  8. package/lib/core/flags.js +168 -169
  9. package/lib/core/lingering.js +44 -61
  10. package/lib/core/main.js +94 -136
  11. package/lib/core/missing_side_file.js +12 -24
  12. package/lib/core/normalize_flags.js +52 -63
  13. package/lib/core/severity.js +13 -15
  14. package/lib/core/user_node_version.js +26 -35
  15. package/lib/env/changes.js +29 -37
  16. package/lib/env/main.js +10 -15
  17. package/lib/env/metadata.js +63 -76
  18. package/lib/error/api.js +31 -40
  19. package/lib/error/build.js +27 -38
  20. package/lib/error/cancel.js +5 -6
  21. package/lib/error/colors.js +7 -9
  22. package/lib/error/handle.js +38 -49
  23. package/lib/error/info.js +26 -35
  24. package/lib/error/monitor/location.js +12 -17
  25. package/lib/error/monitor/normalize.js +75 -85
  26. package/lib/error/monitor/print.js +19 -41
  27. package/lib/error/monitor/report.js +102 -121
  28. package/lib/error/monitor/start.js +43 -56
  29. package/lib/error/parse/clean_stack.js +52 -69
  30. package/lib/error/parse/location.js +40 -52
  31. package/lib/error/parse/normalize.js +18 -23
  32. package/lib/error/parse/parse.js +59 -93
  33. package/lib/error/parse/plugin.js +42 -57
  34. package/lib/error/parse/properties.js +13 -20
  35. package/lib/error/parse/serialize_log.js +29 -37
  36. package/lib/error/parse/serialize_status.js +15 -23
  37. package/lib/error/parse/stack.js +29 -38
  38. package/lib/error/type.js +132 -150
  39. package/lib/install/functions.js +16 -24
  40. package/lib/install/local.js +31 -48
  41. package/lib/install/main.js +52 -66
  42. package/lib/install/missing.js +40 -53
  43. package/lib/log/colors.js +15 -22
  44. package/lib/log/description.js +16 -21
  45. package/lib/log/header.js +11 -13
  46. package/lib/log/header_func.js +11 -15
  47. package/lib/log/logger.js +88 -119
  48. package/lib/log/messages/compatibility.js +100 -158
  49. package/lib/log/messages/config.js +76 -92
  50. package/lib/log/messages/core.js +40 -60
  51. package/lib/log/messages/core_steps.js +63 -92
  52. package/lib/log/messages/dry.js +31 -53
  53. package/lib/log/messages/install.js +21 -28
  54. package/lib/log/messages/ipc.js +21 -30
  55. package/lib/log/messages/mutations.js +51 -71
  56. package/lib/log/messages/plugins.js +18 -31
  57. package/lib/log/messages/status.js +12 -14
  58. package/lib/log/messages/steps.js +14 -18
  59. package/lib/log/old_version.js +23 -32
  60. package/lib/log/serialize.js +7 -10
  61. package/lib/log/stream.js +48 -65
  62. package/lib/log/theme.js +22 -23
  63. package/lib/plugins/child/diff.js +31 -40
  64. package/lib/plugins/child/error.js +20 -26
  65. package/lib/plugins/child/lazy.js +11 -14
  66. package/lib/plugins/child/load.js +15 -22
  67. package/lib/plugins/child/logic.js +51 -58
  68. package/lib/plugins/child/main.js +32 -46
  69. package/lib/plugins/child/run.js +18 -27
  70. package/lib/plugins/child/status.js +52 -63
  71. package/lib/plugins/child/typescript.js +19 -36
  72. package/lib/plugins/child/utils.js +36 -49
  73. package/lib/plugins/child/validate.js +25 -28
  74. package/lib/plugins/compatibility.js +64 -92
  75. package/lib/plugins/error.js +29 -35
  76. package/lib/plugins/events.js +7 -12
  77. package/lib/plugins/expected_version.js +61 -99
  78. package/lib/plugins/ipc.js +79 -102
  79. package/lib/plugins/list.js +49 -62
  80. package/lib/plugins/load.js +44 -64
  81. package/lib/plugins/manifest/check.js +64 -85
  82. package/lib/plugins/manifest/load.js +34 -37
  83. package/lib/plugins/manifest/main.js +16 -21
  84. package/lib/plugins/manifest/path.js +18 -25
  85. package/lib/plugins/manifest/validate.js +77 -94
  86. package/lib/plugins/node_version.js +22 -42
  87. package/lib/plugins/options.js +45 -78
  88. package/lib/plugins/pinned_version.js +58 -106
  89. package/lib/plugins/resolve.js +91 -133
  90. package/lib/plugins/spawn.js +43 -61
  91. package/lib/plugins_core/add.js +26 -40
  92. package/lib/plugins_core/build_command.js +47 -72
  93. package/lib/plugins_core/deploy/buildbot_client.js +61 -87
  94. package/lib/plugins_core/deploy/index.js +47 -71
  95. package/lib/plugins_core/edge_functions/index.js +73 -116
  96. package/lib/plugins_core/edge_functions/lib/error.js +13 -17
  97. package/lib/plugins_core/edge_functions/lib/internal_manifest.js +45 -55
  98. package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +65 -80
  99. package/lib/plugins_core/functions/error.js +88 -128
  100. package/lib/plugins_core/functions/feature_flags.js +5 -5
  101. package/lib/plugins_core/functions/index.js +98 -145
  102. package/lib/plugins_core/functions/utils.js +36 -57
  103. package/lib/plugins_core/functions/zisi.js +35 -52
  104. package/lib/plugins_core/functions_install/index.js +8 -11
  105. package/lib/plugins_core/list.js +15 -22
  106. package/lib/status/add.js +26 -32
  107. package/lib/status/colors.js +14 -19
  108. package/lib/status/load_error.js +8 -9
  109. package/lib/status/report.js +72 -126
  110. package/lib/status/success.js +10 -14
  111. package/lib/steps/core_step.js +54 -89
  112. package/lib/steps/error.js +50 -87
  113. package/lib/steps/get.js +33 -41
  114. package/lib/steps/plugin.js +53 -83
  115. package/lib/steps/return.js +24 -51
  116. package/lib/steps/run_core_steps.js +119 -171
  117. package/lib/steps/run_step.js +154 -270
  118. package/lib/steps/run_steps.js +91 -174
  119. package/lib/steps/update_config.js +45 -72
  120. package/lib/telemetry/main.js +89 -128
  121. package/lib/time/aggregate.js +84 -110
  122. package/lib/time/main.js +23 -34
  123. package/lib/time/measure.js +11 -15
  124. package/lib/time/report.js +32 -44
  125. package/lib/utils/errors.js +10 -9
  126. package/lib/utils/json.js +11 -15
  127. package/lib/utils/omit.js +3 -4
  128. package/lib/utils/package.js +19 -22
  129. package/lib/utils/remove_falsy.js +5 -7
  130. package/lib/utils/resolve.js +30 -34
  131. package/lib/utils/runtime.js +4 -4
  132. package/lib/utils/semver.js +19 -25
  133. package/package.json +14 -7
@@ -1,75 +1,50 @@
1
- import { platform } from 'process'
2
-
3
- import { execa } from 'execa'
4
-
5
- import { addErrorInfo } from '../error/info.js'
6
- import { getBuildCommandDescription } from '../log/description.js'
7
- import { logBuildCommandStart } from '../log/messages/steps.js'
8
- import { getBuildCommandStdio, handleBuildCommandOutput } from '../log/stream.js'
9
-
1
+ import { platform } from 'process';
2
+ import { execa } from 'execa';
3
+ import { addErrorInfo } from '../error/info.js';
4
+ import { getBuildCommandDescription } from '../log/description.js';
5
+ import { logBuildCommandStart } from '../log/messages/steps.js';
6
+ import { getBuildCommandStdio, handleBuildCommandOutput } from '../log/stream.js';
10
7
  // Fire `build.command`
11
- const coreStep = async function ({
12
- configPath,
13
- buildDir,
14
- nodePath,
15
- childEnv,
16
- logs,
17
- netlifyConfig: {
18
- build: { command: buildCommand, commandOrigin: buildCommandOrigin },
19
- },
20
- }) {
21
- logBuildCommandStart(logs, buildCommand)
22
-
23
- const stdio = getBuildCommandStdio(logs)
24
- const childProcess = execa(buildCommand, {
25
- shell: SHELL,
26
- cwd: buildDir,
27
- preferLocal: true,
28
- execPath: nodePath,
29
- env: childEnv,
30
- extendEnv: false,
31
- stdio,
32
- })
33
-
34
- try {
35
- const buildCommandOutput = await childProcess
36
- handleBuildCommandOutput(buildCommandOutput, logs)
37
- return {}
38
- } catch (error) {
39
- // In our test environment we use `stdio: 'pipe'` on the build command, meaning our `stdout/stderr` output are
40
- // buffered and consequently added to `error.message`. To avoid this and end up with duplicated output in our
41
- // logs/snapshots we need to rely on `error.shortMessage`.
42
- error.message = error.shortMessage
43
- handleBuildCommandOutput(error, logs)
44
- addErrorInfo(error, { type: 'buildCommand', location: { buildCommand, buildCommandOrigin, configPath } })
45
- throw error
46
- }
47
- }
48
-
8
+ const coreStep = async function ({ configPath, buildDir, nodePath, childEnv, logs, netlifyConfig: { build: { command: buildCommand, commandOrigin: buildCommandOrigin }, }, }) {
9
+ logBuildCommandStart(logs, buildCommand);
10
+ const stdio = getBuildCommandStdio(logs);
11
+ const childProcess = execa(buildCommand, {
12
+ shell: SHELL,
13
+ cwd: buildDir,
14
+ preferLocal: true,
15
+ execPath: nodePath,
16
+ env: childEnv,
17
+ extendEnv: false,
18
+ stdio,
19
+ });
20
+ try {
21
+ const buildCommandOutput = await childProcess;
22
+ handleBuildCommandOutput(buildCommandOutput, logs);
23
+ return {};
24
+ }
25
+ catch (error) {
26
+ // In our test environment we use `stdio: 'pipe'` on the build command, meaning our `stdout/stderr` output are
27
+ // buffered and consequently added to `error.message`. To avoid this and end up with duplicated output in our
28
+ // logs/snapshots we need to rely on `error.shortMessage`.
29
+ error.message = error.shortMessage;
30
+ handleBuildCommandOutput(error, logs);
31
+ addErrorInfo(error, { type: 'buildCommand', location: { buildCommand, buildCommandOrigin, configPath } });
32
+ throw error;
33
+ }
34
+ };
49
35
  // We use Bash on Unix and `cmd.exe` on Windows
50
- const SHELL = platform === 'win32' ? true : 'bash'
51
-
52
- const coreStepDescription = function ({
53
- netlifyConfig: {
54
- build: { commandOrigin: buildCommandOrigin },
55
- },
56
- }) {
57
- return getBuildCommandDescription(buildCommandOrigin)
58
- }
59
-
60
- const hasBuildCommand = function ({
61
- netlifyConfig: {
62
- build: { command: buildCommand },
63
- },
64
- }) {
65
- return buildCommand !== undefined && buildCommand !== ''
66
- }
67
-
36
+ const SHELL = platform === 'win32' ? true : 'bash';
37
+ const coreStepDescription = function ({ netlifyConfig: { build: { commandOrigin: buildCommandOrigin }, }, }) {
38
+ return getBuildCommandDescription(buildCommandOrigin);
39
+ };
40
+ const hasBuildCommand = function ({ netlifyConfig: { build: { command: buildCommand }, }, }) {
41
+ return buildCommand !== undefined && buildCommand !== '';
42
+ };
68
43
  export const buildCommandCore = {
69
- event: 'onBuild',
70
- coreStep,
71
- coreStepId: 'build_command',
72
- coreStepName: 'build.command',
73
- coreStepDescription,
74
- condition: hasBuildCommand,
75
- }
44
+ event: 'onBuild',
45
+ coreStep,
46
+ coreStepId: 'build_command',
47
+ coreStepName: 'build.command',
48
+ coreStepDescription,
49
+ condition: hasBuildCommand,
50
+ };
@@ -1,74 +1,54 @@
1
- import net from 'net'
2
- import { normalize, resolve, relative } from 'path'
3
- import { promisify } from 'util'
4
-
5
- import { pEvent } from 'p-event'
6
-
7
- import { addErrorInfo } from '../../error/info.js'
8
- import { runsAfterDeploy } from '../../plugins/events.js'
9
- import { addAsyncErrorMessage } from '../../utils/errors.js'
10
-
1
+ import net from 'net';
2
+ import { normalize, resolve, relative } from 'path';
3
+ import { promisify } from 'util';
4
+ import { pEvent } from 'p-event';
5
+ import { addErrorInfo } from '../../error/info.js';
6
+ import { runsAfterDeploy } from '../../plugins/events.js';
7
+ import { addAsyncErrorMessage } from '../../utils/errors.js';
11
8
  export const createBuildbotClient = function (buildbotServerSocket) {
12
- const connectionOpts = getConnectionOpts(buildbotServerSocket)
13
- const client = net.createConnection(connectionOpts)
14
- return client
15
- }
16
-
9
+ const connectionOpts = getConnectionOpts(buildbotServerSocket);
10
+ const client = net.createConnection(connectionOpts);
11
+ return client;
12
+ };
17
13
  // Windows does not support Unix sockets well, so we also support `host:port`
18
14
  const getConnectionOpts = function (buildbotServerSocket) {
19
- if (!buildbotServerSocket.includes(':')) {
20
- return { path: buildbotServerSocket }
21
- }
22
-
23
- const [host, port] = buildbotServerSocket.split(':')
24
- return { host, port }
25
- }
26
-
15
+ if (!buildbotServerSocket.includes(':')) {
16
+ return { path: buildbotServerSocket };
17
+ }
18
+ const [host, port] = buildbotServerSocket.split(':');
19
+ return { host, port };
20
+ };
27
21
  const eConnectBuildbotClient = async function (client) {
28
- await pEvent(client, 'connect')
29
- }
30
-
31
- export const connectBuildbotClient = addAsyncErrorMessage(eConnectBuildbotClient, 'Could not connect to buildbot')
32
-
22
+ await pEvent(client, 'connect');
23
+ };
24
+ export const connectBuildbotClient = addAsyncErrorMessage(eConnectBuildbotClient, 'Could not connect to buildbot');
33
25
  export const closeBuildbotClient = async function (client) {
34
- if (client.destroyed) {
35
- return
36
- }
37
-
38
- await promisify(client.end.bind(client))()
39
- }
40
-
26
+ if (client.destroyed) {
27
+ return;
28
+ }
29
+ await promisify(client.end.bind(client))();
30
+ };
41
31
  const cWritePayload = async function (buildbotClient, payload) {
42
- await promisify(buildbotClient.write.bind(buildbotClient))(JSON.stringify(payload))
43
- }
44
-
45
- const writePayload = addAsyncErrorMessage(cWritePayload, 'Could not send payload to buildbot')
46
-
32
+ await promisify(buildbotClient.write.bind(buildbotClient))(JSON.stringify(payload));
33
+ };
34
+ const writePayload = addAsyncErrorMessage(cWritePayload, 'Could not send payload to buildbot');
47
35
  const cGetNextParsedResponsePromise = async function (buildbotClient) {
48
- const data = await pEvent(buildbotClient, 'data')
49
- return JSON.parse(data)
50
- }
51
-
52
- const getNextParsedResponsePromise = addAsyncErrorMessage(
53
- cGetNextParsedResponsePromise,
54
- 'Invalid response from buildbot',
55
- )
56
-
36
+ const data = await pEvent(buildbotClient, 'data');
37
+ return JSON.parse(data);
38
+ };
39
+ const getNextParsedResponsePromise = addAsyncErrorMessage(cGetNextParsedResponsePromise, 'Invalid response from buildbot');
57
40
  export const deploySiteWithBuildbotClient = async function ({ client, events, buildDir, repositoryRoot, constants }) {
58
- const action = shouldWaitForPostProcessing(events) ? 'deploySiteAndAwaitLive' : 'deploySite'
59
- const deployDir = getDeployDir({ buildDir, repositoryRoot, constants })
60
- const payload = { action, deployDir }
61
-
62
- const [{ succeeded, values: { error, error_type: errorType } = {} }] = await Promise.all([
63
- getNextParsedResponsePromise(client),
64
- writePayload(client, payload),
65
- ])
66
-
67
- if (!succeeded) {
68
- return handleDeployError(error, errorType)
69
- }
70
- }
71
-
41
+ const action = shouldWaitForPostProcessing(events) ? 'deploySiteAndAwaitLive' : 'deploySite';
42
+ const deployDir = getDeployDir({ buildDir, repositoryRoot, constants });
43
+ const payload = { action, deployDir };
44
+ const [{ succeeded, values: { error, error_type: errorType } = {} }] = await Promise.all([
45
+ getNextParsedResponsePromise(client),
46
+ writePayload(client, payload),
47
+ ]);
48
+ if (!succeeded) {
49
+ return handleDeployError(error, errorType);
50
+ }
51
+ };
72
52
  // The file paths in the buildbot are relative to the repository root.
73
53
  // However, the file paths in Build plugins, including `constants.PUBLISH_DIR`
74
54
  // are relative to the build directory, which is different when there is a
@@ -76,38 +56,32 @@ export const deploySiteWithBuildbotClient = async function ({ client, events, bu
76
56
  // We need to call `normalize()` in case the publish directory is the
77
57
  // repository root, so `deployDir` is "." not ""
78
58
  const getDeployDir = function ({ buildDir, repositoryRoot, constants: { PUBLISH_DIR } }) {
79
- const absolutePublishDir = resolve(buildDir, PUBLISH_DIR)
80
- const relativePublishDir = relative(repositoryRoot, absolutePublishDir)
81
- const deployDir = normalize(relativePublishDir)
82
- return deployDir
83
- }
84
-
59
+ const absolutePublishDir = resolve(buildDir, PUBLISH_DIR);
60
+ const relativePublishDir = relative(repositoryRoot, absolutePublishDir);
61
+ const deployDir = normalize(relativePublishDir);
62
+ return deployDir;
63
+ };
85
64
  // We distinguish between user errors and system errors during deploys
86
65
  const handleDeployError = function (error, errorType) {
87
- const errorIs422 = error !== undefined && error.code === '422'
88
-
89
- const errMsg = errorIs422
90
- ? `
66
+ const errorIs422 = error !== undefined && error.code === '422';
67
+ const errMsg = errorIs422
68
+ ? `
91
69
  File upload failed because of mismatched SHAs.
92
70
  This can happen when files are changed after the deployment process has started but before they are uploaded.
93
71
 
94
72
  Error: ${error}
95
73
  `
96
- : `Deploy did not succeed: ${error}`
97
-
98
- const errorA = new Error(errMsg)
99
- const errorInfo =
100
- errorType === 'user' ? { type: 'resolveConfig' } : { type: 'coreStep', location: { coreStepName: 'Deploy site' } }
101
- addErrorInfo(errorA, errorInfo)
102
- throw errorA
103
- }
104
-
74
+ : `Deploy did not succeed: ${error}`;
75
+ const errorA = new Error(errMsg);
76
+ const errorInfo = errorType === 'user' ? { type: 'resolveConfig' } : { type: 'coreStep', location: { coreStepName: 'Deploy site' } };
77
+ addErrorInfo(errorA, errorInfo);
78
+ throw errorA;
79
+ };
105
80
  // We only wait for post-processing (last stage before site deploy) if the build
106
81
  // has some plugins that do post-deploy logic
107
82
  const shouldWaitForPostProcessing = function (events) {
108
- return events.some(hasPostDeployLogic)
109
- }
110
-
83
+ return events.some(hasPostDeployLogic);
84
+ };
111
85
  const hasPostDeployLogic = function (event) {
112
- return runsAfterDeploy(event)
113
- }
86
+ return runsAfterDeploy(event);
87
+ };
@@ -1,73 +1,49 @@
1
- import { saveUpdatedConfig, restoreUpdatedConfig } from '../../core/config.js'
2
- import { logDeploySuccess } from '../../log/messages/plugins.js'
3
-
4
- import {
5
- createBuildbotClient,
6
- connectBuildbotClient,
7
- closeBuildbotClient,
8
- deploySiteWithBuildbotClient,
9
- } from './buildbot_client.js'
10
-
11
- const coreStep = async function ({
12
- buildDir,
13
- configPath,
14
- repositoryRoot,
15
- constants,
16
- buildbotServerSocket,
17
- events,
18
- logs,
19
- featureFlags,
20
- context,
21
- branch,
22
- configMutations,
23
- headersPath,
24
- redirectsPath,
25
- debug,
26
- saveConfig,
27
- }) {
28
- const client = createBuildbotClient(buildbotServerSocket)
29
- try {
30
- await connectBuildbotClient(client)
31
- await saveUpdatedConfig({
32
- configMutations,
33
- buildDir,
34
- repositoryRoot,
35
- configPath,
36
- headersPath,
37
- redirectsPath,
38
- logs,
39
- featureFlags,
40
- context,
41
- branch,
42
- debug,
43
- saveConfig,
44
- })
45
- await deploySiteWithBuildbotClient({ client, events, buildDir, repositoryRoot, constants })
46
- await restoreUpdatedConfig({
47
- configMutations,
48
- buildDir,
49
- repositoryRoot,
50
- configPath,
51
- headersPath,
52
- redirectsPath,
53
- saveConfig,
54
- })
55
- logDeploySuccess(logs)
56
- return {}
57
- } finally {
58
- await closeBuildbotClient(client)
59
- }
60
- }
61
-
1
+ import { saveUpdatedConfig, restoreUpdatedConfig } from '../../core/config.js';
2
+ import { logDeploySuccess } from '../../log/messages/plugins.js';
3
+ import { createBuildbotClient, connectBuildbotClient, closeBuildbotClient, deploySiteWithBuildbotClient, } from './buildbot_client.js';
4
+ const coreStep = async function ({ buildDir, configPath, repositoryRoot, constants, buildbotServerSocket, events, logs, featureFlags, context, branch, configMutations, headersPath, redirectsPath, debug, saveConfig, }) {
5
+ const client = createBuildbotClient(buildbotServerSocket);
6
+ try {
7
+ await connectBuildbotClient(client);
8
+ await saveUpdatedConfig({
9
+ configMutations,
10
+ buildDir,
11
+ repositoryRoot,
12
+ configPath,
13
+ headersPath,
14
+ redirectsPath,
15
+ logs,
16
+ featureFlags,
17
+ context,
18
+ branch,
19
+ debug,
20
+ saveConfig,
21
+ });
22
+ await deploySiteWithBuildbotClient({ client, events, buildDir, repositoryRoot, constants });
23
+ await restoreUpdatedConfig({
24
+ configMutations,
25
+ buildDir,
26
+ repositoryRoot,
27
+ configPath,
28
+ headersPath,
29
+ redirectsPath,
30
+ saveConfig,
31
+ });
32
+ logDeploySuccess(logs);
33
+ return {};
34
+ }
35
+ finally {
36
+ await closeBuildbotClient(client);
37
+ }
38
+ };
62
39
  const shouldDeploy = function ({ buildbotServerSocket }) {
63
- return buildbotServerSocket !== undefined
64
- }
65
-
40
+ return buildbotServerSocket !== undefined;
41
+ };
66
42
  export const deploySite = {
67
- event: 'onPostBuild',
68
- coreStep,
69
- coreStepId: 'deploy_site',
70
- coreStepName: 'Deploy site',
71
- coreStepDescription: () => 'Deploy site',
72
- condition: shouldDeploy,
73
- }
43
+ event: 'onPostBuild',
44
+ coreStep,
45
+ coreStepId: 'deploy_site',
46
+ coreStepName: 'Deploy site',
47
+ coreStepDescription: () => 'Deploy site',
48
+ condition: shouldDeploy,
49
+ };
@@ -1,122 +1,79 @@
1
- import { promises as fs } from 'fs'
2
- import { dirname, join, resolve } from 'path'
3
-
4
- import { bundle, find } from '@netlify/edge-bundler'
5
- import { pathExists } from 'path-exists'
6
-
7
- import { logFunctionsToBundle } from '../../log/messages/core_steps.js'
8
-
9
- import { tagBundlingError } from './lib/error.js'
10
- import { parseManifest } from './lib/internal_manifest.js'
11
- import { validateEdgeFunctionsManifest } from './validate_manifest/validate_edge_functions_manifest.js'
12
-
1
+ import { promises as fs } from 'fs';
2
+ import { dirname, join, resolve } from 'path';
3
+ import { bundle, find } from '@netlify/edge-bundler';
4
+ import { pathExists } from 'path-exists';
5
+ import { logFunctionsToBundle } from '../../log/messages/core_steps.js';
6
+ import { tagBundlingError } from './lib/error.js';
7
+ import { parseManifest } from './lib/internal_manifest.js';
8
+ import { validateEdgeFunctionsManifest } from './validate_manifest/validate_edge_functions_manifest.js';
13
9
  // TODO: Replace this with a custom cache directory.
14
- const DENO_CLI_CACHE_DIRECTORY = '.netlify/plugins/deno-cli'
15
- const IMPORT_MAP_FILENAME = 'edge-functions-import-map.json'
16
-
17
- const coreStep = async function ({
18
- buildDir,
19
- constants: {
20
- EDGE_FUNCTIONS_DIST: distDirectory,
21
- EDGE_FUNCTIONS_SRC: srcDirectory,
22
- INTERNAL_EDGE_FUNCTIONS_SRC: internalSrcDirectory,
23
- IS_LOCAL: isRunningLocally,
24
- },
25
- debug,
26
- systemLog,
27
- featureFlags,
28
- logs,
29
- netlifyConfig,
30
- }) {
31
- const { edge_functions: configDeclarations = [] } = netlifyConfig
32
- const distPath = resolve(buildDir, distDirectory)
33
- const internalSrcPath = resolve(buildDir, internalSrcDirectory)
34
- const distImportMapPath = join(dirname(internalSrcPath), IMPORT_MAP_FILENAME)
35
- const srcPath = srcDirectory ? resolve(buildDir, srcDirectory) : undefined
36
- const sourcePaths = [internalSrcPath, srcPath].filter(Boolean)
37
-
38
- logFunctions({ internalSrcDirectory, internalSrcPath, logs, srcDirectory, srcPath })
39
-
40
- const { declarations: internalDeclarations, importMap } = await parseManifest(internalSrcPath, systemLog)
41
- const declarations = [...configDeclarations, ...internalDeclarations]
42
-
43
- // If we're running in buildbot and the feature flag is enabled, we set the
44
- // Deno cache dir to a directory that is persisted between builds.
45
- const cacheDirectory =
46
- !isRunningLocally && featureFlags.edge_functions_cache_cli ? resolve(buildDir, DENO_CLI_CACHE_DIRECTORY) : undefined
47
-
48
- // Edge Bundler expects the dist directory to exist.
49
- await fs.mkdir(distPath, { recursive: true })
50
-
51
- try {
52
- const { manifest } = await bundle(sourcePaths, distPath, declarations, {
53
- basePath: buildDir,
54
- cacheDirectory,
55
- debug,
56
- distImportMapPath,
57
- featureFlags,
58
- importMaps: [importMap].filter(Boolean),
59
- systemLogger: featureFlags.edge_functions_system_logger ? systemLog : undefined,
60
- })
61
-
62
- systemLog('Edge Functions manifest:', manifest)
63
- } catch (error) {
64
- tagBundlingError(error)
65
-
66
- throw error
67
- }
68
-
69
- await validateEdgeFunctionsManifest({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory } })
70
-
71
- return {}
72
- }
73
-
10
+ const DENO_CLI_CACHE_DIRECTORY = '.netlify/plugins/deno-cli';
11
+ const IMPORT_MAP_FILENAME = 'edge-functions-import-map.json';
12
+ const coreStep = async function ({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory, EDGE_FUNCTIONS_SRC: srcDirectory, INTERNAL_EDGE_FUNCTIONS_SRC: internalSrcDirectory, IS_LOCAL: isRunningLocally, }, debug, systemLog, featureFlags, logs, netlifyConfig, }) {
13
+ const { edge_functions: configDeclarations = [] } = netlifyConfig;
14
+ const distPath = resolve(buildDir, distDirectory);
15
+ const internalSrcPath = resolve(buildDir, internalSrcDirectory);
16
+ const distImportMapPath = join(dirname(internalSrcPath), IMPORT_MAP_FILENAME);
17
+ const srcPath = srcDirectory ? resolve(buildDir, srcDirectory) : undefined;
18
+ const sourcePaths = [internalSrcPath, srcPath].filter(Boolean);
19
+ logFunctions({ internalSrcDirectory, internalSrcPath, logs, srcDirectory, srcPath });
20
+ const { declarations: internalDeclarations, importMap } = await parseManifest(internalSrcPath, systemLog);
21
+ const declarations = [...configDeclarations, ...internalDeclarations];
22
+ // If we're running in buildbot and the feature flag is enabled, we set the
23
+ // Deno cache dir to a directory that is persisted between builds.
24
+ const cacheDirectory = !isRunningLocally && featureFlags.edge_functions_cache_cli ? resolve(buildDir, DENO_CLI_CACHE_DIRECTORY) : undefined;
25
+ // Edge Bundler expects the dist directory to exist.
26
+ await fs.mkdir(distPath, { recursive: true });
27
+ try {
28
+ const { manifest } = await bundle(sourcePaths, distPath, declarations, {
29
+ basePath: buildDir,
30
+ cacheDirectory,
31
+ debug,
32
+ distImportMapPath,
33
+ featureFlags,
34
+ importMaps: [importMap].filter(Boolean),
35
+ systemLogger: featureFlags.edge_functions_system_logger ? systemLog : undefined,
36
+ });
37
+ systemLog('Edge Functions manifest:', manifest);
38
+ }
39
+ catch (error) {
40
+ tagBundlingError(error);
41
+ throw error;
42
+ }
43
+ await validateEdgeFunctionsManifest({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory } });
44
+ return {};
45
+ };
74
46
  // We run this core step if at least one of the functions directories (the
75
47
  // one configured by the user or the internal one) exists. We use a dynamic
76
48
  // `condition` because the directories might be created by the build command
77
49
  // or plugins.
78
- const hasEdgeFunctionsDirectories = async function ({
79
- buildDir,
80
- constants: { INTERNAL_EDGE_FUNCTIONS_SRC, EDGE_FUNCTIONS_SRC },
81
- }) {
82
- const hasFunctionsSrc = EDGE_FUNCTIONS_SRC !== undefined && EDGE_FUNCTIONS_SRC !== ''
83
-
84
- if (hasFunctionsSrc) {
85
- return true
86
- }
87
-
88
- const internalFunctionsSrc = resolve(buildDir, INTERNAL_EDGE_FUNCTIONS_SRC)
89
-
90
- return await pathExists(internalFunctionsSrc)
91
- }
92
-
93
- const logFunctions = async ({
94
- internalSrcDirectory,
95
- internalSrcPath,
96
- logs,
97
- srcDirectory: userFunctionsSrc,
98
- srcPath,
99
- }) => {
100
- const [userFunctions, internalFunctions] = await Promise.all([find([srcPath]), find([internalSrcPath])])
101
- const userFunctionsSrcExists = await pathExists(srcPath)
102
- const internalFunctionsSrc = internalSrcDirectory
103
-
104
- logFunctionsToBundle({
105
- logs,
106
- userFunctions: userFunctions.map(({ name }) => name),
107
- userFunctionsSrc,
108
- userFunctionsSrcExists,
109
- internalFunctions: internalFunctions.map(({ name }) => name),
110
- internalFunctionsSrc,
111
- type: 'Edge Functions',
112
- })
113
- }
114
-
50
+ const hasEdgeFunctionsDirectories = async function ({ buildDir, constants: { INTERNAL_EDGE_FUNCTIONS_SRC, EDGE_FUNCTIONS_SRC }, }) {
51
+ const hasFunctionsSrc = EDGE_FUNCTIONS_SRC !== undefined && EDGE_FUNCTIONS_SRC !== '';
52
+ if (hasFunctionsSrc) {
53
+ return true;
54
+ }
55
+ const internalFunctionsSrc = resolve(buildDir, INTERNAL_EDGE_FUNCTIONS_SRC);
56
+ return await pathExists(internalFunctionsSrc);
57
+ };
58
+ const logFunctions = async ({ internalSrcDirectory, internalSrcPath, logs, srcDirectory: userFunctionsSrc, srcPath, }) => {
59
+ const [userFunctions, internalFunctions] = await Promise.all([find([srcPath]), find([internalSrcPath])]);
60
+ const userFunctionsSrcExists = await pathExists(srcPath);
61
+ const internalFunctionsSrc = internalSrcDirectory;
62
+ logFunctionsToBundle({
63
+ logs,
64
+ userFunctions: userFunctions.map(({ name }) => name),
65
+ userFunctionsSrc,
66
+ userFunctionsSrcExists,
67
+ internalFunctions: internalFunctions.map(({ name }) => name),
68
+ internalFunctionsSrc,
69
+ type: 'Edge Functions',
70
+ });
71
+ };
115
72
  export const bundleEdgeFunctions = {
116
- event: 'onBuild',
117
- coreStep,
118
- coreStepId: 'edge_functions_bundling',
119
- coreStepName: 'Edge Functions bundling',
120
- coreStepDescription: () => 'Edge Functions bundling',
121
- condition: hasEdgeFunctionsDirectories,
122
- }
73
+ event: 'onBuild',
74
+ coreStep,
75
+ coreStepId: 'edge_functions_bundling',
76
+ coreStepName: 'Edge Functions bundling',
77
+ coreStepDescription: () => 'Edge Functions bundling',
78
+ condition: hasEdgeFunctionsDirectories,
79
+ };
@@ -1,21 +1,17 @@
1
- import { CUSTOM_ERROR_KEY, getErrorInfo, isBuildError } from '../../../error/info.js'
2
-
1
+ import { CUSTOM_ERROR_KEY, getErrorInfo, isBuildError } from '../../../error/info.js';
3
2
  // If we have a custom error tagged with `functionsBundling` (which happens if
4
3
  // there is an issue with user code), we tag it as coming from an edge function
5
4
  // so that we can adjust the downstream error messages accordingly.
6
5
  export const tagBundlingError = (error) => {
7
- if (!isBuildError(error)) {
8
- return
9
- }
10
-
11
- const [errorInfo = {}] = getErrorInfo(error)
12
-
13
- if (errorInfo.type !== 'functionsBundling') {
14
- return
15
- }
16
-
17
- error[CUSTOM_ERROR_KEY].location = {
18
- ...error[CUSTOM_ERROR_KEY].location,
19
- functionType: 'edge',
20
- }
21
- }
6
+ if (!isBuildError(error)) {
7
+ return;
8
+ }
9
+ const [errorInfo = {}] = getErrorInfo(error);
10
+ if (errorInfo.type !== 'functionsBundling') {
11
+ return;
12
+ }
13
+ error[CUSTOM_ERROR_KEY].location = {
14
+ ...error[CUSTOM_ERROR_KEY].location,
15
+ functionType: 'edge',
16
+ };
17
+ };