File size: 1,737 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable @typescript-eslint/no-unused-vars */

/// <reference path="../shared/runtime-utils.ts" />

/// A 'base' utilities to support runtime can have externals.
/// Currently this is for node.js / edge runtime both.
/// If a fn requires node.js specific behavior, it should be placed in `node-external-utils` instead.

async function externalImport(id: DependencySpecifier) {
  let raw
  try {
    raw = await import(id)
  } catch (err) {
    // TODO(alexkirsz) This can happen when a client-side module tries to load
    // an external module we don't provide a shim for (e.g. querystring, url).
    // For now, we fail semi-silently, but in the future this should be a
    // compilation error.
    throw new Error(`Failed to load external module ${id}: ${err}`)
  }

  if (raw && raw.__esModule && raw.default && 'default' in raw.default) {
    return interopEsm(raw.default, createNS(raw), true)
  }

  return raw
}
contextPrototype.y = externalImport

function externalRequire(
  id: ModuleId,
  thunk: () => any,
  esm: boolean = false
): Exports | EsmNamespaceObject {
  let raw
  try {
    raw = thunk()
  } catch (err) {
    // TODO(alexkirsz) This can happen when a client-side module tries to load
    // an external module we don't provide a shim for (e.g. querystring, url).
    // For now, we fail semi-silently, but in the future this should be a
    // compilation error.
    throw new Error(`Failed to load external module ${id}: ${err}`)
  }

  if (!esm || raw.__esModule) {
    return raw
  }

  return interopEsm(raw, createNS(raw), true)
}

externalRequire.resolve = (
  id: string,
  options?: {
    paths?: string[]
  }
) => {
  return require.resolve(id, options)
}
contextPrototype.x = externalRequire