const { extname, dirname, join, relative } = require('path');
const finder = require('find-package-json');

module.exports = (originalPath, filePath) => {
  // FIXME For now in ESM we need to disable loading prop-types. Now that it's
  // MIT we can fork and rebrand it as diffhtml-prop-types.
  if (originalPath.indexOf('prop-types') === 0) {
    return './fake-prop-types.js';
  }

  if (originalPath.indexOf('diffhtml') !== 0) {
    // Ensure this path has a trailing extension.
    if (!extname(originalPath)) {
      originalPath += '.js';
    }

    return originalPath;
  }

  return originalPath.replace(/diffhtml(.*)\/lib\/(.*)|diffhtml(.*)?/, (full, sub, file, sub2) => {
    const path = dirname(finder(filePath).next().value.__path);
    let newPath = null;

    if (sub && file) {
      newPath = `diffhtml${sub}/${file}`;
    }
    else if (file) {
      newPath = `diffhtml/${file}`;
    }
    else if (sub2) {
      const [ name, ...path ] = sub2.split('/');
      newPath = `diffhtml${name}/${path.length ? path.join('/') : ''}`;
    }
    else {
      newPath = 'diffhtml';
    }

    return newPath;
  });
}

@@ -399,34 +399,46 @@
 Path
-.indexOf('diffhtml') !== 0
+%5B0%5D === '.' %7C%7C originalPath%5B0%5D === '/'
 ) %7B%0A

Support all modules in ESM babel preset