calckey/webpack/module/rules/fa.ts

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-12-07 17:44:50 +00:00
/**
* Replace fontawesome symbols
*/
const StringReplacePlugin = require('string-replace-webpack-plugin');
2017-12-08 04:10:31 +00:00
import * as fontawesome from '@fortawesome/fontawesome';
import * as regular from '@fortawesome/fontawesome-free-regular';
import * as solid from '@fortawesome/fontawesome-free-solid';
import * as brands from '@fortawesome/fontawesome-free-brands';
2017-12-07 17:44:50 +00:00
2017-12-08 04:10:31 +00:00
// Add icons
fontawesome.library.add(regular);
2017-12-07 17:44:50 +00:00
fontawesome.library.add(solid);
2017-12-08 04:10:31 +00:00
fontawesome.library.add(brands);
2017-12-07 17:44:50 +00:00
export default () => ({
enforce: 'pre',
test: /\.(tag|js|ts)$/,
exclude: /node_modules/,
loader: StringReplacePlugin.replace({
replacements: [{
pattern: /%fa:(.+?)%/g, replacement: (_, key) => {
const args = key.split(' ');
let prefix = 'fas';
2017-12-08 04:10:31 +00:00
const classes = [];
2017-12-07 17:44:50 +00:00
let transform = '';
let name;
args.forEach(arg => {
2017-12-08 04:10:31 +00:00
if (arg == 'R' || arg == 'S' || arg == 'B') {
2017-12-07 17:44:50 +00:00
prefix =
arg == 'R' ? 'far' :
arg == 'S' ? 'fas' :
2017-12-08 04:10:31 +00:00
arg == 'B' ? 'fab' :
2017-12-07 17:44:50 +00:00
'';
} else if (arg[0] == '.') {
2017-12-08 04:10:31 +00:00
classes.push('fa-' + arg.substr(1));
2017-12-07 17:44:50 +00:00
} else if (arg[0] == '-') {
transform = arg.substr(1).split('|').join(' ');
} else {
name = arg;
}
});
2017-12-08 04:10:31 +00:00
const icon = fontawesome.icon({ prefix, iconName: name }, {
classes: classes
});
2017-12-07 17:44:50 +00:00
if (icon) {
icon.transform = fontawesome.parse.transform(transform);
return `<i data-fa style="display:inline-block" class="${name}">${icon.html[0]}</i>`;
} else {
console.warn(`'${name}' not found in fa`);
return '';
}
}
}]
})
});