config.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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) {
  12. options = opts || {}
  13. addKey('autoRename', false)
  14. addKey('autoRenameStrict', false)
  15. addKey('blacklist', {})
  16. addKey('clean', true)
  17. addKey('greedy', false)
  18. addKey('processUrls', false)
  19. addKey('stringMap', [])
  20. // default strings map
  21. if (Array.isArray(config.stringMap)) {
  22. var hasLeftRight, hasLtrRtl
  23. for (var x = 0; x < config.stringMap.length; x++) {
  24. var map = config.stringMap[x]
  25. if (hasLeftRight && hasLtrRtl) {
  26. break
  27. } else if (map.name === 'left-right') {
  28. hasLeftRight = true
  29. } else if (map.name === 'ltr-rtl') {
  30. hasLtrRtl = true
  31. }
  32. }
  33. if (!hasLeftRight) {
  34. config.stringMap.push({
  35. 'name': 'left-right',
  36. 'priority': 100,
  37. 'exclusive': false,
  38. 'search': ['left', 'Left', 'LEFT'],
  39. 'replace': ['right', 'Right', 'RIGHT'],
  40. 'options': { 'scope': '*', 'ignoreCase': false }
  41. })
  42. }
  43. if (!hasLtrRtl) {
  44. config.stringMap.push({
  45. 'name': 'ltr-rtl',
  46. 'priority': 100,
  47. 'exclusive': false,
  48. 'search': ['ltr', 'Ltr', 'LTR'],
  49. 'replace': ['rtl', 'Rtl', 'RTL'],
  50. 'options': { 'scope': '*', 'ignoreCase': false }
  51. })
  52. }
  53. config.stringMap.sort(function (a, b) { return a.priority - b.priority })
  54. }
  55. // plugins
  56. config.plugins = []
  57. if (Array.isArray(plugins)) {
  58. if (!plugins.some(function (plugin) { return plugin.name === 'rtlcss' })) {
  59. config.plugins.push(corePlugin)
  60. }
  61. config.plugins = config.plugins.concat(plugins)
  62. } else if (!plugins || plugins.name !== 'rtlcss') {
  63. config.plugins.push(corePlugin)
  64. }
  65. config.plugins.sort(function (a, b) { return a.priority - b.priority })
  66. return config
  67. }
  68. module.exports.configure = main