@netlify/build 29.0.1-rc → 29.0.2
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.
- package/bin.js +5 -0
- package/lib/core/bin.js +66 -0
- package/lib/core/build.js +356 -0
- package/lib/core/config.js +121 -0
- package/lib/core/constants.js +116 -0
- package/lib/core/dev.js +27 -0
- package/lib/core/dry.js +21 -0
- package/lib/core/feature_flags.js +21 -0
- package/lib/core/flags.js +201 -0
- package/lib/core/lingering.js +68 -0
- package/lib/core/main.js +110 -0
- package/lib/core/missing_side_file.js +17 -0
- package/lib/core/normalize_flags.js +59 -0
- package/lib/core/severity.js +21 -0
- package/lib/core/types.js +8 -0
- package/lib/core/user_node_version.js +32 -0
- package/lib/env/changes.js +43 -0
- package/lib/env/main.js +14 -0
- package/lib/env/metadata.js +68 -0
- package/lib/error/api.js +37 -0
- package/lib/error/build.js +36 -0
- package/{src → lib}/error/cancel.js +5 -6
- package/lib/error/colors.js +9 -0
- package/lib/error/handle.js +46 -0
- package/lib/error/info.js +37 -0
- package/lib/error/monitor/location.js +16 -0
- package/lib/error/monitor/normalize.js +86 -0
- package/lib/error/monitor/print.js +20 -0
- package/lib/error/monitor/report.js +120 -0
- package/lib/error/monitor/start.js +61 -0
- package/lib/error/parse/clean_stack.js +70 -0
- package/lib/error/parse/location.js +50 -0
- package/lib/error/parse/normalize.js +24 -0
- package/lib/error/parse/parse.js +67 -0
- package/lib/error/parse/plugin.js +55 -0
- package/lib/error/parse/properties.js +16 -0
- package/lib/error/parse/serialize_log.js +34 -0
- package/lib/error/parse/serialize_status.js +18 -0
- package/lib/error/parse/stack.js +34 -0
- package/lib/error/report.js +27 -0
- package/lib/error/type.js +177 -0
- package/lib/install/functions.js +20 -0
- package/lib/install/local.js +45 -0
- package/lib/install/main.js +67 -0
- package/lib/install/missing.js +54 -0
- package/lib/log/colors.js +28 -0
- package/lib/log/description.js +21 -0
- package/lib/log/header.js +14 -0
- package/lib/log/header_func.js +13 -0
- package/lib/log/logger.js +140 -0
- package/lib/log/messages/compatibility.js +146 -0
- package/lib/log/messages/config.js +91 -0
- package/lib/log/messages/core.js +51 -0
- package/lib/log/messages/core_steps.js +75 -0
- package/lib/log/messages/dry.js +41 -0
- package/lib/log/messages/install.js +25 -0
- package/lib/log/messages/ipc.js +29 -0
- package/lib/log/messages/mutations.js +62 -0
- package/{src → lib}/log/messages/plugins.js +18 -32
- package/lib/log/messages/status.js +14 -0
- package/lib/log/messages/steps.js +18 -0
- package/lib/log/old_version.js +32 -0
- package/lib/log/serialize.js +10 -0
- package/lib/log/stream.js +68 -0
- package/lib/log/theme.js +27 -0
- package/lib/plugins/child/diff.js +46 -0
- package/lib/plugins/child/error.js +26 -0
- package/lib/plugins/child/lazy.js +15 -0
- package/lib/plugins/child/load.js +22 -0
- package/lib/plugins/child/logic.js +57 -0
- package/lib/plugins/child/main.js +37 -0
- package/lib/plugins/child/run.js +19 -0
- package/lib/plugins/child/status.js +63 -0
- package/lib/plugins/child/typescript.js +28 -0
- package/lib/plugins/child/utils.js +42 -0
- package/lib/plugins/child/validate.js +31 -0
- package/lib/plugins/compatibility.js +104 -0
- package/{src → lib}/plugins/error.js +31 -35
- package/{src → lib}/plugins/events.js +7 -12
- package/lib/plugins/expected_version.js +81 -0
- package/lib/plugins/internal.js +10 -0
- package/lib/plugins/ipc.js +120 -0
- package/lib/plugins/list.js +73 -0
- package/lib/plugins/load.js +50 -0
- package/lib/plugins/manifest/check.js +85 -0
- package/lib/plugins/manifest/load.js +38 -0
- package/lib/plugins/manifest/main.js +19 -0
- package/lib/plugins/manifest/path.js +24 -0
- package/lib/plugins/manifest/validate.js +91 -0
- package/lib/plugins/node_version.js +35 -0
- package/lib/plugins/options.js +70 -0
- package/lib/plugins/pinned_version.js +83 -0
- package/lib/plugins/resolve.js +110 -0
- package/lib/plugins/spawn.js +58 -0
- package/lib/plugins_core/add.js +35 -0
- package/lib/plugins_core/build_command.js +50 -0
- package/lib/plugins_core/deploy/buildbot_client.js +87 -0
- package/lib/plugins_core/deploy/index.js +49 -0
- package/{src → lib}/plugins_core/deploy/manifest.yml +0 -0
- package/lib/plugins_core/edge_functions/index.js +76 -0
- package/{src → lib}/plugins_core/edge_functions/lib/error.js +13 -17
- package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +21 -0
- package/lib/plugins_core/functions/error.js +123 -0
- package/lib/plugins_core/functions/feature_flags.js +6 -0
- package/lib/plugins_core/functions/index.js +114 -0
- package/lib/plugins_core/functions/utils.js +45 -0
- package/lib/plugins_core/functions/zisi.js +39 -0
- package/{src → lib}/plugins_core/functions_install/index.js +8 -11
- package/{src → lib}/plugins_core/functions_install/manifest.yml +0 -0
- package/lib/plugins_core/list.js +20 -0
- package/lib/report/statsd.js +31 -0
- package/lib/status/add.js +30 -0
- package/lib/status/colors.js +18 -0
- package/lib/status/load_error.js +10 -0
- package/lib/status/report.js +83 -0
- package/lib/status/success.js +14 -0
- package/lib/steps/core_step.js +59 -0
- package/lib/steps/error.js +65 -0
- package/lib/steps/get.js +43 -0
- package/lib/steps/plugin.js +55 -0
- package/lib/steps/return.js +25 -0
- package/lib/steps/run_core_steps.js +117 -0
- package/lib/steps/run_step.js +190 -0
- package/lib/steps/run_steps.js +96 -0
- package/lib/steps/update_config.js +66 -0
- package/lib/telemetry/main.js +97 -0
- package/lib/time/aggregate.js +109 -0
- package/lib/time/main.js +31 -0
- package/lib/time/measure.js +16 -0
- package/lib/time/report.js +24 -0
- package/lib/utils/errors.js +13 -0
- package/lib/utils/json.js +15 -0
- package/lib/utils/omit.js +3 -0
- package/lib/utils/package.js +24 -0
- package/lib/utils/remove_falsy.js +8 -0
- package/lib/utils/resolve.js +41 -0
- package/lib/utils/runtime.js +5 -0
- package/lib/utils/semver.js +28 -0
- package/package.json +41 -24
- package/types/config/netlify_config.d.ts +4 -4
- package/types/netlify_plugin_constants.d.ts +8 -8
- package/src/core/bin.js +0 -83
- package/src/core/build.js +0 -554
- package/src/core/config.js +0 -186
- package/src/core/constants.js +0 -156
- package/src/core/dev.js +0 -31
- package/src/core/dry.js +0 -39
- package/src/core/feature_flags.js +0 -22
- package/src/core/flags.js +0 -204
- package/src/core/lingering.js +0 -85
- package/src/core/main.js +0 -165
- package/src/core/missing_side_file.js +0 -29
- package/src/core/normalize_flags.js +0 -70
- package/src/core/severity.js +0 -22
- package/src/core/user_node_version.js +0 -41
- package/src/env/changes.js +0 -52
- package/src/env/main.js +0 -19
- package/src/env/metadata.js +0 -81
- package/src/error/api.js +0 -46
- package/src/error/build.js +0 -50
- package/src/error/colors.js +0 -11
- package/src/error/handle.js +0 -57
- package/src/error/info.js +0 -46
- package/src/error/monitor/location.js +0 -21
- package/src/error/monitor/normalize.js +0 -96
- package/src/error/monitor/print.js +0 -42
- package/src/error/monitor/report.js +0 -138
- package/src/error/monitor/start.js +0 -69
- package/src/error/parse/clean_stack.js +0 -87
- package/src/error/parse/location.js +0 -62
- package/src/error/parse/normalize.js +0 -29
- package/src/error/parse/parse.js +0 -97
- package/src/error/parse/plugin.js +0 -70
- package/src/error/parse/properties.js +0 -23
- package/src/error/parse/serialize_log.js +0 -42
- package/src/error/parse/serialize_status.js +0 -23
- package/src/error/parse/stack.js +0 -43
- package/src/error/type.js +0 -189
- package/src/install/functions.js +0 -28
- package/src/install/local.js +0 -62
- package/src/install/main.js +0 -81
- package/src/install/missing.js +0 -67
- package/src/log/colors.js +0 -34
- package/src/log/description.js +0 -26
- package/src/log/header.js +0 -16
- package/src/log/header_func.js +0 -17
- package/src/log/logger.js +0 -161
- package/src/log/messages/compatibility.js +0 -164
- package/src/log/messages/config.js +0 -107
- package/src/log/messages/core.js +0 -70
- package/src/log/messages/core_steps.js +0 -104
- package/src/log/messages/dry.js +0 -63
- package/src/log/messages/install.js +0 -20
- package/src/log/messages/ipc.js +0 -38
- package/src/log/messages/mutations.js +0 -82
- package/src/log/messages/status.js +0 -16
- package/src/log/messages/steps.js +0 -22
- package/src/log/old_version.js +0 -41
- package/src/log/serialize.js +0 -13
- package/src/log/stream.js +0 -85
- package/src/log/theme.js +0 -26
- package/src/plugins/child/diff.js +0 -55
- package/src/plugins/child/error.js +0 -32
- package/src/plugins/child/lazy.js +0 -18
- package/src/plugins/child/load.js +0 -29
- package/src/plugins/child/logic.js +0 -57
- package/src/plugins/child/main.js +0 -51
- package/src/plugins/child/run.js +0 -28
- package/src/plugins/child/status.js +0 -74
- package/src/plugins/child/typescript.js +0 -45
- package/src/plugins/child/utils.js +0 -56
- package/src/plugins/child/validate.js +0 -34
- package/src/plugins/compatibility.js +0 -128
- package/src/plugins/expected_version.js +0 -119
- package/src/plugins/ipc.js +0 -145
- package/src/plugins/list.js +0 -86
- package/src/plugins/load.js +0 -70
- package/src/plugins/manifest/check.js +0 -106
- package/src/plugins/manifest/load.js +0 -41
- package/src/plugins/manifest/main.js +0 -22
- package/src/plugins/manifest/path.js +0 -31
- package/src/plugins/manifest/validate.js +0 -108
- package/src/plugins/node_version.js +0 -50
- package/src/plugins/options.js +0 -88
- package/src/plugins/pinned_version.js +0 -131
- package/src/plugins/resolve.js +0 -152
- package/src/plugins/spawn.js +0 -66
- package/src/plugins_core/add.js +0 -49
- package/src/plugins_core/build_command.js +0 -75
- package/src/plugins_core/deploy/buildbot_client.js +0 -102
- package/src/plugins_core/deploy/index.js +0 -73
- package/src/plugins_core/edge_functions/index.js +0 -123
- package/src/plugins_core/edge_functions/lib/internal_manifest.js +0 -54
- package/src/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +0 -89
- package/src/plugins_core/functions/error.js +0 -163
- package/src/plugins_core/functions/feature_flags.js +0 -6
- package/src/plugins_core/functions/index.js +0 -160
- package/src/plugins_core/functions/utils.js +0 -66
- package/src/plugins_core/functions/zisi.js +0 -53
- package/src/plugins_core/list.js +0 -27
- package/src/status/add.js +0 -36
- package/src/status/colors.js +0 -23
- package/src/status/load_error.js +0 -11
- package/src/status/report.js +0 -137
- package/src/status/success.js +0 -18
- package/src/steps/core_step.js +0 -92
- package/src/steps/error.js +0 -102
- package/src/steps/get.js +0 -51
- package/src/steps/plugin.js +0 -85
- package/src/steps/return.js +0 -52
- package/src/steps/run_core_steps.js +0 -200
- package/src/steps/run_step.js +0 -304
- package/src/steps/run_steps.js +0 -179
- package/src/steps/update_config.js +0 -93
- package/src/telemetry/main.js +0 -136
- package/src/time/aggregate.js +0 -146
- package/src/time/main.js +0 -48
- package/src/time/measure.js +0 -22
- package/src/time/report.js +0 -59
- package/src/utils/errors.js +0 -12
- package/src/utils/json.js +0 -19
- package/src/utils/omit.js +0 -6
- package/src/utils/package.js +0 -23
- package/src/utils/remove_falsy.js +0 -10
- package/src/utils/resolve.js +0 -46
- package/src/utils/semver.js +0 -34
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { addErrorInfo } from '../error/info.js';
|
|
2
|
+
import { installMissingPlugins } from '../install/missing.js';
|
|
3
|
+
import { resolvePath, tryResolvePath } from '../utils/resolve.js';
|
|
4
|
+
import { addExpectedVersions } from './expected_version.js';
|
|
5
|
+
import { addPluginsNodeVersion } from './node_version.js';
|
|
6
|
+
import { addPinnedVersions } from './pinned_version.js';
|
|
7
|
+
// Try to find plugins in four places, by priority order:
|
|
8
|
+
// - already loaded (core plugins)
|
|
9
|
+
// - local plugin
|
|
10
|
+
// - external plugin already installed in `node_modules`, most likely through `package.json`
|
|
11
|
+
// - automatically installed by us, to `.netlify/plugins/`
|
|
12
|
+
export const resolvePluginsPath = async function ({ pluginsOptions, siteInfo, buildDir, nodePath, packageJson, userNodeVersion, mode, api, logs, debug, sendStatus, testOpts, featureFlags, }) {
|
|
13
|
+
const autoPluginsDir = getAutoPluginsDir(buildDir);
|
|
14
|
+
const pluginsOptionsA = await Promise.all(pluginsOptions.map((pluginOptions) => resolvePluginPath({ pluginOptions, buildDir, autoPluginsDir })));
|
|
15
|
+
const pluginsOptionsB = addPluginsNodeVersion({
|
|
16
|
+
pluginsOptions: pluginsOptionsA,
|
|
17
|
+
nodePath,
|
|
18
|
+
userNodeVersion,
|
|
19
|
+
logs,
|
|
20
|
+
});
|
|
21
|
+
const pluginsOptionsC = await addPinnedVersions({ pluginsOptions: pluginsOptionsB, api, siteInfo, sendStatus });
|
|
22
|
+
const pluginsOptionsD = await addExpectedVersions({
|
|
23
|
+
pluginsOptions: pluginsOptionsC,
|
|
24
|
+
autoPluginsDir,
|
|
25
|
+
packageJson,
|
|
26
|
+
debug,
|
|
27
|
+
logs,
|
|
28
|
+
buildDir,
|
|
29
|
+
testOpts,
|
|
30
|
+
featureFlags,
|
|
31
|
+
});
|
|
32
|
+
const pluginsOptionsE = await handleMissingPlugins({
|
|
33
|
+
pluginsOptions: pluginsOptionsD,
|
|
34
|
+
autoPluginsDir,
|
|
35
|
+
mode,
|
|
36
|
+
logs,
|
|
37
|
+
});
|
|
38
|
+
return pluginsOptionsE;
|
|
39
|
+
};
|
|
40
|
+
// Find the path to the directory used to install plugins automatically.
|
|
41
|
+
// It is a subdirectory of `buildDir`, so that the plugin can require the
|
|
42
|
+
// project's dependencies (peer dependencies).
|
|
43
|
+
const getAutoPluginsDir = function (buildDir) {
|
|
44
|
+
return `${buildDir}/${AUTO_PLUGINS_DIR}`;
|
|
45
|
+
};
|
|
46
|
+
const AUTO_PLUGINS_DIR = '.netlify/plugins/';
|
|
47
|
+
const resolvePluginPath = async function ({ pluginOptions, pluginOptions: { packageName, loadedFrom }, buildDir, autoPluginsDir, }) {
|
|
48
|
+
// Core plugins
|
|
49
|
+
if (loadedFrom !== undefined) {
|
|
50
|
+
return pluginOptions;
|
|
51
|
+
}
|
|
52
|
+
const localPackageName = normalizeLocalPackageName(packageName);
|
|
53
|
+
// Local plugins
|
|
54
|
+
if (localPackageName.startsWith('.')) {
|
|
55
|
+
const { path: localPath, error } = await tryResolvePath(localPackageName, buildDir);
|
|
56
|
+
validateLocalPluginPath(error, localPackageName);
|
|
57
|
+
return { ...pluginOptions, pluginPath: localPath, loadedFrom: 'local' };
|
|
58
|
+
}
|
|
59
|
+
// Plugin added to `package.json`
|
|
60
|
+
const { path: manualPath } = await tryResolvePath(packageName, buildDir);
|
|
61
|
+
if (manualPath !== undefined) {
|
|
62
|
+
return { ...pluginOptions, pluginPath: manualPath, loadedFrom: 'package.json' };
|
|
63
|
+
}
|
|
64
|
+
// Previously automatically installed
|
|
65
|
+
const { path: automaticPath } = await tryResolvePath(packageName, autoPluginsDir);
|
|
66
|
+
if (automaticPath !== undefined) {
|
|
67
|
+
return { ...pluginOptions, pluginPath: automaticPath, loadedFrom: 'auto_install' };
|
|
68
|
+
}
|
|
69
|
+
// Happens if the plugin:
|
|
70
|
+
// - name is mispelled
|
|
71
|
+
// - is not in our official list
|
|
72
|
+
// - is in our official list but has not been installed by this site yet
|
|
73
|
+
return { ...pluginOptions, loadedFrom: 'auto_install' };
|
|
74
|
+
};
|
|
75
|
+
// `packageName` starting with `/` are relative to the build directory
|
|
76
|
+
const normalizeLocalPackageName = function (packageName) {
|
|
77
|
+
if (packageName.startsWith('/')) {
|
|
78
|
+
return `.${packageName}`;
|
|
79
|
+
}
|
|
80
|
+
return packageName;
|
|
81
|
+
};
|
|
82
|
+
// When requiring a local plugin with an invalid file path
|
|
83
|
+
const validateLocalPluginPath = function (error, localPackageName) {
|
|
84
|
+
if (error !== undefined) {
|
|
85
|
+
error.message = `Plugin could not be found using local path: ${localPackageName}\n${error.message}`;
|
|
86
|
+
addErrorInfo(error, { type: 'resolveConfig' });
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
// Install plugins from the official list that have not been previously installed.
|
|
91
|
+
// Print a warning if they have not been installed through the UI.
|
|
92
|
+
const handleMissingPlugins = async function ({ pluginsOptions, autoPluginsDir, mode, logs }) {
|
|
93
|
+
const missingPlugins = pluginsOptions.filter(isMissingPlugin);
|
|
94
|
+
if (missingPlugins.length === 0) {
|
|
95
|
+
return pluginsOptions;
|
|
96
|
+
}
|
|
97
|
+
await installMissingPlugins({ missingPlugins, autoPluginsDir, mode, logs });
|
|
98
|
+
return await Promise.all(pluginsOptions.map((pluginOptions) => resolveMissingPluginPath({ pluginOptions, autoPluginsDir })));
|
|
99
|
+
};
|
|
100
|
+
// Resolve the plugins that just got automatically installed
|
|
101
|
+
const resolveMissingPluginPath = async function ({ pluginOptions, pluginOptions: { packageName }, autoPluginsDir }) {
|
|
102
|
+
if (!isMissingPlugin(pluginOptions)) {
|
|
103
|
+
return pluginOptions;
|
|
104
|
+
}
|
|
105
|
+
const pluginPath = await resolvePath(packageName, autoPluginsDir);
|
|
106
|
+
return { ...pluginOptions, pluginPath };
|
|
107
|
+
};
|
|
108
|
+
const isMissingPlugin = function ({ isMissing }) {
|
|
109
|
+
return isMissing;
|
|
110
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { fileURLToPath } from 'url';
|
|
2
|
+
import { execaNode } from 'execa';
|
|
3
|
+
import { addErrorInfo } from '../error/info.js';
|
|
4
|
+
import { logRuntime, logLoadingPlugins, logOutdatedPlugins, logIncompatiblePlugins, } from '../log/messages/compatibility.js';
|
|
5
|
+
import { measureDuration } from '../time/main.js';
|
|
6
|
+
import { getEventFromChild } from './ipc.js';
|
|
7
|
+
import { getSpawnInfo } from './options.js';
|
|
8
|
+
const CHILD_MAIN_FILE = fileURLToPath(new URL('child/main.js', import.meta.url));
|
|
9
|
+
// Start child processes used by all plugins
|
|
10
|
+
// We fire plugins through child processes so that:
|
|
11
|
+
// - each plugin is sandboxed, e.g. cannot access/modify its parent `process`
|
|
12
|
+
// (for both security and safety reasons)
|
|
13
|
+
// - logs can be buffered which allows manipulating them for log shipping,
|
|
14
|
+
// transforming and parallel plugins
|
|
15
|
+
const tStartPlugins = async function ({ pluginsOptions, buildDir, childEnv, logs, debug, featureFlags }) {
|
|
16
|
+
logRuntime(logs, pluginsOptions);
|
|
17
|
+
logLoadingPlugins(logs, pluginsOptions, debug);
|
|
18
|
+
logOutdatedPlugins(logs, pluginsOptions, featureFlags);
|
|
19
|
+
logIncompatiblePlugins(logs, pluginsOptions);
|
|
20
|
+
const childProcesses = await Promise.all(pluginsOptions.map(({ pluginDir, nodePath }) => startPlugin({ pluginDir, nodePath, buildDir, childEnv, featureFlags })));
|
|
21
|
+
return { childProcesses };
|
|
22
|
+
};
|
|
23
|
+
export const startPlugins = measureDuration(tStartPlugins, 'start_plugins');
|
|
24
|
+
const startPlugin = async function ({ pluginDir, nodePath, buildDir, childEnv, featureFlags }) {
|
|
25
|
+
const childProcess = execaNode(CHILD_MAIN_FILE, [], {
|
|
26
|
+
cwd: buildDir,
|
|
27
|
+
preferLocal: true,
|
|
28
|
+
localDir: pluginDir,
|
|
29
|
+
nodePath,
|
|
30
|
+
execPath: nodePath,
|
|
31
|
+
env: childEnv,
|
|
32
|
+
extendEnv: false,
|
|
33
|
+
// Feature flag: https://app.launchdarkly.com/default/production/features/netlify_build_use_json_serialization_for_plugin_ipc/targeting
|
|
34
|
+
// TODO: remove feature flag once fully rolled out
|
|
35
|
+
...(!featureFlags.netlify_build_use_json_serialization_for_plugin_ipc && {
|
|
36
|
+
serialization: 'advanced',
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
try {
|
|
40
|
+
await getEventFromChild(childProcess, 'ready');
|
|
41
|
+
return { childProcess };
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
const spawnInfo = getSpawnInfo();
|
|
45
|
+
addErrorInfo(error, spawnInfo);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
// Stop all plugins child processes
|
|
50
|
+
export const stopPlugins = function (childProcesses) {
|
|
51
|
+
childProcesses.forEach(stopPlugin);
|
|
52
|
+
};
|
|
53
|
+
const stopPlugin = function ({ childProcess }) {
|
|
54
|
+
if (childProcess.connected) {
|
|
55
|
+
childProcess.disconnect();
|
|
56
|
+
}
|
|
57
|
+
childProcess.kill();
|
|
58
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { listCorePlugins, isCorePlugin } from './list.js';
|
|
2
|
+
// Add core plugins and user plugins together.
|
|
3
|
+
// Do not allow user override of core plugins.
|
|
4
|
+
export const addCorePlugins = function ({ netlifyConfig: { plugins }, constants }) {
|
|
5
|
+
const corePlugins = listCorePlugins(constants);
|
|
6
|
+
const allCorePlugins = corePlugins
|
|
7
|
+
.map((corePlugin) => addCoreProperties(corePlugin, plugins))
|
|
8
|
+
.filter((corePlugin) => !isOptionalCore(corePlugin, plugins));
|
|
9
|
+
const userPlugins = plugins.filter(isUserPlugin);
|
|
10
|
+
const allPlugins = [...userPlugins, ...allCorePlugins];
|
|
11
|
+
const pluginsOptions = allPlugins.map(normalizePluginOptions);
|
|
12
|
+
return pluginsOptions;
|
|
13
|
+
};
|
|
14
|
+
const addCoreProperties = function (corePlugin, plugins) {
|
|
15
|
+
const inputs = getCorePluginInputs(corePlugin, plugins);
|
|
16
|
+
return { ...corePlugin, inputs, loadedFrom: 'core', origin: 'core' };
|
|
17
|
+
};
|
|
18
|
+
// Core plugins can get inputs too
|
|
19
|
+
const getCorePluginInputs = function (corePlugin, plugins) {
|
|
20
|
+
const configuredCorePlugin = plugins.find((plugin) => plugin.package === corePlugin.package);
|
|
21
|
+
if (configuredCorePlugin === undefined) {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
return configuredCorePlugin.inputs;
|
|
25
|
+
};
|
|
26
|
+
// Optional core plugins requires user opt-in
|
|
27
|
+
const isOptionalCore = function (pluginA, plugins) {
|
|
28
|
+
return pluginA.optional && plugins.every((pluginB) => pluginB.package !== pluginA.package);
|
|
29
|
+
};
|
|
30
|
+
const isUserPlugin = function (plugin) {
|
|
31
|
+
return !isCorePlugin(plugin.package);
|
|
32
|
+
};
|
|
33
|
+
const normalizePluginOptions = function ({ package: packageName, pluginPath, pinned_version: pinnedVersion, loadedFrom, origin, inputs, }) {
|
|
34
|
+
return { packageName, pluginPath, pinnedVersion, loadedFrom, origin, inputs };
|
|
35
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
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';
|
|
7
|
+
// Fire `build.command`
|
|
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
|
+
};
|
|
35
|
+
// We use Bash on Unix and `cmd.exe` on Windows
|
|
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
|
+
};
|
|
43
|
+
export const buildCommandCore = {
|
|
44
|
+
event: 'onBuild',
|
|
45
|
+
coreStep,
|
|
46
|
+
coreStepId: 'build_command',
|
|
47
|
+
coreStepName: 'build.command',
|
|
48
|
+
coreStepDescription,
|
|
49
|
+
condition: hasBuildCommand,
|
|
50
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
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';
|
|
8
|
+
export const createBuildbotClient = function (buildbotServerSocket) {
|
|
9
|
+
const connectionOpts = getConnectionOpts(buildbotServerSocket);
|
|
10
|
+
const client = net.createConnection(connectionOpts);
|
|
11
|
+
return client;
|
|
12
|
+
};
|
|
13
|
+
// Windows does not support Unix sockets well, so we also support `host:port`
|
|
14
|
+
const getConnectionOpts = function (buildbotServerSocket) {
|
|
15
|
+
if (!buildbotServerSocket.includes(':')) {
|
|
16
|
+
return { path: buildbotServerSocket };
|
|
17
|
+
}
|
|
18
|
+
const [host, port] = buildbotServerSocket.split(':');
|
|
19
|
+
return { host, port };
|
|
20
|
+
};
|
|
21
|
+
const eConnectBuildbotClient = async function (client) {
|
|
22
|
+
await pEvent(client, 'connect');
|
|
23
|
+
};
|
|
24
|
+
export const connectBuildbotClient = addAsyncErrorMessage(eConnectBuildbotClient, 'Could not connect to buildbot');
|
|
25
|
+
export const closeBuildbotClient = async function (client) {
|
|
26
|
+
if (client.destroyed) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
await promisify(client.end.bind(client))();
|
|
30
|
+
};
|
|
31
|
+
const cWritePayload = async function (buildbotClient, payload) {
|
|
32
|
+
await promisify(buildbotClient.write.bind(buildbotClient))(JSON.stringify(payload));
|
|
33
|
+
};
|
|
34
|
+
const writePayload = addAsyncErrorMessage(cWritePayload, 'Could not send payload to buildbot');
|
|
35
|
+
const cGetNextParsedResponsePromise = async function (buildbotClient) {
|
|
36
|
+
const data = await pEvent(buildbotClient, 'data');
|
|
37
|
+
return JSON.parse(data);
|
|
38
|
+
};
|
|
39
|
+
const getNextParsedResponsePromise = addAsyncErrorMessage(cGetNextParsedResponsePromise, 'Invalid response from buildbot');
|
|
40
|
+
export const deploySiteWithBuildbotClient = async function ({ client, events, buildDir, repositoryRoot, constants }) {
|
|
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
|
+
};
|
|
52
|
+
// The file paths in the buildbot are relative to the repository root.
|
|
53
|
+
// However, the file paths in Build plugins, including `constants.PUBLISH_DIR`
|
|
54
|
+
// are relative to the build directory, which is different when there is a
|
|
55
|
+
// base directory. This converts it.
|
|
56
|
+
// We need to call `normalize()` in case the publish directory is the
|
|
57
|
+
// repository root, so `deployDir` is "." not ""
|
|
58
|
+
const getDeployDir = function ({ buildDir, repositoryRoot, constants: { PUBLISH_DIR } }) {
|
|
59
|
+
const absolutePublishDir = resolve(buildDir, PUBLISH_DIR);
|
|
60
|
+
const relativePublishDir = relative(repositoryRoot, absolutePublishDir);
|
|
61
|
+
const deployDir = normalize(relativePublishDir);
|
|
62
|
+
return deployDir;
|
|
63
|
+
};
|
|
64
|
+
// We distinguish between user errors and system errors during deploys
|
|
65
|
+
const handleDeployError = function (error, errorType) {
|
|
66
|
+
const errorIs422 = error !== undefined && error.code === '422';
|
|
67
|
+
const errMsg = errorIs422
|
|
68
|
+
? `
|
|
69
|
+
File upload failed because of mismatched SHAs.
|
|
70
|
+
This can happen when files are changed after the deployment process has started but before they are uploaded.
|
|
71
|
+
|
|
72
|
+
Error: ${error}
|
|
73
|
+
`
|
|
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
|
+
};
|
|
80
|
+
// We only wait for post-processing (last stage before site deploy) if the build
|
|
81
|
+
// has some plugins that do post-deploy logic
|
|
82
|
+
const shouldWaitForPostProcessing = function (events) {
|
|
83
|
+
return events.some(hasPostDeployLogic);
|
|
84
|
+
};
|
|
85
|
+
const hasPostDeployLogic = function (event) {
|
|
86
|
+
return runsAfterDeploy(event);
|
|
87
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
};
|
|
39
|
+
const shouldDeploy = function ({ buildbotServerSocket }) {
|
|
40
|
+
return buildbotServerSocket !== undefined;
|
|
41
|
+
};
|
|
42
|
+
export const deploySite = {
|
|
43
|
+
event: 'onPostBuild',
|
|
44
|
+
coreStep,
|
|
45
|
+
coreStepId: 'deploy_site',
|
|
46
|
+
coreStepName: 'Deploy site',
|
|
47
|
+
coreStepDescription: () => 'Deploy site',
|
|
48
|
+
condition: shouldDeploy,
|
|
49
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,76 @@
|
|
|
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 { validateEdgeFunctionsManifest } from './validate_manifest/validate_edge_functions_manifest.js';
|
|
8
|
+
// TODO: Replace this with a custom cache directory.
|
|
9
|
+
const DENO_CLI_CACHE_DIRECTORY = '.netlify/plugins/deno-cli';
|
|
10
|
+
const IMPORT_MAP_FILENAME = 'edge-functions-import-map.json';
|
|
11
|
+
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, }) {
|
|
12
|
+
const { edge_functions: declarations = [] } = netlifyConfig;
|
|
13
|
+
const distPath = resolve(buildDir, distDirectory);
|
|
14
|
+
const internalSrcPath = resolve(buildDir, internalSrcDirectory);
|
|
15
|
+
const distImportMapPath = join(dirname(internalSrcPath), IMPORT_MAP_FILENAME);
|
|
16
|
+
const srcPath = srcDirectory ? resolve(buildDir, srcDirectory) : undefined;
|
|
17
|
+
const sourcePaths = [internalSrcPath, srcPath].filter(Boolean);
|
|
18
|
+
logFunctions({ internalSrcDirectory, internalSrcPath, logs, srcDirectory, srcPath });
|
|
19
|
+
// If we're running in buildbot and the feature flag is enabled, we set the
|
|
20
|
+
// Deno cache dir to a directory that is persisted between builds.
|
|
21
|
+
const cacheDirectory = !isRunningLocally && featureFlags.edge_functions_cache_cli ? resolve(buildDir, DENO_CLI_CACHE_DIRECTORY) : undefined;
|
|
22
|
+
// Edge Bundler expects the dist directory to exist.
|
|
23
|
+
await fs.mkdir(distPath, { recursive: true });
|
|
24
|
+
try {
|
|
25
|
+
const { manifest } = await bundle(sourcePaths, distPath, declarations, {
|
|
26
|
+
basePath: buildDir,
|
|
27
|
+
cacheDirectory,
|
|
28
|
+
configPath: join(internalSrcPath, 'manifest.json'),
|
|
29
|
+
debug,
|
|
30
|
+
distImportMapPath,
|
|
31
|
+
featureFlags,
|
|
32
|
+
systemLogger: featureFlags.edge_functions_system_logger ? systemLog : undefined,
|
|
33
|
+
});
|
|
34
|
+
systemLog('Edge Functions manifest:', manifest);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
tagBundlingError(error);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
await validateEdgeFunctionsManifest({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory } });
|
|
41
|
+
return {};
|
|
42
|
+
};
|
|
43
|
+
// We run this core step if at least one of the functions directories (the
|
|
44
|
+
// one configured by the user or the internal one) exists. We use a dynamic
|
|
45
|
+
// `condition` because the directories might be created by the build command
|
|
46
|
+
// or plugins.
|
|
47
|
+
const hasEdgeFunctionsDirectories = async function ({ buildDir, constants: { INTERNAL_EDGE_FUNCTIONS_SRC, EDGE_FUNCTIONS_SRC }, }) {
|
|
48
|
+
const hasFunctionsSrc = EDGE_FUNCTIONS_SRC !== undefined && EDGE_FUNCTIONS_SRC !== '';
|
|
49
|
+
if (hasFunctionsSrc) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
const internalFunctionsSrc = resolve(buildDir, INTERNAL_EDGE_FUNCTIONS_SRC);
|
|
53
|
+
return await pathExists(internalFunctionsSrc);
|
|
54
|
+
};
|
|
55
|
+
const logFunctions = async ({ internalSrcDirectory, internalSrcPath, logs, srcDirectory: userFunctionsSrc, srcPath, }) => {
|
|
56
|
+
const [userFunctions, internalFunctions] = await Promise.all([find([srcPath]), find([internalSrcPath])]);
|
|
57
|
+
const userFunctionsSrcExists = await pathExists(srcPath);
|
|
58
|
+
const internalFunctionsSrc = internalSrcDirectory;
|
|
59
|
+
logFunctionsToBundle({
|
|
60
|
+
logs,
|
|
61
|
+
userFunctions: userFunctions.map(({ name }) => name),
|
|
62
|
+
userFunctionsSrc,
|
|
63
|
+
userFunctionsSrcExists,
|
|
64
|
+
internalFunctions: internalFunctions.map(({ name }) => name),
|
|
65
|
+
internalFunctionsSrc,
|
|
66
|
+
type: 'Edge Functions',
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
export const bundleEdgeFunctions = {
|
|
70
|
+
event: 'onBuild',
|
|
71
|
+
coreStep,
|
|
72
|
+
coreStepId: 'edge_functions_bundling',
|
|
73
|
+
coreStepName: 'Edge Functions bundling',
|
|
74
|
+
coreStepDescription: () => 'Edge Functions bundling',
|
|
75
|
+
condition: hasEdgeFunctionsDirectories,
|
|
76
|
+
};
|
|
@@ -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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import { join, resolve } from 'path';
|
|
3
|
+
import { ManifestValidationError, validateManifest } from '@netlify/edge-bundler';
|
|
4
|
+
import { addErrorInfo } from '../../../error/info.js';
|
|
5
|
+
export const validateEdgeFunctionsManifest = async function ({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory }, }) {
|
|
6
|
+
const edgeFunctionsDistPath = resolve(buildDir, distDirectory);
|
|
7
|
+
const manifestPath = join(edgeFunctionsDistPath, 'manifest.json');
|
|
8
|
+
const data = await fs.readFile(manifestPath);
|
|
9
|
+
// @ts-expect-error TypeScript is not aware that parse can handle Buffer
|
|
10
|
+
const manifestData = JSON.parse(data);
|
|
11
|
+
try {
|
|
12
|
+
validateManifest(manifestData);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error instanceof ManifestValidationError) {
|
|
16
|
+
addErrorInfo(error, { type: 'coreStep' });
|
|
17
|
+
}
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
return {};
|
|
21
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import readdirp from 'readdirp';
|
|
2
|
+
import { addErrorInfo } from '../../error/info.js';
|
|
3
|
+
const MODULE_NOT_FOUND_CODE = 'MODULE_NOT_FOUND';
|
|
4
|
+
const MODULE_NOT_FOUND_ESBUILD_REGEXP = /^Could not resolve ['"]([^'"]+)/;
|
|
5
|
+
const MODULE_NOT_FOUND_REGEXP = /Cannot find module ['"]([^'"]+)/;
|
|
6
|
+
// Handle errors coming from zip-it-and-ship-it
|
|
7
|
+
export const getZipError = async function (error, functionsSrc) {
|
|
8
|
+
const moduleNotFoundError = await getModuleNotFoundError(error, functionsSrc);
|
|
9
|
+
if (moduleNotFoundError) {
|
|
10
|
+
return moduleNotFoundError;
|
|
11
|
+
}
|
|
12
|
+
if (isPackageJsonError(error)) {
|
|
13
|
+
return getPackageJsonError(error);
|
|
14
|
+
}
|
|
15
|
+
return error;
|
|
16
|
+
};
|
|
17
|
+
const getModuleNotFoundError = async function (error, functionsSrc) {
|
|
18
|
+
const errorFromZisi = await getModuleNotFoundErrorFromZISI(error, functionsSrc);
|
|
19
|
+
if (errorFromZisi) {
|
|
20
|
+
return errorFromZisi;
|
|
21
|
+
}
|
|
22
|
+
const errorFromEsbuild = await getModuleNotFoundErrorFromEsbuild(error, functionsSrc);
|
|
23
|
+
if (errorFromEsbuild) {
|
|
24
|
+
return errorFromEsbuild;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const getModuleNotFoundErrorObject = async ({ error, functionsSrc, moduleNames }) => {
|
|
28
|
+
const message = await getModuleNotFoundMessage(functionsSrc, moduleNames);
|
|
29
|
+
error.message = `${message}\n\n${error.message}`;
|
|
30
|
+
addErrorInfo(error, { type: 'dependencies' });
|
|
31
|
+
return error;
|
|
32
|
+
};
|
|
33
|
+
const getModuleNotFoundMessage = async function (functionsSrc, moduleNames) {
|
|
34
|
+
if (moduleNames.length === 0 || !(await lacksNodeModules(functionsSrc))) {
|
|
35
|
+
return MODULE_NOT_FOUND_MESSAGE;
|
|
36
|
+
}
|
|
37
|
+
if (moduleNames.filter(Boolean).some(isLocalPath)) {
|
|
38
|
+
return PATH_NOT_FOUND_MESSAGE;
|
|
39
|
+
}
|
|
40
|
+
return getLocalInstallMessage(moduleNames);
|
|
41
|
+
};
|
|
42
|
+
const isLocalPath = function (moduleName) {
|
|
43
|
+
return moduleName.startsWith('.') || moduleName.startsWith('/');
|
|
44
|
+
};
|
|
45
|
+
const getModuleNotFoundErrorFromEsbuild = function (error, functionsSrc) {
|
|
46
|
+
const { errors = [] } = error;
|
|
47
|
+
const modulesNotFound = errors.reduce((modules, errorObject) => {
|
|
48
|
+
const match = errorObject.text.match(MODULE_NOT_FOUND_ESBUILD_REGEXP);
|
|
49
|
+
if (!match) {
|
|
50
|
+
return modules;
|
|
51
|
+
}
|
|
52
|
+
return [...modules, match[1]];
|
|
53
|
+
}, []);
|
|
54
|
+
if (modulesNotFound.length === 0) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
return getModuleNotFoundErrorObject({ error, functionsSrc, moduleNames: modulesNotFound });
|
|
58
|
+
};
|
|
59
|
+
const getModuleNotFoundErrorFromZISI = function (error, functionsSrc) {
|
|
60
|
+
if (!(error instanceof Error && error.code === MODULE_NOT_FOUND_CODE)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const moduleName = getModuleNameFromZISIError(error);
|
|
64
|
+
return getModuleNotFoundErrorObject({ error, functionsSrc, moduleNames: moduleName ? [moduleName] : [] });
|
|
65
|
+
};
|
|
66
|
+
// This error message always include the same words
|
|
67
|
+
const getModuleNameFromZISIError = function (error) {
|
|
68
|
+
if (typeof error.message !== 'string') {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const result = MODULE_NOT_FOUND_REGEXP.exec(error.message);
|
|
72
|
+
if (result === null) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
return result[1];
|
|
76
|
+
};
|
|
77
|
+
// Netlify Functions has a `package.json` but no `node_modules`
|
|
78
|
+
const lacksNodeModules = async function (functionsSrc) {
|
|
79
|
+
return (functionsSrc !== undefined &&
|
|
80
|
+
(await hasFunctionRootFile('package.json', functionsSrc)) &&
|
|
81
|
+
!(await hasFunctionRootFile('node_modules', functionsSrc)));
|
|
82
|
+
};
|
|
83
|
+
// Functions can be either files or directories, so we need to check on two
|
|
84
|
+
// depth levels
|
|
85
|
+
const hasFunctionRootFile = async function (filename, functionsSrc) {
|
|
86
|
+
const files = await readdirp.promise(functionsSrc, { depth: 1, fileFilter: filename });
|
|
87
|
+
return files.length !== 0;
|
|
88
|
+
};
|
|
89
|
+
const MODULE_NOT_FOUND_MESSAGE = `A Netlify Function failed to require one of its dependencies.
|
|
90
|
+
Please make sure it is present in the site's top-level "package.json".`;
|
|
91
|
+
const PATH_NOT_FOUND_MESSAGE = `A Netlify Function failed to require a local file.
|
|
92
|
+
Please make sure the file exists and its path is correctly spelled.`;
|
|
93
|
+
// A common mistake is to assume Netlify Functions dependencies are
|
|
94
|
+
// automatically installed. This checks for this pattern.
|
|
95
|
+
const getLocalInstallMessage = function (modules) {
|
|
96
|
+
const genericMessage = `
|
|
97
|
+
|
|
98
|
+
By default, dependencies inside a Netlify Function's "package.json" are not automatically installed.
|
|
99
|
+
There are several ways to fix this problem:
|
|
100
|
+
- Removing your Function's "package.json" and adding the dependencies to the project's top-level "package.json" instead. This is the fastest and safest solution.
|
|
101
|
+
- Running "npm install" or "yarn" inside your Netlify Function in your build command.
|
|
102
|
+
- Adding the following plugin to your "netlify.toml":
|
|
103
|
+
|
|
104
|
+
[[plugins]]
|
|
105
|
+
package = "@netlify/plugin-functions-install-core"
|
|
106
|
+
`;
|
|
107
|
+
if (modules.length === 1) {
|
|
108
|
+
return `A Netlify Function is using "${modules[0]}" but that dependency has not been installed yet.${genericMessage}`;
|
|
109
|
+
}
|
|
110
|
+
const moduleNames = modules.map((name) => `"${name}"`).join(', ');
|
|
111
|
+
return `A Netlify Function is using dependencies that have not been installed yet: ${moduleNames}${genericMessage}`;
|
|
112
|
+
};
|
|
113
|
+
// We need to load the site's `package.json` when bundling Functions. This is
|
|
114
|
+
// because `optionalDependencies` can make `import()` fail, but we don't want
|
|
115
|
+
// to error then. However, if the `package.json` is invalid, we fail the build.
|
|
116
|
+
const isPackageJsonError = function (error) {
|
|
117
|
+
return PACKAGE_JSON_ORIGINAL_MESSAGES.some((msg) => error.message.includes(msg));
|
|
118
|
+
};
|
|
119
|
+
const PACKAGE_JSON_ORIGINAL_MESSAGES = ['is invalid JSON', 'in JSON at position'];
|
|
120
|
+
const getPackageJsonError = function (error) {
|
|
121
|
+
addErrorInfo(error, { type: 'resolveConfig' });
|
|
122
|
+
return error;
|
|
123
|
+
};
|