diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:58:02 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-10-14 15:58:02 +0800 |
| commit | fee47f6414cc3ae9a88f3ee0ed20f1d6f2d9f556 (patch) | |
| tree | 2b99dd57fc676a3819ef3cc46ac43df0043c2bb3 /node_modules/postcss-fontpath/index.js | |
| parent | 0379526e20e1ff212df23e6326741aa9c06df204 (diff) | |
added plugin from postcss: postcss-fixes rucksack-css
Diffstat (limited to 'node_modules/postcss-fontpath/index.js')
| -rw-r--r-- | node_modules/postcss-fontpath/index.js | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/node_modules/postcss-fontpath/index.js b/node_modules/postcss-fontpath/index.js new file mode 100644 index 00000000..2fd38c5e --- /dev/null +++ b/node_modules/postcss-fontpath/index.js @@ -0,0 +1,88 @@ +'use strict'; + +var fs = require('fs'), + path = require('path'), + postcss = require('postcss'), + url = require('url'); + +var DEFAULT_OPTS = { + checkFiles: false, + ie8Fix: false, + formats: [ + { type: 'embedded-opentype', ext: 'eot' }, + { type: 'woff2', ext: 'woff2' }, + { type: 'woff', ext: 'woff' }, + { type: 'truetype', ext: 'ttf' }, + { type: 'svg', ext: 'svg'} + ] +}; + +module.exports = postcss.plugin('postcss-fontpath', function(opts) { + + var config = Object.assign({}, DEFAULT_OPTS, opts || {}); + + return function (css) { + // Loop through each @rule + css.walkAtRules('font-face', function(rule) { + + // Loop through each decleration in the rule + rule.walkDecls('font-path', function(decl) { + + // Replace single and double quotes with nothing + var fontPath = decl.value.replace(/"/g, '').replace(/'/g, ''), + fonts = [], + ieHack = false, + ext = ''; + + config.formats.forEach(function(format) { + + if (config.checkFiles) { + // Best guess at where our fonts might be relative to + var basePath = css.source.input.file || process.cwd(), + absoluteFontPath = url.parse(path.resolve(path.dirname(basePath), fontPath) + '.' + format.ext).pathname; + + try { + // Try to see if the font exists + fs.accessSync(absoluteFontPath, fs.F_OK); + } catch (err) { + return; + } + } + + // Set the ext var + ext = format.ext; + + if (ext === 'eot' && config.ie8Fix) { + ieHack = true; + ext = 'eot?#iefix'; + } + + // Add the font to the font-face decl + fonts.push('url("' + fontPath + '.' + ext + '") format(\'' + format.type + '\')'); + }); + + if (fonts.length > 0) { + + // If the EOT exists, add the fallback + if (ieHack && config.ie8Fix) { + decl.cloneBefore({ + prop: 'src', + value: 'url("' + fontPath + '.eot")' + }); + } + + // Implode the rest of the fonts + decl.cloneBefore({ + prop: 'src', + value: fonts.join(',\n ') + }); + } + + // Remove our custom decleration + decl.remove(); + + }); + + }); + }; +}); |
