@netlify/build 19.0.8 → 20.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/build",
3
- "version": "19.0.8",
3
+ "version": "20.0.3",
4
4
  "description": "Netlify build module",
5
5
  "main": "src/core/main.js",
6
6
  "types": "types/index.d.ts",
@@ -58,10 +58,10 @@
58
58
  "@netlify/config": "^16.0.0",
59
59
  "@netlify/functions-utils": "^3.0.0",
60
60
  "@netlify/git-utils": "^3.0.0",
61
- "@netlify/plugin-edge-handlers": "^2.0.0",
61
+ "@netlify/plugin-edge-handlers": "^3.0.0",
62
62
  "@netlify/plugins-list": "^5.0.0",
63
63
  "@netlify/run-utils": "^3.0.0",
64
- "@netlify/zip-it-and-ship-it": "^5.1.0",
64
+ "@netlify/zip-it-and-ship-it": "^5.2.0",
65
65
  "@sindresorhus/slugify": "^1.1.0",
66
66
  "ansi-escapes": "^4.3.2",
67
67
  "chalk": "^4.1.2",
@@ -11,9 +11,9 @@ const { validatePlugin } = require('./validate')
11
11
  // This also validates the plugin.
12
12
  // Do it when parent requests it using the `load` event.
13
13
  // Also figure out the list of plugin steps. This is also passed to the parent.
