summaryrefslogtreecommitdiff
path: root/scripts/test/node-esm-loader.mjs
blob: ad58c0618585ccc600b9be252a4058383a0f458f (plain)
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
// originally lifted from https://nodejs.org/api/esm.html

import path from 'path';
import process from 'process';

const baseURL = new URL('file://');
const binaryen_root = path.dirname(path.dirname(process.cwd()));
baseURL.pathname = `${binaryen_root}/`;

const specialTestSuiteModules = {
  'spectest': {
    url: new URL('scripts/test/spectest.js', baseURL).href,
    format: 'module'
  },
  'env': {
    url: new URL('scripts/test/env.js', baseURL).href,
    format: 'module'
  },
  'mod.ule': {
    url: new URL('scripts/test/mod.ule.js', baseURL).href,
    format: 'module'
  }
};

export async function resolve(specifier, context, defaultResolve) {
  const specialModule = specialTestSuiteModules[specifier];
  if (specialModule) {
    // This is needed for newer node versions.
    specialModule.shortCircuit = true;
    return specialModule;
  }
  return defaultResolve(specifier, context, defaultResolve);
}

export async function getFormat(url, context, defaultGetFormat) {
  const specifiers = Object.keys(specialTestSuiteModules);
  for (let i = 0, k = specifiers.length; i < k; ++i) {
    const specialModule = specialTestSuiteModules[specifiers[i]];
    if (specialModule.url == url) {
      return specialModule;
    }
  }
  return defaultGetFormat(url, context, defaultGetFormat);
}