aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/module.exports.js
blob: 856f7587574bedee42d4bfb4855d9af66927cbbe (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
var fileURLToPath;

var pathsFunction = function paths() {
  return [];
};

export function createRequire(filename) {
  var filenameString = filename;
  const isURL =
    typeof filename === "object" && filename && filename instanceof URL;

  if (isURL) {
    fileURLToPath ||= globalThis[Symbol.for("Bun.lazy")]("fileURLToPath");
    filenameString = fileURLToPath(filename);
  }

  var pathObject = {
    path: filenameString,
    resolveSync,
  };
  var bunResolveSync = import.meta.resolveSync;
  var realRequire = import.meta.require;

  function resolveSync(id) {
    return arguments.length <= 1
      ? bunResolveSync.call(pathObject, id)
      : bunResolveSync.call(pathObject, id, arguments[1]);
  }

  var requireFunction = function require(id) {
    return realRequire.call(
      pathObject,
      bunResolveSync.call(pathObject, id, filenameString)
    );
  };

  requireFunction.resolve = function resolve(id, pathsArg) {
    if (arguments.length > 1 && pathsArg && typeof pathsArg === "object") {
      var { paths } = pathsArg;
      if (paths && Array.isArray(paths) && paths.length > 0) {
        return bunResolveSync.call(pathObject, id, paths[0]);
      }
    }

    return bunResolveSync.call(pathObject, id);
  };
  requireFunction.resolve.paths = pathsFunction;
  requireFunction.main = undefined;

  return requireFunction;
}

// this isn't exhaustive
export const builtinModules = ["node:path", "node:fs", "bun:ffi", "bun:sqlite"];

// noop
export function syncBuiltinESMExports() {}

export function findSourceMap(path) {
  throw new Error("findSourceMap is not implemented");
}

export function SourceMap() {
  throw new Error("SourceMap is not implemented");
}

export default {
  createRequire,
  syncBuiltinESMExports,
  findSourceMap,
  SourceMap,
};