aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/module.exports.js
blob: f0817a059181eec89053ff9e2bcba3851b7df94a (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function resolve(request, args) {
  if (typeof args === "object" && args?.paths?.length) {
    return this.resolveSync(request, args);
  }

  return this.resolveSync(request);
}

// not implemented
resolve.paths = () => [];

function require(pathString) {
  // this refers to an ImportMeta instance
  const resolved = this.resolveSync(pathString);
  if (
    !resolved.endsWith(".node") &&
    !resolved.endsWith(".json") &&
    !resolved.endsWith(".toml")
  ) {
    throw new Error(
      "Dynamic require() in Bun.js currently only supports .node, .json, and .toml files.\n\tConsider using ESM import() instead."
    );
  }

  return this.require(resolved);
}

const main = {
  get() {
    return Bun.main;
  },
  set() {
    return false;
  },
  configurable: false,
};

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

  var lastSlash = filenameString.lastIndexOf(
    // TODO: WINDOWS
    // windows is more complicated here
    // but we don't support windows yet
    process.platform !== "win32" ? "/" : "\\"
  );
  var customImportMeta = {
    ...import.meta,
    path: filenameString,
    file:
      lastSlash > -1 ? filenameString.substring(lastSlash + 1) : filenameString,
    dir: lastSlash > -1 ? filenameString.substring(0, lastSlash) : "",
  };

  if (isURL) {
    customImportMeta.url = filename;
  } else {
    // lazy because URL is slow and also can throw
    Object.defineProperty(customImportMeta, "url", {
      get() {
        const value = new URL("file://" + customImportMeta.path).href;
        Object.defineProperty(customImportMeta, "url", {
          value,
        });
        return value;
      },
      configurable: true,
    });
  }

  var bound = require.bind(customImportMeta);
  bound.resolve = resolve.bind(customImportMeta);

  // do this one lazily
  Object.defineProperty(bound, "main", main);

  return bound;
}

// 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,
};