Last active
February 3, 2020 03:51
-
-
Save mutoo/159e9fd618b9b82908edbdb3e3e025af to your computer and use it in GitHub Desktop.
the icon-font-loader that parses the key/value pairs from the font style.css into an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { getOptions } = require('loader-utils'); | |
const validateOptions = require('schema-utils'); | |
const css = require('css'); | |
const schema = require('./options.json'); | |
/** | |
* This loader parses the key/value pairs from the font style.css into an array | |
*/ | |
module.exports = function loader(source) { | |
const options = getOptions(this) || {}; | |
validateOptions(schema, options, 'Racing Icon Loader'); | |
let charsets = []; | |
try { | |
const { prefix } = options; | |
const regKey = new RegExp(`\\.${prefix}([a-zA-Z0-9-]+)::?before`); | |
const regValue = new RegExp(/"(\\\w+)"/); | |
const ast = css.parse(source); | |
charsets = ast.stylesheet.rules | |
.filter(r => r.type === 'rule' && r.selectors[0].startsWith(`.${prefix}`)) | |
.map(r => { | |
const selector = r.selectors[0]; | |
const key = selector.match(regKey)[1]; | |
const content = r.declarations.find(d => d.property === 'content'); | |
const value = content.value.match(regValue)[1]; | |
return { key, value }; | |
}); | |
} catch (error) { | |
this.emitError(error); | |
} | |
return `module.exports = ${JSON.stringify(charsets)};`; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "object", | |
"properties": { | |
"prefix": { | |
"type": "string" | |
} | |
}, | |
"additionalProperties": false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment