Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a public API through a global without path aliases? #26

Closed
vonWolfehaus opened this issue Feb 13, 2014 · 7 comments
Closed

Expose a public API through a global without path aliases? #26

vonWolfehaus opened this issue Feb 13, 2014 · 7 comments
Assignees

Comments

@vonWolfehaus
Copy link

I'm creating a little game framework, and with these sorts of things developers expect a global namespace that holds an API they can access. Take a look at Phaser as an example: Richard has var Phaser = {}, so it's attached to the window, then devs can instantiate modules within that namespace, like this: var player = new Phaser.Sprite().

I'm using requirejs for development, but I'd like to use amdclean to strip it away and simply leave a global object containing an exposed API similar to Phaser's. AMDclean gets so close, as you can see in this snippet.

The problem is that it takes the relative path of the module, like util/Thing and turns it into util_Thing. Honestly, there's nothing else out there that even gets this close, but it'd be a lot nicer if there was an option that stripped out the path automatically and placed it on the declared global object, so it'd become myGlobal.Thing instead of myGlobal.util_Thing.

Also notice that the global isn't prepended to display_Whatever in that linked snippet, so that's also an issue, I think?

I have too many modules to keep up a list of any kind, so making path aliases as suggested in the ReadMe is not an option for me. I understand there's danger of collisions, and while that wouldn't happen in my case, this could be a place for more control, such that a module path can be aliased away if things get too crazy (eg my/really/deep/Module to be attached as myGlobal.deep.Module. It'd be cool to map any path to any alias on the global! And I might be wrong, but I think r.js only takes module names, not paths, and aliases them?

Any pointers would be greatly appreciated if I'm actually doing something wrong too! Thanks for this excellent build tool!

@gfranko
Copy link
Owner

gfranko commented Feb 17, 2014

To use a "global" instead of a local variable, you just need to set the globalObject option to true.

Also, I am about to add a new option, called prefixTransform, that should help you adjust how AMDClean normalizes (aka prefixes) module names. This option will be included with the next version of AMDClean (I am shooting to release tomorrow).

prefixTransform Example

    amdclean.clean({
        'code': 'var example = require("util/anotherModule");',
        'prefixTransform': function(moduleName) {
            return moduleName.substring(moduleName.lastIndexOf('_') + 1, moduleName.length);
        }
    });

This would result in the following code: var example=anotherModule;

@gfranko
Copy link
Owner

gfranko commented Feb 18, 2014

You can now use the prefixTransform option with the AMDClean 1.2.1 release.

@gfranko gfranko closed this as completed Feb 18, 2014
@vonWolfehaus
Copy link
Author

Hey, elegant solution. Thank you!

@gfranko
Copy link
Owner

gfranko commented Feb 18, 2014

@vonWolfehaus Glad you like it! Does this work for you? Do you need anything else?

@vonWolfehaus
Copy link
Author

It does work, much appreciated. At least I think so, as what I'm seeing might be another issue entirely. I have an inline require: return require('core/Kai').registerComponents(arguments);. The first part got replaced with core_Kai, and not the expected global-prefixed version, von.core_Kai. It worked before I upgraded to 1.2.1 but maybe I did something different? The repo is here if you want to check out the gulpfile. It looks like a special case, where it's expecting core_Kai to be defined in the arguments array, but it isn't. Any suggestions?

@vonWolfehaus
Copy link
Author

The file in question is this one, forgot to mention.

@gfranko gfranko self-assigned this Feb 19, 2014
@gfranko
Copy link
Owner

gfranko commented Feb 23, 2014

#32

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

No branches or pull requests

2 participants