resolve-id.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict"
  2. // external tooling
  3. const resolve = require("resolve")
  4. const moduleDirectories = ["web_modules", "node_modules"]
  5. function resolveModule(id, opts) {
  6. return new Promise((res, rej) => {
  7. resolve(id, opts, (err, path) => (err ? rej(err) : res(path)))
  8. })
  9. }
  10. module.exports = function(id, base, options) {
  11. const paths = options.path
  12. const resolveOpts = {
  13. basedir: base,
  14. moduleDirectory: moduleDirectories.concat(options.addModulesDirectories),
  15. paths: paths,
  16. extensions: [".css"],
  17. packageFilter: function processPackage(pkg) {
  18. if (pkg.style) pkg.main = pkg.style
  19. else if (!pkg.main || !/\.css$/.test(pkg.main)) pkg.main = "index.css"
  20. return pkg
  21. },
  22. preserveSymlinks: false,
  23. }
  24. return resolveModule(`./${id}`, resolveOpts)
  25. .catch(() => resolveModule(id, resolveOpts))
  26. .catch(() => {
  27. if (paths.indexOf(base) === -1) paths.unshift(base)
  28. throw new Error(
  29. `Failed to find '${id}'
  30. in [
  31. ${paths.join(",\n ")}
  32. ]`
  33. )
  34. })
  35. }