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.

51 lines
1.3 KiB

4 years ago
  1. var camelCase = require("lodash/camelCase");
  2. function dashesCamelCase(str) {
  3. return str.replace(/-+(\w)/g, function(match, firstLetter) {
  4. return firstLetter.toUpperCase();
  5. });
  6. }
  7. module.exports = function compileExports(result, importItemMatcher, camelCaseKeys) {
  8. if (!Object.keys(result.exports).length) {
  9. return "";
  10. }
  11. var exportJs = Object.keys(result.exports).reduce(function(res, key) {
  12. var valueAsString = JSON.stringify(result.exports[key]);
  13. valueAsString = valueAsString.replace(result.importItemRegExpG, importItemMatcher);
  14. function addEntry(k) {
  15. res.push("\t" + JSON.stringify(k) + ": " + valueAsString);
  16. }
  17. var targetKey;
  18. switch(camelCaseKeys) {
  19. case true:
  20. addEntry(key);
  21. targetKey = camelCase(key);
  22. if (targetKey !== key) {
  23. addEntry(targetKey);
  24. }
  25. break;
  26. case 'dashes':
  27. addEntry(key);
  28. targetKey = dashesCamelCase(key);
  29. if (targetKey !== key) {
  30. addEntry(targetKey);
  31. }
  32. break;
  33. case 'only':
  34. addEntry(camelCase(key));
  35. break;
  36. case 'dashesOnly':
  37. addEntry(dashesCamelCase(key));
  38. break;
  39. default:
  40. addEntry(key);
  41. break;
  42. }
  43. return res;
  44. }, []).join(",\n");
  45. return "{\n" + exportJs + "\n}";
  46. };