sass.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var empty = require('is-empty-object');
  2. var format = require('util').format;
  3. var stripIndent = require('strip-indent');
  4. var SASS_TEMPLATE = stripIndent(`
  5. @charset 'utf-8';
  6. // Variables go here
  7. %s
  8. // Core imports go here
  9. @import 'foundation';
  10. @import 'motion-ui';
  11. // Modules go here
  12. %s
  13. // Motion UI goes here
  14. @include motion-ui-transitions;
  15. @include motion-ui-animations;
  16. `);
  17. /**
  18. * Generates an entry point Sass file with a custom list of CSS exports and Sass variables.
  19. * @param {Object} config - Customizer configuration object.
  20. * @param {String[]} modules - Modules to include CSS for.
  21. * @param {Object} variables - Sass variable overrides to include. The key is the name of the variable, and the value is the value.
  22. * @returns {String} Formatted Sass file.
  23. */
  24. module.exports = function(config, modules, variables) {
  25. var variableList = [];
  26. var colorList = {};
  27. var exportList = ['@include foundation-global-styles;'];
  28. if (empty(modules)) {
  29. modules = Object.keys(config);
  30. }
  31. // Create variable overrides code
  32. for (var i in variables) {
  33. var name = i.replace('_', '-');
  34. if (name.match(/-color$/)) {
  35. var key = name.replace('-color', '');
  36. colorList[key] = variables[i];
  37. }
  38. else {
  39. variableList.push(format('$%s: %s;', name, variables[i]));
  40. }
  41. }
  42. if (!empty(colorList)) {
  43. variableList.push(createPaletteMap(colorList));
  44. }
  45. // Create module exports with @include
  46. for (var i in modules) {
  47. var name = modules[i];
  48. if (config[name] && config[name].sass) {
  49. exportList.push(format('@include foundation-%s;', config[name].sass));
  50. }
  51. }
  52. return format(SASS_TEMPLATE, variableList.join('\n'), exportList.join('\n'))
  53. }
  54. function createPaletteMap(colors) {
  55. var output = '$foundation-palette: (%s\n);';
  56. var keys = '';
  57. for (var i in colors) {
  58. keys += format('\n %s: %s,', i, colors[i]);
  59. }
  60. return format(output, keys);
  61. }