Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

cache[path] not cache[name] #23

Open
philcockfield opened this issue Sep 18, 2011 · 1 comment
Open

cache[path] not cache[name] #23

philcockfield opened this issue Sep 18, 2011 · 1 comment

Comments

@philcockfield
Copy link

Currently the require JS code is caching modules based on the name. This means that if I have two different modules, with the same name in different folders, I will get the wrong module returned when making relative calls for it.

For example, with modules in the following folder structure

 module1/foo
 module2/foo

If I were to do a require('./foo') from within module2 - if module1/foo had already been required (and thus cached) then it would return the module1/foo module.

This can be resolved by changing the cache key from name to path, eg:

Line 64: https://github.com/sstephenson/stitch/blob/master/src/stitch.coffee

        require = function(name, root) {
            var path = expand(root, name), 
                module = cache[path],
                fn;
        ...
@philcockfield
Copy link
Author

Here's the complete function with my re-writes that is passing tests on my end:

(function() {
    if (!this.require) {
        var modules = {},
        cache = {},
        require = function(name, root) {
            var path = expand(root, name), 
                cacheKey = path,
                module = cache[cacheKey],
                fn;
            if (module) {
                return module;
            } else if (fn = modules[path] || modules[path = expand(path, './index')]) {
                module = {
                    id: path,
                    exports: {}
                };
                try {
                    cache[cacheKey] = module.exports;
                    fn(module.exports,
                    function(name) {
                        return require(name, dirname(path));
                    },
                    module);
                    return cache[cacheKey] = module.exports;
                } catch(err) {
                    delete cache[cacheKey];
                    throw err;
                }
            } else {
                throw 'module \'' + name + '\' not found';
            }
        },
        expand = function(root, name) {
            var results = [],
            parts,
            part;
            if (/^\.\.?(\/|$)/.test(name)) {
                parts = [root, name].join('/').split('/');
            } else {
                parts = name.split('/');
            }
            for (var i = 0, length = parts.length; i < length; i++) {
                part = parts[i];
                if (part == '..') {
                    results.pop();
                } else if (part != '.' && part != '') {
                    results.push(part);
                }
            }
            return results.join('/');
        },
        dirname = function(path) {
            return path.split('/').slice(0, -1).join('/');
        };
        this.require = function(name) {
            return require(name, '');
        };
        this.require.define = function(bundle) {
            for (var key in bundle)
            modules[key] = bundle[key];
        };
    }
    return this.require.define;
}).call(this);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant