@netlify/build 19.0.8 → 20.0.0

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.0",
4
4
  "description": "Netlify build module",
5
5
  "main": "src/core/main.js",
6
6
  "types": "types/index.d.ts",
@@ -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