Skip to content

Commit c3d5f33

Browse files
committed
Fixes requirejs#497: introduce bundles config, allows modules and loader plugin resources to be loaded by bundle ID
1 parent 9a7ef7d commit c3d5f33

15 files changed

Lines changed: 192 additions & 1 deletion

File tree

docs/api.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,21 @@ <h2>
543543

544544
<p>When run in a browser, <a href="#pathsfallbacks">paths fallbacks</a> can be specified, to allow trying a load from a CDN location, but falling back to a local location if the CDN location fails to load.</p>
545545

546+
<p id="config-bundles"><strong><a href="#config-bundles">bundles</a></strong>: Introduced in RequireJS 2.1.10: allows pointing multiple module IDs to a module ID that contains a bundle of modules. Example:</p>
547+
548+
<pre><code>requirejs.config({
549+
bundles: {
550+
'primary': ['main', 'util', 'text', 'text!template.html'],
551+
'secondary': ['text!secondary.html']
552+
}
553+
});
554+
</code></pre>
555+
556+
<p>That config states: modules 'main', 'util', 'text' and 'text!template.html' will be found by loading module ID 'primary'. Module 'text!secondary.html' can be found by loading module ID 'secondary'.
557+
</p>
558+
559+
<p>This is useful if doing a build and that build target was not an existing module ID, or if you have loader plugin resources in built JS files that should not be loaded by the loader plugin. <strong>Note that the keys and values are module IDs</strong>, not path segments. They are absolute module IDs, not a module ID prefix like <a href="#config-paths">paths config</a> or <a href="#config-map">map config</a>. Also, bundle config is different from map config in that map config is a one-to-one module ID relationship, where bundle config is for pointing multiple module IDs to a bundle's module ID.</p>
560+
546561
<p id="config-shim"><strong><a href="#config-shim">shim</a></strong>: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value.</p>
547562

548563
<p>Here is an example. It requires RequireJS 2.1.0+, and assumes backbone.js, underscore.js and jquery.js have been installed in the baseUrl directory. If not, then you may need to set a paths config for them:</p>

require.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ var requirejs, require, define;
204204
waitSeconds: 7,
205205
baseUrl: './',
206206
paths: {},
207+
bundles: {},
207208
pkgs: {},
208209
shim: {},
209210
config: {}
@@ -217,6 +218,7 @@ var requirejs, require, define;
217218
defQueue = [],
218219
defined = {},
219220
urlFetched = {},
221+
bundlesMap = {},
220222
requireCounter = 1,
221223
unnormalizedCounter = 1;
222224

@@ -936,6 +938,7 @@ var requirejs, require, define;
936938

