postcss-fontpath
Advanced tools
Comparing version
37
index.js
'use strict'; | ||
var fs = require('fs'); | ||
var objectAssign = require('object-assign'); | ||
var path = require('path'); | ||
var postcss = require('postcss'); | ||
var url = require('url'); | ||
module.exports = postcss.plugin('postcss-fontpath', function () { | ||
return function (css) { | ||
module.exports = postcss.plugin('postcss-fontpath', function (opts) { | ||
opts = objectAssign({ | ||
checkPath: false | ||
}, opts); | ||
return function (css, result) { | ||
// Loop through each @rule | ||
@@ -28,2 +36,16 @@ css.walkAtRules('font-face', function(rule) { | ||
if (opts.checkPath === true) { | ||
// Make the fontPath absolute and normalize it (removes the #iefix hash) | ||
var absoluteFontPath = url.parse(path.resolve(path.dirname(css.source.input.file), fontPath) + format.ext).pathname; | ||
try { | ||
fs.accessSync(absoluteFontPath, fs.F_OK); | ||
} catch (err) { | ||
decl.warn(result, 'Cannot find file "' + fontPath + format.ext + '"'); | ||
// Skip the format in the src output | ||
return; | ||
} | ||
} | ||
if (index === array.length - 1){ | ||
@@ -37,8 +59,11 @@ src += 'url("' + fontPath + format.ext + '") format(\'' + format.type + '\')'; | ||
// IE Fix src prop | ||
decl.cloneBefore({ prop: 'src', value: ieSrc }); | ||
if (src.length > 0) { | ||
// New src prop | ||
decl.cloneBefore({ prop: 'src', value: src }); | ||
// IE Fix src prop | ||
decl.cloneBefore({ prop: 'src', value: ieSrc }); | ||
// New src prop | ||
decl.cloneBefore({ prop: 'src', value: src }); | ||
} | ||
// Remove our custom decleration | ||
@@ -45,0 +70,0 @@ decl.remove(); |
{ | ||
"name": "postcss-fontpath", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "PostCSS plugin that adds a font-path attribute to @font-face which expands to the FontSpring syntax", | ||
@@ -29,2 +29,3 @@ "keywords": [ | ||
"dependencies": { | ||
"object-assign": "^4.0.1", | ||
"postcss": "^5.0.0" | ||
@@ -31,0 +32,0 @@ }, |
@@ -43,2 +43,10 @@ # PostCSS FontPath | ||
### Options | ||
**checkPath** (boolean, default = `false`) | ||
If true, the plugin will check if the path to the font file exists. When not found, the file is skipped in the output and a warning is logged. | ||
-- | ||
### License | ||
@@ -45,0 +53,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
5812
21.34%56
55.56%63
14.55%2
100%1
Infinity%+ Added
+ Added