I want to define a module, that does nothing but require other modules, to make sure they are included. like this:
File: common.js
define(['routes/index_route', 'routes/guides_route']);```
r.js output is:
define(['routes/index_route', 'routes/guides_route']);
this is wrong because it is supposed to output a named module.
However if i define it with a dummy function callback like this:
File common.js
define(['routes/index_route', 'routes/guides_route'], function() {});
r.js outputs properly.
define('routes/common',['routes/index_route', 'routes/guides_route'],
function() {});
Is it okay to define a module with a sole purpose of requiring other modules like this?
And why does r.js behave like this?
Edit
I am having trouble with the amd lingo so i will define my problem.
I have a router.js which includes all the modules.
File: router.js
define(['routes/index_route', 'routes/guides_route', 'routes/other_route'],
function() {
});
I include all these routes
('index_route, guides_route'), but i don't really need any references. So instead of including a bunch of
routes
inside router.js like this, i want to include all these routes
in a seperate file, and include just that file inside router.js.
File: router.js
define(['routes/common'], function() {});
File: routes/common.js
define(['routes/index_route', 'routes/guides_route', 'routes/other_route']);
Then the above problem arises.
TL;DR I think you are confusing what require and define is. and what r.js is used for.
I though the difference between require(['module'], function() {})
and define(['module'], function() {})
is
require function is immediately called, define function is called only when its required by some other module.
So
if you want to request modules you need to use require insead of define
makes no sense.
TL;DR Just tell me the difference between:
define(['module']);
define(['module'], function() {})
In terms of how optimizer operates on them. That will tell me why optimizer outputs differently in two cases.
define(['module']);
define('filename', ['module']);
If i am in some kind of pitfall please shed more light please otherwise don't confuse me further with noise.
If I undestand your question correctly, using 'packages' property of require.config will help you,
Keep your common file as it is, i.e.
File common.js
define(['routes/index_route', 'routes/guides_route'], function() {});
Like,
options: {
paths: ...,
packages: [{
name: 'routes/common', // or 'routes' or anything
location: 'routes',
main: 'common'
}],
....
}
Then you can directly use that name of module to in your 'router.js' file.
I do this to create a single named module for multiple files, including which load all sub-modules in main file of package.
Edit:
You can also omit the 'name' property of package if you want. In that case you can rename 'common.js' to 'main.js' since it is a standard name for entry point of packages. So, renaming 'common.js' to 'main.js' it can become,
options: {
paths: ...,
packages: ['routes'],
....
}
Refer following URL for more explanation/options,