requirepaths.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Copyright (c) 2014 Google Inc. All rights reserved.
  3. Use of this source code is governed by the MIT License, available in this package's LICENSE file
  4. or at http://opensource.org/licenses/MIT.
  5. */
  6. const path = require('path');
  7. function resolvePaths({filepath}, paths) {
  8. if (!paths) {
  9. return [];
  10. }
  11. return paths.slice(0).map(p => path.resolve(filepath, p));
  12. }
  13. function requirePaths(parentModule, opts) {
  14. const result = {
  15. before: [],
  16. after: []
  17. };
  18. if (!parentModule) {
  19. return result;
  20. }
  21. if (Array.isArray(opts)) {
  22. result.before = resolvePaths(parentModule, opts);
  23. } else {
  24. result.before = resolvePaths(parentModule, opts.before);
  25. result.after = resolvePaths(parentModule, opts.after);
  26. }
  27. return result;
  28. }
  29. exports.before = function before(targetPath, parentModule, opts) {
  30. const resolvedPaths = requirePaths(parentModule, opts);
  31. return `module.paths = ${JSON.stringify(resolvedPaths.before)}.concat(module.paths)` +
  32. `.concat(${JSON.stringify(resolvedPaths.after)}); `;
  33. };
  34. exports.after = function after() {
  35. return '';
  36. };