14
- const load = function ({ pluginPath, inputs, packageJson }) {
14
+ const load = async function ({ pluginPath, inputs, packageJson }) {
15
15
  const tsNodeService = registerTypeScript(pluginPath)
16
- const logic = getLogic({ pluginPath, inputs, tsNodeService })
16
+ const logic = await getLogic({ pluginPath, inputs, tsNodeService })
17
17
 
18
18
  validatePlugin(logic)
19
19
 
@@ -1,22 +1,43 @@
1
1
  'use strict'
2
2
 
3
+ const { pathToFileURL } = require('url')
4
+
3
5
  const { addTsErrorInfo } = require('./typescript')
4
6
 
5
7
  // Require the plugin file and fire its top-level function.
6
8
  // The returned object is the `logic` which includes all event handlers.
7
- const getLogic = function ({ pluginPath, inputs, tsNodeService }) {
8
- const logic = requireLogic(pluginPath, tsNodeService)
9
+ const getLogic = async function ({ pluginPath, inputs, tsNodeService }) {
10
+ const logic = await importLogic(pluginPath, tsNodeService)
9
11
  const logicA = loadLogic({ logic, inputs })
10
12
  return logicA
11
13
  }
12
14
 
13
- const requireLogic = function (pluginPath, tsNodeService) {
15
+ const importLogic = async function (pluginPath, tsNodeService) {
14
16
  try {
15
- // eslint-disable-next-line node/global-require, import/no-dynamic-require
16
- return require(pluginPath)
17
+ // `ts-node` is not available programmatically for pure ES modules yet,
18
+ // which is currently making it impossible for local plugins to use both
19
+ // pure ES modules and TypeScript.
20
+ if (tsNodeService !== undefined) {
21
+ // eslint-disable-next-line node/global-require, import/no-dynamic-require
22
+ return require(pluginPath)
23
+ }
24
+
25
+ // `pluginPath` is an absolute file path but `import()` needs URLs.
26
+ // Converting those with `pathToFileURL()` is needed especially on Windows
27
+ // where the drive letter would not work with `import()`.
28
+ const returnValue = await import(pathToFileURL(pluginPath))
29
+ // Plugins should use named exports, but we still support default exports
30
+ // for backward compatibility with CommonJS
31
+ return returnValue.default === undefined ? returnValue : returnValue.default
17
32
  } catch (error) {
18
33
  addTsErrorInfo(error, tsNodeService)
19
- error.message = `Could not import plugin:\n${error.message}`
34
+ // We must change `error.stack` instead of `error.message` because some
35
+ // errors thrown from `import()` access `error.stack` before throwing.
36
+ // `error.stack` is lazily instantiated by Node.js, so changing
37
+ // `error.message` afterwards would not modify `error.stack`. Therefore, the
38
+ // resulting stack trace, which is printed in the build logs, would not
39
+ // include the additional message prefix.
40
+ error.stack = `Could not import plugin:\n${error.stack}`
20
41
  throw error
21
42
  }
22
43
  }
@@ -1,7 +1,5 @@
1
1
  'use strict'
2
2
 
3
- const isPlainObj = require('is-plain-obj')
4
-
5
3
  const { addErrorInfo } = require('../../error/info')
6
4
  const { serializeArray } = require('../../log/serialize')
7
5
  const { EVENTS } = require('../events')
@@ -9,7 +7,9 @@ const { EVENTS } = require('../events')
9
7
  // Validate the shape of a plugin return value
10
8
  const validatePlugin = function (logic) {
11
9
  try {
12
- if (!isPlainObj(logic)) {
10
+ // This validation must work with the return value of `import()` which has
11
+ // a `Module` prototype, not `Object`
12
+ if (typeof logic !== 'object' || logic === null) {
13
13
  throw new Error('Plugin must be an object or a function')
14
14
  }
15
15
 
package/types/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export { OnBuild, OnEnd, OnError, OnPostBuild, OnPreBuild, OnSuccess } from './n
4
4
  export { NetlifyConfig } from './config/netlify_config'
5
5
  export { NetlifyPluginUtils } from './options'
6
6
  export { NetlifyPluginConstants } from './netlify_plugin_constants'
7
+ export { ListedFunction, ListedFunctionFile } from './options/netlify_plugin_functions_util'
@@ -0,0 +1,32 @@
1
+ import type { listFunctions, listFunctionsFiles } from '@netlify/zip-it-and-ship-it'
2
+
3
+ type Resolved<T> = T extends Promise<infer U> ? U : T
4
+
5
+ export type ListedFunction = Resolved<ReturnType<typeof listFunctions>>[0]
6
+ export type ListedFunctionFile = Resolved<ReturnType<typeof listFunctionsFiles>>[0]
7
+
8
+ export interface NetlifyPluginFunctionsUtil {
9
+ /**
10
+ * Returns the list of Netlify Functions main files as a Promise resolving to an array of objects with the following properties:
11
+ *
12
+ * - `name`: Function name, as used in the URL `https://{hostname}/.netlify/functions/{name}`
13
+ * - `mainFile`: absolute path to the Function's main file
14
+ * - `extension`: file extension of the Function's main file. For Go Functions, this might be an empty string. For Node.js Functions, this is either `.js` or `.zip`.
15
+ * - `runtime` `"js" | "go"`: Function's language runtime. TypeScript Functions use the "js" runtime
16
+ */
17
+ list(): Promise<Array<ListedFunction>>
18
+
19
+ /**
20
+ * Same as `list()` except it also returns the files required by the Functions' main files. This is much slower. The object have the following additional member:
21
+ *
22
+ * - `srcFile`: absolute path to the file
23
+ */
24
+ listAll(): Promise<Array<ListedFunctionFile>>
25
+
26
+ /**
27
+ * Add a Functions file or directory to a build.
28
+ *
29
+ * @param path Path to the function file or directory.
30
+ */
31
+ add(path: string): Promise<void>
32
+ }
@@ -1,5 +1,6 @@
1
1
  import { NetlifyPluginBuildUtil } from './netlify_plugin_build_util'
2
2
  import { NetlifyPluginCacheUtil } from './netlify_plugin_cache_util'
3
+ import { NetlifyPluginFunctionsUtil } from './netlify_plugin_functions_util'
3
4
  import { NetlifyPluginGitUtil } from './netlify_plugin_git_util'
4
5
  import { NetlifyPluginRunUtil } from './netlify_plugin_run_util'
5
6
  import { NetlifyPluginStatusUtil } from './netlify_plugin_status_util'
@@ -10,4 +11,5 @@ export interface NetlifyPluginUtils {
10
11
  cache: NetlifyPluginCacheUtil
11
12
  run: NetlifyPluginRunUtil
12
13
  git: NetlifyPluginGitUtil
14
+ functions: NetlifyPluginFunctionsUtil
13
15
  }