| 1 |
const { createMacro, MacroError } = require('babel-plugin-macros') |
| 2 |
const { addNamed } = require('@babel/helper-module-imports') |
| 3 |
const { |
| 4 |
FAFamilyIds, |
| 5 |
FAFamilyClassicId, |
| 6 |
FAFamilyDuotoneId |
| 7 |
} = require('@fortawesome-internal-tools/fontawesome-icons/canonical') |
| 8 |
const { FALegacyStyleIds } = require('@fortawesome-internal-tools/fontawesome-icons/legacy') |
| 9 |
|
| 10 |
module.exports = createMacro(importer, { |
| 11 |
configName: 'fontawesome-svg-core' |
| 12 |
}) |
| 13 |
|
| 14 |
const styles = FALegacyStyleIds |
| 15 |
|
| 16 |
const macroNames = [...styles, 'icon'] |
| 17 |
|
| 18 |
const families = FAFamilyIds.map((family) => family.toLowerCase()) |
| 19 |
|
| 20 |
function importer({ references, state, babel, source, config }) { |
| 21 |
const license = config !== undefined ? config.license : 'free' |
| 22 |
|
| 23 |
if (!['free', 'pro'].includes(license)) { |
| 24 |
throw new Error("config license must be either 'free' or 'pro'") |
| 25 |
} |
| 26 |
|
| 27 |
Object.keys(references).forEach((key) => { |
| 28 |
replace({ |
| 29 |
macroName: key, |
| 30 |
license, |
| 31 |
references: references[key], |
| 32 |
state, |
| 33 |
babel, |
| 34 |
source |
| 35 |
}) |
| 36 |
}) |
| 37 |
} |
| 38 |
|
| 39 |
function replace({ macroName, license, references, state, babel }) { |
| 40 |
references.forEach((nodePath) => { |
| 41 |
const { iconName, style, family } = resolveReplacement({ nodePath, babel, state, macroName }) |
| 42 |
|
| 43 |
const name = `fa${capitalize(camelCase(iconName))}` |
| 44 |
const importFrom = getImport({ family, style, license, name }) |
| 45 |
|
| 46 |
const importName = addNamed(nodePath, name, importFrom) |
| 47 |
|
| 48 |
nodePath.parentPath.replaceWith(importName) |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
function getImport({ family, style, license, name }) { |
| 53 |
if (family) { |
| 54 |
return `@fortawesome/${family.toLowerCase()}-${style}-svg-icons/${name}` |
| 55 |
} else if (style === 'brands') { |
| 56 |
return `@fortawesome/free-brands-svg-icons/${name}` |
| 57 |
} else { |
| 58 |
return `@fortawesome/${license}-${style}-svg-icons/${name}` |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
function resolveReplacement({ nodePath, babel, state, macroName }) { |
| 63 |
if ('icon' === macroName) { |
| 64 |
return resolveReplacementIcon({ nodePath, babel, state, macroName }) |
| 65 |
} else { |
| 66 |
return resolveReplacementLegacyStyle({ nodePath, babel, state, macroName }) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
function resolveReplacementLegacyStyle({ nodePath, babel, macroName }) { |
| 72 |
const { types: t } = babel |
| 73 |
const { parentPath } = nodePath |
| 74 |
|
| 75 |
if (!styles.includes(macroName)) { |
| 76 |
throw parentPath.buildCodeFrameError( |
| 77 |
`${macroName} is not a valid macro name. Use one of ${macroNames.join(', ')}`, |
| 78 |
MacroError |
| 79 |
) |
| 80 |
} |
| 81 |
|
| 82 |
if (parentPath.node.arguments) { |
| 83 |
if (parentPath.node.arguments.length < 1) { |
| 84 |
throw parentPath.buildCodeFrameError( |
| 85 |
`Received an invalid number of arguments for ${macroName} macro: must be exactly 1`, |
| 86 |
MacroError |
| 87 |
) |
| 88 |
} |
| 89 |
|
| 90 |
if (parentPath.node.arguments.length > 1) { |
| 91 |
throw parentPath.buildCodeFrameError( |
| 92 |
`Received an invalid number of arguments for ${macroName} macro: must be exactly 1`, |
| 93 |
MacroError |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
if ( |
| 98 |
(parentPath.node.arguments.length === 1 || parentPath.node.arguments.length === 2) && |
| 99 |
t.isStringLiteral(parentPath.node.arguments[0]) && |
| 100 |
nodePath.parentPath.node.arguments[0].value.startsWith('fa-') |
| 101 |
) { |
| 102 |
throw parentPath.buildCodeFrameError( |
| 103 |
`Don't begin the icon name with fa-, just use ${nodePath.parentPath.node.arguments[0].value.slice(3)}`, |
| 104 |
MacroError |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
if ( |
| 109 |
(parentPath.node.arguments.length === 1 || parentPath.node.arguments.length === 2) && |
| 110 |
!t.isStringLiteral(parentPath.node.arguments[0]) |
| 111 |
) { |
| 112 |
throw parentPath.buildCodeFrameError( |
| 113 |
'Only string literals are supported when referencing icons (use a string here instead)', |
| 114 |
MacroError |
| 115 |
) |
| 116 |
} |
| 117 |
} else { |
| 118 |
throw parentPath.buildCodeFrameError('Pass the icon name you would like to import as an argument.', MacroError) |
| 119 |
} |
| 120 |
|
| 121 |
return { |
| 122 |
iconName: nodePath.parentPath.node.arguments[0].value, |
| 123 |
style: macroName, |
| 124 |
family: undefined |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
function resolveReplacementIcon({ nodePath, babel, macroName }) { |
| 130 |
const { types: t } = babel |
| 131 |
const { parentPath } = nodePath |
| 132 |
|
| 133 |
if ('icon' !== macroName) { |
| 134 |
throw parentPath.buildCodeFrameError( |
| 135 |
`${macroName} is not a valid macro name. Use one of ${macroNames.join(', ')}`, |
| 136 |
MacroError |
| 137 |
) |
| 138 |
} |
| 139 |
|
| 140 |
if (parentPath.node.arguments.length !== 1) { |
| 141 |
throw parentPath.buildCodeFrameError( |
| 142 |
`Received an invalid number of arguments for ${macroName} macro: must be exactly 1`, |
| 143 |
MacroError |
| 144 |
) |
| 145 |
} |
| 146 |
|
| 147 |
if (!t.isObjectExpression(parentPath.node.arguments[0])) { |
| 148 |
throw parentPath.buildCodeFrameError( |
| 149 |
"Only object expressions are supported when referencing icons with this macro, like this: { name: 'star' }", |
| 150 |
MacroError |
| 151 |
) |
| 152 |
} |
| 153 |
|
| 154 |
const properties = parentPath.node.arguments[0].properties || [] |
| 155 |
|
| 156 |
const namePropIndex = properties.findIndex((prop) => 'name' === prop.key.name) |
| 157 |
|
| 158 |
const name = |
| 159 |
namePropIndex >= 0 |
| 160 |
? getStringLiteralPropertyValue(t, parentPath, parentPath.node.arguments[0].properties[namePropIndex]) |
| 161 |
: undefined |
| 162 |
|
| 163 |
if (!name) { |
| 164 |
throw parentPath.buildCodeFrameError( |
| 165 |
'The object argument to the icon() macro must have a name property', |
| 166 |
MacroError |
| 167 |
) |
| 168 |
} |
| 169 |
|
| 170 |
const stylePropIndex = properties.findIndex((prop) => 'style' === prop.key.name) |
| 171 |
|
| 172 |
let style = |
| 173 |
stylePropIndex >= 0 |
| 174 |
? getStringLiteralPropertyValue(t, parentPath, parentPath.node.arguments[0].properties[stylePropIndex]) |
| 175 |
: undefined |
| 176 |
|
| 177 |
if (style && !styles.includes(style)) { |
| 178 |
throw parentPath.buildCodeFrameError( |
| 179 |
`Invalid style name: ${style}. It must be one of the following: ${styles.join(', ')}`, |
| 180 |
MacroError |
| 181 |
) |
| 182 |
} |
| 183 |
|
| 184 |
const familyPropIndex = properties.findIndex((prop) => 'family' === prop.key.name) |
| 185 |
|
| 186 |
let family = |
| 187 |
familyPropIndex >= 0 |
| 188 |
? getStringLiteralPropertyValue(t, parentPath, parentPath.node.arguments[0].properties[familyPropIndex]) |
| 189 |
: undefined |
| 190 |
|
| 191 |
if (family && !families.includes(family)) { |
| 192 |
throw parentPath.buildCodeFrameError( |
| 193 |
`Invalid family name: ${family}. It must be one of the following: ${families.join(', ')}`, |
| 194 |
MacroError |
| 195 |
) |
| 196 |
} |
| 197 |
|
| 198 |
if (FAFamilyDuotoneId === style && family && FAFamilyClassicId !== family) { |
| 199 |
throw parentPath.buildCodeFrameError( |
| 200 |
`duotone cannot be used as a style name with any family other than classic`, |
| 201 |
MacroError |
| 202 |
) |
| 203 |
} |
| 204 |
|
| 205 |
if ('brands' === style && family && FAFamilyClassicId !== family) { |
| 206 |
throw parentPath.buildCodeFrameError( |
| 207 |
`brands cannot be used as a style name with any family other than classic`, |
| 208 |
MacroError |
| 209 |
) |
| 210 |
} |
| 211 |
|
| 212 |
if (family && !style) { |
| 213 |
throw parentPath.buildCodeFrameError(`When a family is specified, a style must also be specified`, MacroError) |
| 214 |
} |
| 215 |
|
| 216 |
if (FAFamilyDuotoneId === style || FAFamilyDuotoneId === family) { |
| 217 |
family = undefined |
| 218 |
style = FAFamilyDuotoneId |
| 219 |
} |
| 220 |
|
| 221 |
if ('brands' === style) { |
| 222 |
family = undefined |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if (!style) { |
| 227 |
style = 'solid' |
| 228 |
} |
| 229 |
|
| 230 |
if (FAFamilyClassicId === family) { |
| 231 |
family = undefined |
| 232 |
} |
| 233 |
|
| 234 |
return { |
| 235 |
iconName: name, |
| 236 |
family, |
| 237 |
style |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
function getStringLiteralPropertyValue(t, parentPath, property) { |
| 242 |
if (!('object' === typeof t && 'function' === typeof t.isStringLiteral)) { |
| 243 |
throw Error('ERROR: invalid babel-types arg. This is probably a programming error in import.macro') |
| 244 |
} |
| 245 |
|
| 246 |
if (!('object' === typeof property && 'object' === typeof property.value && 'object' == typeof property.key)) { |
| 247 |
throw Error('ERROR: invalid babel property arg. This is probably a programming error in import.macro') |
| 248 |
} |
| 249 |
|
| 250 |
if (!('object' === typeof parentPath && 'function' === typeof parentPath.buildCodeFrameError)) { |
| 251 |
throw Error('ERROR: invalid babel parentPath arg. This is probably a programming error in import.macro') |
| 252 |
} |
| 253 |
|
| 254 |
if (!t.isStringLiteral(property.value)) { |
| 255 |
throw parentPath.buildCodeFrameError( |
| 256 |
`Only string literals are supported for the ${property.key.name} property (use a string here instead)`, |
| 257 |
MacroError |
| 258 |
) |
| 259 |
} |
| 260 |
|
| 261 |
return property.value.value |
| 262 |
} |
| 263 |
|
| 264 |
function capitalize(str) { |
| 265 |
return str[0].toUpperCase() + str.slice(1) |
| 266 |
} |
| 267 |
|
| 268 |
function camelCase(str) { |
| 269 |
return str |
| 270 |
.split('-') |
| 271 |
.map((s, index) => { |
| 272 |
return (index === 0 ? s[0].toLowerCase() : s[0].toUpperCase()) + s.slice(1).toLowerCase() |
| 273 |
}) |
| 274 |
.join('') |
| 275 |
} |