wishthis/node_modules/rtlcss/lib/config.js

104 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict'
2022-06-08 10:36:39 +00:00
let options
const config = {}
const corePlugin = require('./plugin.js')
function sort (arr) {
return arr.sort((a, b) => a.priority - b.priority)
}
2022-01-21 08:28:41 +00:00
function optionOrDefault (option, def) {
return option in options ? options[option] : def
}
function addKey (key, def) {
config[key] = optionOrDefault(key, def)
}
function main (opts, plugins, hooks) {
options = opts || {}
hooks = hooks || {}
addKey('autoRename', false)
addKey('autoRenameStrict', false)
addKey('blacklist', {})
addKey('clean', true)
addKey('greedy', false)
addKey('processUrls', false)
addKey('stringMap', [])
addKey('useCalc', false)
2022-06-08 10:36:39 +00:00
addKey('aliases', {})
addKey('processEnv', true)
2022-01-21 08:28:41 +00:00
// default strings map
if (Array.isArray(config.stringMap)) {
2022-06-08 10:36:39 +00:00
let hasLeftRight, hasLtrRtl
for (const map of config.stringMap) {
2022-01-21 08:28:41 +00:00
if (hasLeftRight && hasLtrRtl) {
break
} else if (map.name === 'left-right') {
hasLeftRight = true
} else if (map.name === 'ltr-rtl') {
hasLtrRtl = true
}
}
2022-06-08 10:36:39 +00:00
2022-01-21 08:28:41 +00:00
if (!hasLeftRight) {
config.stringMap.push({
2022-06-08 10:36:39 +00:00
name: 'left-right',
priority: 100,
exclusive: false,
search: ['left', 'Left', 'LEFT'],
replace: ['right', 'Right', 'RIGHT'],
options: { scope: '*', ignoreCase: false }
2022-01-21 08:28:41 +00:00
})
}
2022-06-08 10:36:39 +00:00
2022-01-21 08:28:41 +00:00
if (!hasLtrRtl) {
config.stringMap.push({
2022-06-08 10:36:39 +00:00
name: 'ltr-rtl',
priority: 100,
exclusive: false,
search: ['ltr', 'Ltr', 'LTR'],
replace: ['rtl', 'Rtl', 'RTL'],
options: { scope: '*', ignoreCase: false }
2022-01-21 08:28:41 +00:00
})
}
2022-06-08 10:36:39 +00:00
sort(config.stringMap)
2022-01-21 08:28:41 +00:00
}
// plugins
config.plugins = []
if (Array.isArray(plugins)) {
2022-06-08 10:36:39 +00:00
if (!plugins.some((plugin) => plugin.name === 'rtlcss')) {
2022-01-21 08:28:41 +00:00
config.plugins.push(corePlugin)
}
2022-06-08 10:36:39 +00:00
2022-01-21 08:28:41 +00:00
config.plugins = config.plugins.concat(plugins)
} else if (!plugins || plugins.name !== 'rtlcss') {
config.plugins.push(corePlugin)
}
2022-06-08 10:36:39 +00:00
sort(config.plugins)
2022-01-21 08:28:41 +00:00
// hooks
2022-06-08 10:36:39 +00:00
config.hooks = {
pre () {},
post () {}
}
2022-01-21 08:28:41 +00:00
if (typeof hooks.pre === 'function') {
config.hooks.pre = hooks.pre
}
2022-06-08 10:36:39 +00:00
2022-01-21 08:28:41 +00:00
if (typeof hooks.post === 'function') {
config.hooks.post = hooks.post
}
return config
}
2022-06-08 10:36:39 +00:00
2022-01-21 08:28:41 +00:00
module.exports.configure = main