You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

2 years ago
  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,
  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. }