937939
on(pluginMap, 'defined', bind(this, function (plugin) {
938940
var load, normalizedMap, normalizedMod,
941+
bundleId = getOwn(bundlesMap, this.map.id),
939942
name = this.map.name,
940943
parentName = this.map.parentMap ? this.map.parentMap.name : null,
941944
localRequire = context.makeRequire(map.parentMap, {
@@ -981,6 +984,14 @@ var requirejs, require, define;
981984
return;
982985
}
983986

987+
//If a paths config, then just load that file instead to
988+
//resolve the plugin, as it is built into that paths layer.
989+
if (bundleId) {
990+
this.map.url = context.nameToUrl(bundleId);
991+
this.load();
992+
return;
993+
}
994+
984995
load = bind(this, function (value) {
985996
this.init([], function () { return value; }, null, {
986997
enabled: true
@@ -1251,6 +1262,7 @@ var requirejs, require, define;
12511262
shim = config.shim,
12521263
objs = {
12531264
paths: true,
1265+
bundles: true,
12541266
config: true,
12551267
map: true
12561268
};
@@ -1266,6 +1278,17 @@ var requirejs, require, define;
12661278
}
12671279
});
12681280

1281+
//Reverse map the bundles
1282+
if (cfg.bundles) {
1283+
eachProp(cfg.bundles, function (value, prop) {
1284+
each(value, function (v) {
1285+
if (v !== prop) {
1286+
bundlesMap[v] = prop;
1287+
}
1288+
});
1289+
});
1290+
}
1291+
12691292
//Merge shim
12701293
if (cfg.shim) {
12711294
eachProp(cfg.shim, function (value, id) {
@@ -1564,7 +1587,12 @@ var requirejs, require, define;
15641587
*/
15651588
nameToUrl: function (moduleName, ext, skipExt) {
15661589
var paths, pkgs, pkg, pkgPath, syms, i, parentModule, url,
1567-
parentPath;
1590+
parentPath,
1591+
bundleId = getOwn(bundlesMap, moduleName);
1592+
1593+
if (bundleId) {
1594+
return context.nameToUrl(bundleId, ext, skipExt);
1595+
}
15681596

15691597
//If a colon is in the URL, it indicates a protocol is used and it is just
15701598
//an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?)

tests/all.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ doh.registerUrl("mapConfigPluginBuilt", "../mapConfig/built/mapConfigPluginBuilt
100100

101101
doh.registerUrl("layers", "../layers/layers.html", 10000);
102102

103+
doh.registerUrl("bundles", "../bundles/bundles.html");
104+
103105
doh.registerUrl("afterload", "../afterload.html", 10000);
104106

105107
doh.registerUrl("universal", "../universal/universal.html");
@@ -113,6 +115,9 @@ doh.registerUrl("nestedDefine2", "../nestedDefine/nestedDefine2.html");
113115
doh.registerUrl("nestedRelativeRequire", "../nestedRelativeRequire/nestedRelativeRequire.html");
114116
doh.registerUrl("nestedRequireConfig", "../nestedRequireConfig/nestedRequireConfig.html");
115117

118+
doh.registerUrl("pluginBundles", "../plugins/pluginBundles/pluginBundles.html");
119+
doh.registerUrl("pluginBundlesSeparateText", "../plugins/pluginBundlesSeparateText/pluginBundlesSeparateText.html");
120+
116121
doh.registerUrl("pluginsSync", "../plugins/sync.html");
117122
doh.registerUrl("pluginsOnError", "../plugins/onerror/onerror.html");
118123
doh.registerUrl("doublePluginCall", "../plugins/double.html");

tests/bundles/bundles-tests.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require({
2+
bundles: {
3+
'main': ['util', 'main'],
4+
'second': ['other'],
5+
'third': ['third']
6+
}
7+
}, ['util', 'other', 'third'], function (util, other, third) {
8+
9+
require(['main'], function (main) {
10+
doh.register(
11+
'bundles',
12+
[
13+
function bundles(t){
14+
t.is('util', util.name);
15+
t.is('other', other.name);
16+
t.is('third', third.name);
17+
t.is('main', main.name);
18+
}
19+
]
20+
);
21+
doh.run();
22+
});
23+
});

tests/bundles/bundles.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>require.js: Bundles Config Test</title>
5+
<script type="text/javascript" src="../../require.js"></script>
6+
<script type="text/javascript" src="../doh/runner.js"></script>
7+
<script type="text/javascript" src="../doh/_browserRunner.js"></script>
8+
<script type="text/javascript" src="bundles-tests.js"></script>
9+
</head>
10+
<body>
11+
<h1>require.js: Bundles Config Test</h1>
12+
<p>Test using the bundles config. More info:
13+
<a href="https://github.com/jrburke/requirejs/issues/497">497</a></p>
14+
<p>Check console for messages</p>
15+
</body>
16+
</html>

tests/bundles/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define('util', {
2+
name: 'util'
3+
});
4+
5+
define('main', {
6+
name: 'main'
7+
});
8+

tests/bundles/second.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define('other', {
2+
name: 'other'
3+
});

tests/bundles/third.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define('third', {
2+
name: 'third'
3+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
define('text', [], {
2+
load: function () {
3+
throw new Error('not implemented');
4+
}
5+
});
6+
7+
define('text!template.html', [], function () {
8+
return 'main template';
9+
});
10+
11+
define('main', ['text!template.html'], function(template) {
12+
return {
13+
name: 'main',
14+
template: template
15+
};
16+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require({
2+
bundles: {
3+
'main': ['text', 'text!template.html']
4+
}
5+
}, ['text!template.html'], function (template) {
6+
7+
doh.register(
8+
'pluginBundles',
9+
[
10+
function pluginBundles(t){
11+
t.is('main template', template);
12+
}
13+
]
14+
);
15+
doh.run();
16+
17+
});

0 commit comments

Comments
 (0)