@wix/zero-config-implementation 1.26.0 → 1.28.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.
@@ -1,9 +1,17 @@
1
1
  import { createRequire } from 'node:module'
2
2
  import { ResultAsync, errAsync, okAsync } from 'neverthrow'
3
3
  import type { ComponentType } from 'react'
4
- import { IoError } from './errors'
5
4
 
6
- type IoErrorInstance = InstanceType<typeof IoError>
5
+ /**
6
+ * Structured failure from `loadModule` when both ESM import and CJS require fail.
7
+ * Carries each error separately so callers can report them independently.
8
+ */
9
+ export interface LoadModuleFailure {
10
+ /** The ESM import error, or null when no ESM was attempted (e.g. empty path). */
11
+ esmError: Error | null
12
+ /** The CJS require error, or a generic error for the empty-path case. */
13
+ cjsError: Error
14
+ }
7
15
 
8
16
  /**
9
17
  * Attempts to load a module, first via ESM `import()`, then via CJS `require`.
@@ -16,22 +24,24 @@ type IoErrorInstance = InstanceType<typeof IoError>
16
24
  *
17
25
  * @param entryPath - Absolute path to the module entry point.
18
26
  * @returns A `ResultAsync` containing the module exports on success.
19
- * @errors {IoError} When both ESM import and CJS require fail.
27
+ * @errors {LoadModuleFailure} When both ESM import and CJS require fail.
20
28
  */
21
- export function loadModule(entryPath: string): ResultAsync<Record<string, unknown>, IoErrorInstance> {
22
- return ResultAsync.fromPromise(import(entryPath) as Promise<Record<string, unknown>>, () => null).orElse(() => {
29
+ export function loadModule(entryPath: string): ResultAsync<Record<string, unknown>, LoadModuleFailure> {
30
+ if (!entryPath) {
31
+ return errAsync({ esmError: null, cjsError: new Error('No compiled entry path provided') })
32
+ }
33
+
34
+ return ResultAsync.fromPromise(
35
+ import(entryPath) as Promise<Record<string, unknown>>,
36
+ (esmErr): Error => (esmErr instanceof Error ? esmErr : new Error(String(esmErr))),
37
+ ).orElse((esmError) => {
23
38
  try {
24
39
  const require = createRequire(import.meta.url)
25
40
  const exports = require(entryPath)
26
41
  return okAsync(exports as Record<string, unknown>)
27
42
  } catch (requireErr) {
28
- const cause = requireErr instanceof Error ? requireErr : new Error(String(requireErr))
29
- return errAsync(
30
- new IoError(`Failed to load ${entryPath}: ${cause.message}`, {
31
- cause,
32
- props: { phase: 'load' },
33
- }),
34
- )
43
+ const cjsError = requireErr instanceof Error ? requireErr : new Error(String(requireErr))
44
+ return errAsync({ esmError, cjsError })
35
45
  }
36
46
  })
37
47
  }