const Module = module.constructor;
const Mp = Module.prototype;

Mp.resolve = function (id) {
  return Module._resolveFilename(id, this);
};

// Enable the module.{watch,export,...} runtime API needed by Reify.
require("reify/lib/runtime").enable(Mp);

const moduleLoad = Mp.load;
Mp.load = function (filename) {
  const result = moduleLoad.apply(this, arguments);
  if (typeof this.runSetters === "function") {
    // Make sure we call module.runSetters (or module.runModuleSetters, a
    // legacy synonym) whenever a module finishes loading.
    this.runSetters();
  }
  return result;
};

const resolved = Promise.resolve();
Mp.dynamicImport = function (id) {
  return resolved.then(() => require(id));
};

const babelParse = require("reify/lib/parsers/babel").parse;
const reifyCompile = require("reify/lib/compiler").compile;
const _compile = Mp._compile;
Mp._compile = function (content, filename) {
  const result = reifyCompile(content, {
    parse: babelParse,
    generateLetDeclarations: false,
    ast: false,
  });
  if (!result.identical) {
    content = result.code;
  }
  return _compile.call(this, content, filename);
};

const Module = module.constructor;
const Mp = Module.prototype;

Mp.resolve = function (id) {
  return Module._resolveFilename(id, this);
};

// Enable the module.{watch,export,...} runtime API needed by Reify.
require("reify/lib/runtime").enable(Mp);

const moduleLoad = Mp.load;
Mp.load = function (filename) {
  const result = moduleLoad.apply(this, arguments);
  if (typeof this.runSetters === "function") {
    // Make sure we call module.runSetters (or module.runModuleSetters, a
    // legacy synonym) whenever a module finishes loading.
    this.runSetters();
  }
  return result;
};

const resolved = Promise.resolve();
Mp.dynamicImport = function (id) {
  return resolved.then(() => require(id));
};

const parse = require("reify/lib/parsers/default").parse;
const reifyCompile = require("reify/lib/compiler").compile;
const _compile = Mp._compile;
Mp._compile = function (content, filename) {
  const result = reifyCompile(content, {
    parse: parse,
    generateLetDeclarations: false,
    ast: false,
  });
  if (!result.identical) {
    content = result.code;
  }
  return _compile.call(this, content, filename);
};

Use default parser for Reify Module.prototype._compile hook.
Use default parser for Reify Module.prototype._compile hook.

This saves us from having to install @babel/parser in the server bundle.

There is currently a bug in Reify that prevents it from loading this
parser by default, but we can work around that on the Meteor side for now.
