Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Is there a way to use requirejs (like amdefine) to create modules which load in browser or server without having a separate require module?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeff Barczewski  
View profile  
 More options Apr 25 2012, 5:14 pm
From: Jeff Barczewski <jeff.barczew...@gmail.com>
Date: Wed, 25 Apr 2012 14:14:07 -0700 (PDT)
Local: Wed, Apr 25 2012 5:14 pm
Subject: Is there a way to use requirejs (like amdefine) to create modules which load in browser or server without having a separate require module?

Question: Is there a way to use requirejs (like amdefine) to create modules
which load in browser or server without having a separate node.js file
which requires the module?

Details:

I am building modules which are to be used on both browser and the server
(node.js) and I really like how with amdefine that you can make things work
in either place (and optimizer strips out the boilerplate).

```javascript

if (typeof define !== 'function') {
    var define = require('amdefine')(module);

}

```

This is great for non-requirejs projects, but one drawback is that you have
to repeat that boilerplate in every module.

Since my team is using requirejs for its projects, I would like to be able
to do similar with requirejs (and thus have access to all its other great
features). For example something like this would be nice,

```javascript
  if (typeof define !== 'function') {
    var define = require('requirejs')(module);
  }
```

That would eliminate the need for amdefine since we are using requirejs and
allows use of all of requirejs capabilities. I'm hoping that something like
that would also eliminate the need to add boilerplate in the other modules
since normally with requirejs I do not need that. And hopefully the
optimizer would be smart enough to eliminate these calls just like it does
for amdefine.

Curently the only way I have been able to use requirejs from node.js is to
create a separate file only used for node.js which requires the other file
and does an export.

```javascript

var requirejs = require('requirejs');

requirejs.config({
    //Use node's special variable __dirname to
    //get the directory containing this file.
    //Useful if building a library that will
    //be used in node but does not require the
    //use of node outside
    baseUrl: __dirname,

    //Pass the top-level main.js/index.js require
    //function to requirejs so that node modules
    //are loaded relative to the top-level JS file.
    nodeRequire: require

});

requirejs(['foo', 'bar'],function   (foo,   bar) {
    //foo and bar are loaded according to requirejs
    //config, but if not found, then node's require
    //is used to load the module.

    //Now export a value visible to Node.
    module.exports = function () {};

});

```

Is there something I am missing where I wouldn't need to have this separate
node.js require file and can just use a define (similar to amdefine use)?

Thanks,

Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Barczewski  
View profile  
 More options Apr 25 2012, 5:17 pm
From: Jeff Barczewski <jeff.barczew...@gmail.com>
Date: Wed, 25 Apr 2012 14:17:59 -0700 (PDT)
Local: Wed, Apr 25 2012 5:17 pm
Subject: Re: Is there a way to use requirejs (like amdefine) to create modules which load in browser or server without having a separate require module?

Actually my desired snippet should have been something like this

```javascript
  if (typeof define !== 'function') {
    var define = require('requirejs')(module)*.define*;
  }
```

I forgot the .define on the end. That would allow you to get access to both
require or define.

By passing in module it could automatically set the baseUrl and nodeRequire
config.

Of course one could still do additional config directly if necessary, but
for many things this is all that would be needed.

Thanks,

Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tyler Kellen  
View profile  
 More options Apr 25 2012, 10:47 pm
From: Tyler Kellen <ty...@sleekcode.net>
Date: Wed, 25 Apr 2012 19:47:34 -0700 (PDT)
Local: Wed, Apr 25 2012 10:47 pm
Subject: Re: Is there a way to use requirejs (like amdefine) to create modules which load in browser or server without having a separate require module?

Hey Jeff,

Are you compiling down with r.js/almond?  If so, this UMD pattern should
cover what you're looking for:
https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js

...and here is some example code using a slightly different pattern that
accomplishes the same thing:
https://github.com/tkellen/requirejs-library-skeleton/blob/master/bui...

Hope that helps!

Tyler


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Barczewski  
View profile  
 More options Apr 26 2012, 12:21 am
From: Jeff Barczewski <jeff.barczew...@gmail.com>
Date: Wed, 25 Apr 2012 21:21:20 -0700 (PDT)
Local: Thurs, Apr 26 2012 12:21 am
Subject: Re: Is there a way to use requirejs (like amdefine) to create modules which load in browser or server without having a separate require module?

Tyler,

Once we get the relative package naming fixed in r.js
(https://github.com/jrburke/r.js/issues/136) , I will be able to use almond
as a lightweight loader in the browser for optimized code.

*But actually I am talking about the opposite side, using from node.js*.
amdefine.js works well but requires one to have the boilerplate repeated in
each module file and of course isn't as full featured as requirejs. Plus I
also use code in the brower without building too, so requirejs handles that
nicely.

*So I do want to continue to use requirejs as my loader for both browser
and server with node.js*, I just don't want to have to create an extra file
simply to use it from node.js. Like with amdefine, I should be able to
prefix the main file, load up requirejs and then everything else should
work fine, no extra files. I was just asking whether if anyone knew if this
was already possible. Right now I believe it requires an extra file.

I have used UMD style wrapper for simple modules, but for advanced work
requirejs seems like the way to go. I'm just proposing or asking if this is
already possible to make requirejs configure itself from passing module to
it, and have it optimize that boilerplate out when building.

If it turns out it hasn't been done and there are no obvious reasons why it
won't work, I could take a stab at implementing something like this since
it feels like something that should be in there.

Thanks for the suggestions.

Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Burke  
View profile  
 More options Apr 29 2012, 12:27 am
From: James Burke <jrbu...@gmail.com>
Date: Sat, 28 Apr 2012 21:27:29 -0700
Local: Sun, Apr 29 2012 12:27 am
Subject: Re: [requirejs] Is there a way to use requirejs (like amdefine) to create modules which load in browser or server without having a separate require module?
On Wed, Apr 25, 2012 at 2:14 PM, Jeff Barczewski

What you have there is pretty much it, other than using amdefine.

Node has an option where you can plug into file extensions and do
special handling, this is how coffeescript plugs into node:

https://github.com/jashkenas/coffee-script/blob/master/lib/coffee-scr...

However, since requirejs would plugin into the '.js' extension, I'm a
bit concerned about affecting plain node modules with any changes to
the .js processing.

James


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »