config.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict'
  2. var options
  3. var config = {}
  4. var corePlugin = require('./plugin.js')
  5. function optionOrDefault (option, def) {
  6. return option in options ? options[option] : def
  7. }
  8. function addKey (key, def) {
  9. config[key] = optionOrDefault(key, def)
  10. }
  11. function main (opts, plugins, hooks) {
  12. options = opts || {}
  13. hooks = hooks || {}
  14. addKey('autoRename', false)
  15. addKey('autoRenameStrict', false)
  16. addKey('blacklist', {})
  17. addKey('clean', true)
  18. addKey('greedy', false)
  19. addKey('processUrls', false)
  20. addKey('stringMap', [])
  21. addKey('useCalc', false)
  22. // default strings map
  23. if (Array.isArray(config.stringMap)) {
  24. var hasLeftRight, hasLtrRtl
  25. for (var x = 0; x < config.stringMap.length; x++) {
  26. var map = config.stringMap[x]
  27. if (hasLeftRight && hasLtrRtl) {
  28. break
  29. } else if (map.name === 'left-right') {
  30. hasLeftRight = true
  31. } else if (map.name === 'ltr-rtl') {
  32. hasLtrRtl = true
  33. }
  34. }
  35. if (!hasLeftRight) {
  36. config.stringMap.push({
  37. 'name': 'left-right',
  38. 'priority': 100,
  39. 'exclusive': false,
  40. 'search': ['left', 'Left', 'LEFT'],
  41. 'replace': ['right', 'Right', 'RIGHT'],
  42. 'options': { 'scope': '*', 'ignoreCase': false }
  43. })
  44. }
  45. if (!hasLtrRtl) {
  46. config.stringMap.push({
  47. 'name': 'ltr-rtl',
  48. 'priority': 100,
  49. 'exclusive': false,
  50. 'search': ['ltr', 'Ltr', 'LTR'],
  51. 'replace': ['rtl', 'Rtl', 'RTL'],
  52. 'options': { 'scope': '*', 'ignoreCase': false }
  53. })
  54. }
  55. config.stringMap.sort(function (a, b) { return a.priority - b.priority })
  56. }
  57. // plugins
  58. config.plugins = []
  59. if (Array.isArray(plugins)) {
  60. if (!plugins.some(function (plugin) { return plugin.name === 'rtlcss' })) {
  61. config.plugins.push(corePlugin)
  62. }
  63. config.plugins = config.plugins.concat(plugins)
  64. } else if (!plugins || plugins.name !== 'rtlcss') {
  65. config.plugins.push(corePlugin)
  66. }
  67. config.plugins.sort(function (a, b) { return a.priority - b.priority })
  68. // hooks
  69. config.hooks = { pre: function () {}, post: function () {} }
  70. if (typeof hooks.pre === 'function') {
  71. config.hooks.pre = hooks.pre
  72. }
  73. if (typeof hooks.post === 'function') {
  74. config.hooks.post = hooks.post
  75. }
  76. return config
  77. }
  78. module.exports.configure = main