Font-Awesome/js-packages/@fortawesome/fontawesome-svg-core/import.macro.js

221 lines
7.9 KiB
JavaScript
Raw Normal View History

2022-02-08 04:16:51 +08:00
const { createMacro, MacroError } = require('babel-plugin-macros')
const { addNamed } = require('@babel/helper-module-imports')
const { FAFamilyIds, FAFamilyClassicId, FAFamilyDuotoneId } = require('@fortawesome-internal-tools/fontawesome-icons/canonical')
const { FALegacyStyleIds } = require('@fortawesome-internal-tools/fontawesome-icons/legacy')
2022-02-08 04:16:51 +08:00
module.exports = createMacro(importer, {
configName: 'fontawesome-svg-core'
})
const styles = FALegacyStyleIds
const macroNames = [...styles, 'icon']
const families = FAFamilyIds.map((family) => family.toLowerCase())
function importer({ references, state, babel, source, config }) {
const license = config !== undefined ? config.license : 'free'
2022-02-08 04:16:51 +08:00
if (!['free', 'pro'].includes(license)) {
throw new Error("config license must be either 'free' or 'pro'")
2022-02-08 04:16:51 +08:00
}
Object.keys(references).forEach((key) => {
replace({
2022-09-01 05:02:43 +08:00
macroName: key,
license,
2022-02-08 04:16:51 +08:00
references: references[key],
state,
babel,
source
})
})
}
function replace({ macroName, license, references, state, babel, source }) {
2022-02-08 04:16:51 +08:00
references.forEach((nodePath) => {
const { iconName, style, family } = resolveReplacement({ nodePath, babel, state, macroName })
2022-02-08 04:16:51 +08:00
2022-09-01 05:02:43 +08:00
const name = `fa${capitalize(camelCase(iconName))}`
const importFrom = getImport({ family, style, license, name })
2022-02-08 04:16:51 +08:00
2022-09-01 05:02:43 +08:00
const importName = addNamed(nodePath, name, importFrom)
nodePath.parentPath.replaceWith(importName)
2022-02-08 04:16:51 +08:00
})
}
function getImport({ family, style, license, name }) {
2022-09-01 05:02:43 +08:00
if (family) {
return `@fortawesome/${family.toLowerCase()}-${style}-svg-icons/${name}`
2022-11-16 02:34:54 +08:00
} else if (style === 'brands') {
return `@fortawesome/free-brands-svg-icons/${name}`
2022-09-01 05:02:43 +08:00
} else {
return `@fortawesome/${license}-${style}-svg-icons/${name}`
}
}
function resolveReplacement({ nodePath, babel, state, macroName }) {
if ('icon' === macroName) {
2022-09-01 05:02:43 +08:00
return resolveReplacementIcon({ nodePath, babel, state, macroName })
} else {
return resolveReplacementLegacyStyle({ nodePath, babel, state, macroName })
}
}
// The macros corresonding to legacy style names: solid(), regular(), light(), thin(), duotone(), brands().
function resolveReplacementLegacyStyle({ nodePath, babel, state, macroName }) {
2022-02-08 04:16:51 +08:00
const { types: t } = babel
const { parentPath } = nodePath
2022-09-01 05:02:43 +08:00
if (!styles.includes(macroName)) {
throw parentPath.buildCodeFrameError(`${macroName} is not a valid macro name. Use one of ${macroNames.join(', ')}`, MacroError)
2022-02-08 04:16:51 +08:00
}
if (parentPath.node.arguments) {
2022-09-01 05:02:43 +08:00
if (parentPath.node.arguments.length < 1) {
throw parentPath.buildCodeFrameError(`Received an invalid number of arguments for ${macroName} macro: must be exactly 1`, MacroError)
2022-09-01 05:02:43 +08:00
}
if (parentPath.node.arguments.length > 1) {
throw parentPath.buildCodeFrameError(`Received an invalid number of arguments for ${macroName} macro: must be exactly 1`, MacroError)
2022-02-08 04:16:51 +08:00
}
if (
(parentPath.node.arguments.length === 1 || parentPath.node.arguments.length === 2) &&
2022-02-08 04:16:51 +08:00
t.isStringLiteral(parentPath.node.arguments[0]) &&
nodePath.parentPath.node.arguments[0].value.startsWith('fa-')
) {
throw parentPath.buildCodeFrameError(`Don't begin the icon name with fa-, just use ${nodePath.parentPath.node.arguments[0].value.slice(3)}`, MacroError)
2022-02-08 04:16:51 +08:00
}
if ((parentPath.node.arguments.length === 1 || parentPath.node.arguments.length === 2) && !t.isStringLiteral(parentPath.node.arguments[0])) {
throw parentPath.buildCodeFrameError('Only string literals are supported when referencing icons (use a string here instead)', MacroError)
2022-02-08 04:16:51 +08:00
}
} else {
throw parentPath.buildCodeFrameError('Pass the icon name you would like to import as an argument.', MacroError)
2022-02-08 04:16:51 +08:00
}
2022-09-01 05:02:43 +08:00
return {
iconName: nodePath.parentPath.node.arguments[0].value,
style: macroName,
family: undefined
}
}
// The icon() macro.
function resolveReplacementIcon({ nodePath, babel, state, macroName }) {
2022-09-01 05:02:43 +08:00
const { types: t } = babel
const { parentPath } = nodePath
if ('icon' !== macroName) {
throw parentPath.buildCodeFrameError(`${macroName} is not a valid macro name. Use one of ${macroNames.join(', ')}`, MacroError)
2022-09-01 05:02:43 +08:00
}
if (parentPath.node.arguments.length !== 1) {
throw parentPath.buildCodeFrameError(`Received an invalid number of arguments for ${macroName} macro: must be exactly 1`, MacroError)
2022-09-01 05:02:43 +08:00
}
if (!t.isObjectExpression(parentPath.node.arguments[0])) {
throw parentPath.buildCodeFrameError(
"Only object expressions are supported when referencing icons with this macro, like this: { name: 'star' }",
2022-09-01 05:02:43 +08:00
MacroError
)
}
const properties = parentPath.node.arguments[0].properties || []
2022-09-01 05:02:43 +08:00
const namePropIndex = properties.findIndex((prop) => 'name' === prop.key.name)
const name = namePropIndex >= 0 ? getStringLiteralPropertyValue(t, parentPath, parentPath.node.arguments[0].properties[namePropIndex]) : undefined
2022-09-01 05:02:43 +08:00
if (!name) {
throw parentPath.buildCodeFrameError('The object argument to the icon() macro must have a name property', MacroError)
2022-09-01 05:02:43 +08:00
}
const stylePropIndex = properties.findIndex((prop) => 'style' === prop.key.name)
let style = stylePropIndex >= 0 ? getStringLiteralPropertyValue(t, parentPath, parentPath.node.arguments[0].properties[stylePropIndex]) : undefined
2022-09-01 05:02:43 +08:00
if (style && !styles.includes(style)) {
throw parentPath.buildCodeFrameError(`Invalid style name: ${style}. It must be one of the following: ${styles.join(', ')}`, MacroError)
2022-09-01 05:02:43 +08:00
}
const familyPropIndex = properties.findIndex((prop) => 'family' === prop.key.name)
let family = familyPropIndex >= 0 ? getStringLiteralPropertyValue(t, parentPath, parentPath.node.arguments[0].properties[familyPropIndex]) : undefined
2022-09-01 05:02:43 +08:00
if (family && !families.includes(family)) {
throw parentPath.buildCodeFrameError(`Invalid family name: ${family}. It must be one of the following: ${families.join(', ')}`, MacroError)
2022-09-01 05:02:43 +08:00
}
if (FAFamilyDuotoneId === style && family && FAFamilyClassicId !== family) {
throw parentPath.buildCodeFrameError(`duotone cannot be used as a style name with any family other than classic`, MacroError)
2022-09-01 05:02:43 +08:00
}
if ('brands' === style && family && FAFamilyClassicId !== family) {
throw parentPath.buildCodeFrameError(`brands cannot be used as a style name with any family other than classic`, MacroError)
2022-09-01 05:02:43 +08:00
}
if (family && !style) {
throw parentPath.buildCodeFrameError(`When a family is specified, a style must also be specified`, MacroError)
2022-09-01 05:02:43 +08:00
}
if (FAFamilyDuotoneId === style || FAFamilyDuotoneId === family) {
2022-09-01 05:02:43 +08:00
family = undefined
style = FAFamilyDuotoneId
2022-09-01 05:02:43 +08:00
}
if ('brands' === style) {
2022-09-01 05:02:43 +08:00
family = undefined
}
// defaults
if (!style) {
2022-09-01 05:02:43 +08:00
style = 'solid'
}
if (FAFamilyClassicId === family) {
2022-09-01 05:02:43 +08:00
family = undefined
}
return {
iconName: name,
family,
style
}
}
function getStringLiteralPropertyValue(t, parentPath, property) {
if (!('object' === typeof t && 'function' === typeof t.isStringLiteral)) {
throw Error('ERROR: invalid babel-types arg. This is probably a programming error in import.macro')
2022-09-01 05:02:43 +08:00
}
if (!('object' === typeof property && 'object' === typeof property.value && 'object' == typeof property.key)) {
throw Error('ERROR: invalid babel property arg. This is probably a programming error in import.macro')
2022-09-01 05:02:43 +08:00
}
if (!('object' === typeof parentPath && 'function' === typeof parentPath.buildCodeFrameError)) {
throw Error('ERROR: invalid babel parentPath arg. This is probably a programming error in import.macro')
2022-09-01 05:02:43 +08:00
}
if (!t.isStringLiteral(property.value)) {
throw parentPath.buildCodeFrameError(`Only string literals are supported for the ${property.key.name} property (use a string here instead)`, MacroError)
2022-09-01 05:02:43 +08:00
}
return property.value.value
2022-02-08 04:16:51 +08:00
}
function capitalize(str) {
2022-02-08 04:16:51 +08:00
return str[0].toUpperCase() + str.slice(1)
}
function camelCase(str) {
2022-02-08 04:16:51 +08:00
return str
.split('-')
.map((s, index) => {
return (index === 0 ? s[0].toLowerCase() : s[0].toUpperCase()) + s.slice(1).toLowerCase()
2022-02-08 04:16:51 +08:00
})
.join('')
}