cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

Module or package specific configuration (with config overrides) #219

Closed webpro closed 9 years ago

webpro commented 10 years ago

How can I add module-specific configuration with curl? E.g. packages: [{ name: 'bar', location: 'foo', main: 'core', config: {foo:1} }].

This completely confuses curl it seems. The "proposed standard" way also doesn't seem to work (https://github.com/amdjs/amdjs-api/wiki/Common-Config#config-).

I can request module.config() in a module, yet I don't know how to add data in the main config.

unscriptable commented 10 years ago

This completely confuses curl it seems.

Can you elaborate? This feature works fine in my tests.

curl.config({ packages: [{ name: 'bar', location: 'foo', main: 'core', config: {foo:1} }] });

//... later, in a module in the bar package:

var cfg = module.config ? module.config() : {};
var foo = cfg.foo;
webpro commented 10 years ago

A bit time-consuming to set up a proper test case, but I will if needed.

If I put a config property like you did, and after this I'm doing another call to curl.config() (also containing packages), then that second config is not applied. If I combine the configs into one call, it works fine.

unscriptable commented 10 years ago

Interesting. That's the opposite of what I would have expected. As a work-around until I test this, you could try something like the following if there is only one module that needs module.config() (curl 0.8 or higher):

curl.config({
    paths: {
        'core/module-that-needs-config': { location: 'core/module-that-needs-config', config: { foo: 1 } }
    }
});
webpro commented 10 years ago

Thanks for the work-around, that works fine. Obviously I'm interested in a possible fix.

unscriptable commented 10 years ago

Yes. Let's keep this issue open until I have a chance to look into the problem further. :)

CNSKnight commented 9 years ago

I've been grappling with similar:

Specifically, curl package will not use paths:{} not defined in the original config object.

This, taken from my bootstrapping config.js.config.js has no effect:

        {
            name: 'MyPkg',
            location: 'path/to/templates',
            main: 'js/my-package.source',
            // config options specific to MyPkg
            config: {
                paths: {
                    foo: 'path/to/foo',
                    bootstrap: 'sample/path/to/bootstrap.js'
                }
            }
        }

...and doing this in the above package module throws error (see below)

curl.config({
        paths: {
            bootstrap: 'path/to/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js'
        }
});

define(['firstPkg', 'some/js/module.source', 'js!bootstrap'], function(exposure1, exposure2) {
        // sidenote: module property is not available here in a package 'module'?

        return exposure1;
});

Error thrown:

            // adds the path matching regexp onto the cfg or plugin cfgs.
            function convertPathMatcher (cfg) {
                var pathMap = cfg.pathMap;
                cfg.pathRx = new RegExp('^(' +
                    cfg.pathList.sort(function (a, b) { return pathMap[b].specificity - pathMap[a].specificity; } )
X Uncaught TypeError: Cannot read property 'specificity' of undefined
                        .join('|')
                        .replace(/\/|\./g, '\\$&') +
                    ')(?=\\/|$)'
                );
                delete cfg.pathList;
            }

To be clear, defining the 'bootstrap' path in the paths hash of the mentioned bootstrapping config.js works fine.

unscriptable commented 9 years ago

Hey @CNSKnight,

I think it's highly unlikely that curl will offer per-package paths and packages configuration as you've shown. When implementing the per-package configuration feature, we realized that the complexity of curl's internals would increase significantly. Of course, we could refactor and/or rewrite curl's internals, but that would take some time.

At the moment, we're spending most of our module-focused efforts on rave. Rave already handles per-package mappings automatically (without configuration).

Yes, rave is still under development, but it sounds like you have a work-around that you can use for now?

I'm going to close this issue, but please keep posting if you need additional help on your work-around.

Regards,

-- John

CNSKnight commented 9 years ago

Gotit. Meantime, let's update the docs to to that effect. Still makes it challenging to do tru drop-in modular development but I'll work w/what we got & wait a bit on your rave effort.

Best

unscriptable commented 9 years ago

Done: https://github.com/cujojs/curl/commit/38fbed6e2373163d63178dcd76c2a46650eeca6d