sunflowerdeath / webfonts-generator

Generator of webfonts from svg icons
The Unlicense
271 stars 152 forks source link

Don't issue files, but rather strings or bufers #2

Closed IngwiePhoenix closed 9 years ago

IngwiePhoenix commented 9 years ago

When being done with building the fonts, there should be an option to have a results object in the finalizing callback, that would allow the user to choose to write the output to another location or return it, in case of a WebPack loader. Here is an example on how this would be useful:

var gen = require("webfonts-generator");
var glob = require("glob");
var path = require("path");
var utils = require("loader-utils");

function toPublic(content) {
    // This is copied from file-loader:
    var query = utils.parseQuery(this.query);
    var url = utils.interpolateName(this, "[hash].[ext]", {
        context: query.context || this.options.context,
        content: content,
        regExp: query.regExp
    });
    this.emitFile(url, content);
    return url;
}

module.exports = function(source, map) {
    // the source is the actual data, but we don't want that, but an object.
    var fontData = require(this.resourcePath);
    var next = this.async();
    gen({
        files: glob(fontData.path),
        writeFiles: false, // <---
        cssFontsPath: fontData.url,
        types: ["ttf","woff","eot","svg"],
        rename: function(n) { return path.basename(n, path.extname(n)); }
    }, function(error, result){ // <---
        if(error) next(error);
        // emit the font files, thus turning it into a path relative to the root.
        // We then generate an URL for the public.    
        var font = {
            eof: makePublic.call(this, result.eof),
            ttf: makePublic.call(this, result.ttf),
            woff: makePublic.call(this, result.woff),
            svg: makePublic.call(this, result.svg)
        }
        var css = result.generateCss(font);

        // Return the CSS for further processing
        cb(null, css);
    });
}

As you can see here, a font config is loaded and then processed, turned into a proper font and then emitted to the webpack. Do you think this would be possible?

As a little bonus, I have also added the idea to have the result object also get a function to make the CSS with the generated URLs.

BTW, if the functionality was given, this code would already work! :)

Kind regards, Ingwie.

sunflowerdeath commented 9 years ago

Thank you for the feedback, I will look soon what I can do with this.

sunflowerdeath commented 9 years ago

Added in version 0.3.0 https://github.com/sunflowerdeath/webfonts-generator#writefiles

IngwiePhoenix commented 9 years ago

That is amazing! Working on the loader now. Thank you a lot for implementing this for me. :)