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)?
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?
On Wednesday, 25 April 2012 16:14:07 UTC-5, Jeff Barczewski wrote:
> 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)?
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?
On Wednesday, April 25, 2012 4:17:59 PM UTC-5, Jeff Barczewski wrote:
> 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
> On Wednesday, 25 April 2012 16:14:07 UTC-5, Jeff Barczewski wrote:
>> 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)?
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?
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.
> On Wednesday, April 25, 2012 4:17:59 PM UTC-5, Jeff Barczewski wrote:
>> 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
>> On Wednesday, 25 April 2012 16:14:07 UTC-5, Jeff Barczewski wrote:
>>> 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)?
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?
<jeff.barczew...@gmail.com> wrote:
> 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 () {};
> });
> ```
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:
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.