Skip to content

Commit b5f357d

Browse files
committed
Relates to requirejs#333, initial implementation of default prefix.
1 parent a0e263b commit b5f357d

11 files changed

Lines changed: 225 additions & 2 deletions

File tree

require.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,16 @@ var requirejs, require, define;
437437
name = '_@r' + (requireCounter += 1);
438438
}
439439

440-
if (index !== -1) {
440+
//Do not count IDs that are '!a', that just means
441+
//'a'
442+
if (index > -1) {
441443
prefix = name.substring(0, index);
442444
name = name.substring(index + 1, name.length);
445+
} else if (parentModuleMap &&
446+
parentModuleMap.defaultPrefix) {
447+
//This is dependency mentioned by a plugin that
448+
//has a default prefix.
449+
prefix = parentModuleMap.defaultPrefix;
443450
}
444451

445452
if (prefix) {
@@ -1002,6 +1009,10 @@ var requirejs, require, define;
10021009
var hasInteractive = useInteractive,
10031010
moduleMap = makeModuleMap(moduleName);
10041011

1012+
if (plugin.useDefaultPrefix) {
1013+
moduleMap.defaultPrefix = map.prefix;
1014+
}
1015+
10051016
//Turn off interactive script matching for IE for any define
10061017
//calls in the text, then turn it back on at the end.
10071018
if (hasInteractive) {
@@ -1012,7 +1023,12 @@ var requirejs, require, define;
10121023
//it.
10131024
getModule(moduleMap);
10141025

1015-
req.exec(text);
1026+
try {
1027+
req.exec(text);
1028+
} catch (e) {
1029+
throw new Error('fromText eval for ' + moduleName +
1030+
' failed: ' + e);
1031+
}
10161032

10171033
if (hasInteractive) {
10181034
useInteractive = true;

tests/all.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ doh.registerUrl("pluginLast", "../plugins/pluginLast/pluginLast.html");
109109
doh.registerUrl("pluginShim", "../plugins/pluginShim/pluginShim.html");
110110
doh.registerUrl("pluginMap", "../plugins/pluginMap/pluginMap.html");
111111
doh.registerUrl("pluginMapDynamic", "../plugins/pluginMap/dynamic/pluginMapDynamic.html");
112+
doh.registerUrl("pluginDefaultPrefix", "../plugins/defaultPrefix/defaultPrefix.html");
112113

113114
doh.registerUrl("requirePluginLoad", "../requirePluginLoad/requirePluginLoad.html");
114115

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//The refine plugin changes the word refine into define.
2+
refine(['refine!b', 'c', '!plain', 'text!a.txt'],
3+
function (b, c, plain, text) {
4+
return {
5+
name: 'a',
6+
b: b,
7+
c: c,
8+
plain: plain,
9+
text: text
10+
};
11+
});

tests/plugins/defaultPrefix/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a.txt
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
refine(['!plain', 'text!b.txt'], function (plain, text) {
2+
return {
3+
name: 'b',
4+
plain: plain,
5+
text: text
6+
}
7+
});

tests/plugins/defaultPrefix/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b.txt
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
refine(['b'], function (b) {
2+
return {
3+
name: 'c',
4+
b: b
5+
};
6+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require({
2+
baseUrl: requirejs.isBrowser ? './' : './plugins/defaultPrefix',
3+
paths: {
4+
'text': '../../../../text/text'
5+
},
6+
}, ['refine!a'],
7+
function (a) {
8+
9+
doh.register(
10+
'pluginDefaultPrefix',
11+
[
12+
function pluginDefaultPrefix(t){
13+
t.is('a', a.name);
14+
t.is('b', a.b.name);
15+
t.is('c', a.c.name);
16+
t.is('plain', a.plain.name);
17+
t.is(true, a.text.indexOf('a.txt') === 0);
18+
19+
t.is('plain', a.b.plain.name);
20+
t.is(true, a.b.text.indexOf('b.txt') === 0);
21+
22+
t.is('b', a.c.b.name);
23+
t.is('plain', a.c.b.plain.name);
24+
t.is(true, a.c.b.text.indexOf('b.txt') === 0);
25+
26+
}
27+
]
28+
);
29+
doh.run();
30+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>require.js: Plugin Default Prefix Test</title>
5+
<script type="text/javascript" src="../../doh/runner.js"></script>
6+
<script type="text/javascript" src="../../doh/_browserRunner.js"></script>
7+
<script type="text/javascript" data-main="defaultPrefix-tests.js" src="../../../require.js"></script>
8+
</head>
9+
<body>
10+
<h1>require.js: Plugin Default Prefix Test</h1>
11+
<p>More info:
12+
<a href="https://github.com/jrburke/requirejs/issues/333">333</a>.
13+
<p>Check console for messages</p>
14+
</body>
15+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define({
2+
name: 'plain'
3+
});

0 commit comments

Comments
 (0)