fontgen-loader
Advanced tools
Comparing version 0.1.7 to 0.1.8
150
index.js
var loaderUtils = require("loader-utils"); | ||
var fontgen = require("webfonts-generator"); | ||
var path = require("path"); | ||
var glob = require('glob'); | ||
@@ -9,35 +10,86 @@ var mimeTypes = { | ||
'ttf': 'application/x-font-ttf', | ||
'woff': 'application/font-woff', | ||
'woff': 'application/font-woff' | ||
}; | ||
function absolute(from, to) { | ||
if (arguments.length < 2) { | ||
return function (to) { | ||
return path.resolve(from, to); | ||
}; | ||
} | ||
return path.resolve(from, to); | ||
} | ||
module.exports = function() { | ||
function getFilesAndDeps(patterns, context) { | ||
var files = []; | ||
var filesDeps = []; | ||
var directoryDeps = []; | ||
function addFile(file) { | ||
filesDeps.push(file); | ||
files.push(absolute(context, file)); | ||
} | ||
function addByGlob(globExp) { | ||
var globOptions = {cwd: context}; | ||
var foundFiles = glob.sync(globExp, globOptions); | ||
files = files.concat(foundFiles.map(absolute(context))); | ||
var globDirs = glob.sync(path.dirname(globExp) + '/', globOptions); | ||
directoryDeps = directoryDeps.concat(globDirs.map(absolute(context))); | ||
} | ||
// Re-work the files array. | ||
patterns.forEach(function (pattern) { | ||
if (glob.hasMagic(pattern)) { | ||
addByGlob(pattern); | ||
} | ||
else { | ||
addFile(pattern); | ||
} | ||
}); | ||
return { | ||
files: files, | ||
dependencies: { | ||
directories: directoryDeps, | ||
files: filesDeps | ||
} | ||
}; | ||
} | ||
module.exports = function (content) { | ||
this.cacheable(); | ||
var params = loaderUtils.parseQuery(this.query); | ||
var config = require(this.resourcePath); | ||
var config; | ||
try { | ||
config = JSON.parse(content); | ||
} | ||
catch (ex) { | ||
config = this.exec(content, this.resourcePath); | ||
} | ||
config.__dirname = path.dirname(this.resourcePath); | ||
var relativate = function(file) { | ||
if(path.resolve(file) === path.normalize(file)){ | ||
// Absolute path. | ||
return file; | ||
} else { | ||
// Relative to the config file. | ||
return path.join(config.__dirname, file); | ||
} | ||
}; | ||
// Sanity check | ||
/* | ||
if(typeof config.fontName != "string" || typeof config.files != "array") { | ||
this.reportError("Typemismatch in your config. Verify your config for correct types."); | ||
return false; | ||
} | ||
*/ | ||
if(typeof config.fontName != "string" || typeof config.files != "array") { | ||
this.reportError("Typemismatch in your config. Verify your config for correct types."); | ||
return false; | ||
} | ||
*/ | ||
var filesAndDeps = getFilesAndDeps(config.files, this.context); | ||
filesAndDeps.dependencies.files.forEach(this.addDependency.bind(this)); | ||
filesAndDeps.dependencies.directories.forEach(this.addContextDependency.bind(this)); | ||
config.files = filesAndDeps.files; | ||
// Re-work the files array. | ||
for(var k in config.files) config.files[k] = relativate(config.files[k]); | ||
// With everything set up, let's make an ACTUAL config. | ||
var formats = params.types || ['eot', 'woff', 'ttf', 'svg']; | ||
if(formats.constructor !== Array) formats = [formats]; | ||
if (formats.constructor !== Array) { | ||
formats = [formats]; | ||
} | ||
@@ -54,3 +106,3 @@ var fontconf = { | ||
}, | ||
rename: (typeof config.rename == "function" ? config.rename : function(f){ | ||
rename: (typeof config.rename == "function" ? config.rename : function (f) { | ||
return path.basename(f, ".svg"); | ||
@@ -62,8 +114,8 @@ }), | ||
if(config.cssTemplate) { | ||
fontconf.cssTemplate = relativate(config.cssTemplate); | ||
if (config.cssTemplate) { | ||
fontconf.cssTemplate = absolute(this.context, config.cssTemplate); | ||
} | ||
for(option in config.templateOptions) { | ||
if( config.templateOptions.hasOwnProperty(option) ) { | ||
for (var option in config.templateOptions) { | ||
if (config.templateOptions.hasOwnProperty(option)) { | ||
fontconf.templateOptions[option] = config.templateOptions[option]; | ||
@@ -82,4 +134,4 @@ } | ||
]; | ||
for(var x in keys) { | ||
if(typeof config[keys[x]] != "undefined") { | ||
for (var x in keys) { | ||
if (typeof config[keys[x]] != "undefined") { | ||
fontconf[keys[x]] = config[keys[x]]; | ||
@@ -93,22 +145,28 @@ } | ||
var pub = ( | ||
opts.output.publicPath || "/" | ||
opts.output.publicPath || "/" | ||
); | ||
var embed = !!params.embed | ||
var embed = !!params.embed; | ||
fontgen(fontconf, function(err, res){ | ||
if(err) cb(err); | ||
if (fontconf.cssTemplate) { | ||
this.addDependency(fontconf.cssTemplate) | ||
} | ||
fontgen(fontconf, function (err, res) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
var urls = {}; | ||
for(var i in formats) { | ||
for (var i in formats) { | ||
var format = formats[i]; | ||
if(!embed) { | ||
if (!embed) { | ||
var filename = config.fileName || params.fileName || "[hash]-[fontname][ext]"; | ||
filename = filename | ||
.replace("[fontname]",fontconf.fontName) | ||
.replace("[ext]", "."+format); | ||
.replace("[fontname]", fontconf.fontName) | ||
.replace("[ext]", "." + format); | ||
var url = loaderUtils.interpolateName(this, | ||
filename, | ||
{ | ||
context: self.options.context || this.context, | ||
content: res[format] | ||
} | ||
filename, | ||
{ | ||
context: self.options.context || this.context, | ||
content: res[format] | ||
} | ||
); | ||
@@ -119,5 +177,5 @@ urls[format] = path.join(pub, url); | ||
urls[format] = 'data:' | ||
+ mimeTypes[format] | ||
+ ';charset=utf-8;base64,' | ||
+ (new Buffer(res[format]).toString('base64')); | ||
+ mimeTypes[format] | ||
+ ';charset=utf-8;base64,' | ||
+ (new Buffer(res[format]).toString('base64')); | ||
} | ||
@@ -127,2 +185,2 @@ } | ||
}); | ||
} | ||
}; |
{ | ||
"name": "fontgen-loader", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "A WebPack loader to automaticaly generate font files and CSS to make your own icon font", | ||
@@ -24,3 +24,4 @@ "repository": "DragonsInn/fontgen-loader", | ||
"loader-utils": "^0.2.10", | ||
"webfonts-generator": "^0.3.0" | ||
"webfonts-generator": "^0.3.0", | ||
"glob": "^6.0.2" | ||
}, | ||
@@ -30,3 +31,2 @@ "devDependencies": { | ||
"file-loader": "^0.8.4", | ||
"glob": "^5.0.10", | ||
"node-libs-browser": "^0.5.2", | ||
@@ -33,0 +33,0 @@ "style-loader": "^0.12.3", |
@@ -90,3 +90,3 @@ # `fontgen-loader` - Bam, easy webfonts! | ||
- `files`, Array | ||
An array of SVG icon files. | ||
An array of SVG icon files. Supports glob | ||
@@ -93,0 +93,0 @@ - `fontName`, String |
@@ -1,3 +0,13 @@ | ||
var font = require("./octicons.font"); | ||
require("./octicons.font"); | ||
require("./octicon2/octicon2.font"); | ||
require("./octicons-json.font"); | ||
console.log(font); | ||
document.write('<span class="octicons octicons-alert"></span> '); | ||
document.write('<span class="octicon2 octicon2-alignment-align"></span> '); | ||
document.write('<span class="octicon2 octicon2-arrow-right"></span> '); | ||
document.write('<span class="octicon2 octicon2-arrow-right2"></span> '); | ||
document.write('<span class="octicon2 octicon2-arrow-left"></span> '); | ||
document.write('<span class="octicon2 octicon2-alignment-aligned-to"></span> '); | ||
document.write('<span class="octicon octicon-alert"></span> '); | ||
document.write('<span class="octicon-json octicon-json-alert"></span> '); | ||
@@ -0,6 +1,8 @@ | ||
var path = require('path'); | ||
module.exports = { | ||
context: path.resolve(__dirname), | ||
entry: "./entry.js", | ||
output: { | ||
filename: "bundle.js", | ||
path: "./build" | ||
path: path.join(__dirname, 'build') | ||
}, | ||
@@ -10,5 +12,5 @@ module: { | ||
{ | ||
test: /\.font\.js$/, | ||
loader: "style!css!"+require.resolve("../") | ||
},{ | ||
test: /\.font\.(js|json)$/, | ||
loader: "style!css!" + require.resolve("../") | ||
}, { | ||
test: /\.(woff|eot|ttf|svg)$/, | ||
@@ -19,2 +21,2 @@ loader: "url" | ||
} | ||
} | ||
}; |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
366029
6
210
519
1
3
+ Addedglob@^6.0.2
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedglob@6.0.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedwrappy@1.0.2(transitive)