benbria / aliasify

Rewrite require calls in browserify modules.
MIT License
204 stars 26 forks source link

npm publish #2

Closed bodokaiser closed 10 years ago

bodokaiser commented 10 years ago

Hello @benbria

I would like to use aliasify but did not found it in the npm repository. Could you publish it?

PS: I would prefer just passing a plain js hash to the transform as I it makes independent from how you handle your configuration:

var builder = browserify();

builder.transform(require('./aliasify')(app.settings.aliasify));

//...

Best, Bo

jwalton commented 10 years ago

Oops! I'll take care of this today. I'm at an airport right now, but I should have a moment to do this shortly. :)

-Jason On Dec 28, 2013 8:44 AM, "Bodo Kaiser" notifications@github.com wrote:

Hello @benbria https://github.com/benbria

I would like to use aliasify but did not found it in the npm repository. Could you publish it?

PS: I would prefer just passing a plain js hash to the transform as I it makes independent from how you handle your configuration:

var builder = browserify();

builder.transform(require('./aliasify')(app.settings.aliasify));

//...

Best, Bo

— Reply to this email directly or view it on GitHubhttps://github.com/benbria/aliasify/issues/2 .

jwalton commented 10 years ago

There was a lively discussion here btw, about the configuration of transform options. :)

bodokaiser commented 10 years ago

I do not find the discussion?

Ps: good flight!

Am 28.12.2013 um 16:23 schrieb Jason Walton notifications@github.com:

There was a lively discussion here btw, about the configuration of transform options. :)

— Reply to this email directly or view it on GitHub.

jwalton commented 10 years ago

A link would be good. :) https://github.com/substack/node-browserify/issues/546

Package is now published. Working on configuring. I'm going to make it so you do:

aliasify = require('aliasify').configure(config, {configDir: __dirname});

where configDir is used to resolve relative paths in the config.

bodokaiser commented 10 years ago

Ah okay was on the wrong repository...

I am not aware of your implementation in detail but from my experience its the most easiest to pass it directly to the function as you suggested first in the discussion.

Example with HTML + custom parser option:

var through = require('through');

var regex = /\.[0-9a-z]+$/i;

module.exports = function(options) {

    return function(filename) {
        var template = '';

        if (!isHTMLFile(filename)) {
            return through();
        }

        var stream = through(read, end);

        function read(string) {
            template += string;
        }
        function end() {
            var module = 'module.exports =';

            if (options && options.callback) {
                template = options.callback(template);
            }
            module += JSON.stringify(template);

            this.queue(module);
            this.queue(null);
        }

        return stream;
    };
};

function isHTMLFile(filename) {
    var extension = regex.exec(filename).pop();

    return extension === '.html';
}
bodokaiser commented 10 years ago

I also would move the directory option into the config hash.

bodokaiser commented 10 years ago

I played around a bit and found also an interesting approach. Unfortunately browserify loads the file before the transform so it will always crash...

var through = require('through');

module.exports = function(options) {

    return function(filename) {
        console.log(filename);
        console.log(replace(filename, options));

        return through();
    };

};

function replace(string, options) {
    for (var key in options) {
        var path = options[key];

        if (hasPrefix(string, key)) {
            string = string.replace(key, path);
        }
    }

    return string;
}

function hasPrefix(string, prefix) {
    return string.substring(0, prefix.length) === prefix;
}
jwalton commented 10 years ago

Ok, you can now set configuration either by doing:

aliasify = require('aliasify').configure({
    aliases: {
        "d3": "./shims/d3.js"
    },
    configDir: __dirname,
    verbose: false
});

configure() returns a new instance of aliasify, so you can generate another instance of aliasify with a different configuration if you need to, without affecting the first one.

Lemme know if that works for you. Thanks!

bodokaiser commented 10 years ago

Yes works everything! Thank you much for your patience :)