Skip to content

Commit 788e458

Browse files
committed
Fixes requirejs#217, hide the module and exports for r.js from plugins and dependencies executing as part of a build.
1 parent 714e0fd commit 788e458

File tree

9 files changed

+150
-9
lines changed

9 files changed

+150
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ build/tests/lib/nestedHas/main-builtNeedB.js
3232
build/tests/lib/nestedHas/main-builtNeedC.js
3333
build/tests/lib/nestedHas/main-builtNeedD.js
3434
build/tests/lib/nestedHas/main-builtNested.js
35+
build/tests/lib/noexports/main-built.js
3536
build/tests/lib/nonStrict/main-built.js
3637
build/tests/lib/onBuildRead/main-built.js
3738
build/tests/lib/onBuildWrite/main-built.js

build/jslib/requirePatch.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ function (file, pragma, parse, lang, logger, commonJs) {
3030
var layer,
3131
pluginBuilderRegExp = /(["']?)pluginBuilder(["']?)\s*[=\:]\s*["']([^'"\s]+)["']/,
3232
oldNewContext = require.s.newContext,
33-
oldDef;
33+
oldDef,
34+
35+
//create local undefined values for module and exports,
36+
//so that when files are evaled in this function they do not
37+
//see the node values used for r.js
38+
exports,
39+
module;
3440

3541
//Stored cached file contents for reuse in other layers.
3642
require._cachedFileContents = {};

build/tests/builds.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,4 +1292,23 @@ define(['build', 'env!env/file'], function (build, file) {
12921292
);
12931293
doh.run();
12941294

1295+
//Make sure evaled plugin dependencies in a build do not see the module
1296+
//and exports value for r.js https://github.com/jrburke/r.js/issues/217
1297+
doh.register("noexports",
1298+
[
1299+
function noexports(t) {
1300+
file.deleteFile("lib/noexports/main-built.js");
1301+
1302+
build(["lib/noexports/build.js"]);
1303+
1304+
t.is(nol(c("lib/noexports/expected.js")),
1305+
nol(c("lib/noexports/main-built.js")));
1306+
1307+
require._buildReset();
1308+
}
1309+
1310+
]
1311+
);
1312+
doh.run();
1313+
12951314
});

build/tests/lib/moduleThenPlugin/expected.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ require(['sub1'], function (sub1) {});
77
define("main", function(){});
88

99
/**
10-
* @license RequireJS text 2.0.0 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
10+
* @license RequireJS text 2.0.1 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
1111
* Available via the MIT or new BSD license.
1212
* see: http://github.com/requirejs/text for details
1313
*/
14-
/*jslint */
14+
/*jslint regexp: true */
1515
/*global require: false, XMLHttpRequest: false, ActiveXObject: false,
1616
define: false, window: false, process: false, Packages: false,
1717
java: false, location: false */
@@ -27,11 +27,11 @@ define('text',['module'], function (module) {
2727
defaultHostName = hasLocation && location.hostname,
2828
defaultPort = hasLocation && (location.port || undefined),
2929
buildMap = [],
30-
masterConfig = module.config(),
30+
masterConfig = (module.config && module.config()) || {},
3131
text, fs;
3232

3333
text = {
34-
version: '2.0.0',
34+
version: '2.0.1',
3535

3636
strip: function (content) {
3737
//Strips <?xml ...?> declarations so that external SVG and XML
@@ -55,16 +55,18 @@ define('text',['module'], function (module) {
5555
.replace(/[\b]/g, "\\b")
5656
.replace(/[\n]/g, "\\n")
5757
.replace(/[\t]/g, "\\t")
58-
.replace(/[\r]/g, "\\r");
58+
.replace(/[\r]/g, "\\r")
59+
.replace(/[\u2028]/g, "\\u2028")
60+
.replace(/[\u2029]/g, "\\u2029");
5961
},
6062

61-
createXhr: function () {
63+
createXhr: masterConfig.createXhr || function () {
6264
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
6365
var xhr, i, progId;
6466
if (typeof XMLHttpRequest !== "undefined") {
6567
return new XMLHttpRequest();
6668
} else if (typeof ActiveXObject !== "undefined") {
67-
for (i = 0; i < 3; i++) {
69+
for (i = 0; i < 3; i += 1) {
6870
progId = progIds[i];
6971
try {
7072
xhr = new ActiveXObject(progId);
@@ -132,7 +134,7 @@ define('text',['module'], function (module) {
132134
uHostName = uHostName[0];
133135

134136
return (!uProtocol || uProtocol === protocol) &&
135-
(!uHostName || uHostName === hostname) &&
137+
(!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) &&
136138
((!uPort && !uHostName) || uPort === port);
137139
},
138140

build/tests/lib/noexports/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
baseUrl: '.',
3+
name: 'main',
4+
out: 'main-built.js',
5+
optimize: 'none'
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(function (factory) {
2+
if (typeof exports !== 'undefined') {
3+
// Node/CommonJS
4+
factory(exports);
5+
} else if (typeof define === 'function' && define.amd) {
6+
// AMD
7+
define(['exports'], factory);
8+
}
9+
10+
11+
}(function (exports) {
12+
exports.version = '2';
13+
exports.convert = function (text) {
14+
return text.toUpperCase();
15+
};
16+
}));

build/tests/lib/noexports/expected.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
(function (factory) {
3+
if (typeof exports !== 'undefined') {
4+
// Node/CommonJS
5+
factory(exports);
6+
} else if (typeof define === 'function' && define.amd) {
7+
// AMD
8+
define('converter',['exports'], factory);
9+
}
10+
11+
12+
}(function (exports) {
13+
exports.version = '2';
14+
exports.convert = function (text) {
15+
return text.toUpperCase();
16+
};
17+
}));
18+
19+
define('plug',['converter'], function (converter) {
20+
var buildMap = {};
21+
22+
function jsEscape(content) {
23+
return content.replace(/(['\\])/g, '\\$1')
24+
.replace(/[\f]/g, "\\f")
25+
.replace(/[\b]/g, "\\b")
26+
.replace(/[\n]/g, "\\n")
27+
.replace(/[\t]/g, "\\t")
28+
.replace(/[\r]/g, "\\r");
29+
}
30+
31+
return {
32+
version: '1',
33+
load: function (name, require, onLoad, config) {
34+
var converted = converter.convert(name);
35+
buildMap[name] = converted;
36+
onLoad(converted);
37+
},
38+
39+
write: function (pluginName, moduleName, write, config) {
40+
if (moduleName in buildMap) {
41+
var content = jsEscape(buildMap[moduleName]);
42+
write("define('" + pluginName + "!" + moduleName +
43+
"', function () { return '" + content + "';});\n");
44+
}
45+
}
46+
};
47+
});
48+
49+
define('plug!shouldbeuppercasetext', function () { return 'SHOULDBEUPPERCASETEXT';});
50+
51+
require(['plug', 'plug!shouldbeuppercasetext'],
52+
function (plug, text) {
53+
console.log('plugin version: ' + plug.version);
54+
console.log('converted text: ' + text);
55+
});
56+
57+
define("main", function(){});

build/tests/lib/noexports/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require(['plug', 'plug!shouldbeuppercasetext'],
2+
function (plug, text) {
3+
console.log('plugin version: ' + plug.version);
4+
console.log('converted text: ' + text);
5+
});

build/tests/lib/noexports/plug.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
define(['converter'], function (converter) {
2+
var buildMap = {};
3+
4+
function jsEscape(content) {
5+
return content.replace(/(['\\])/g, '\\$1')
6+
.replace(/[\f]/g, "\\f")
7+
.replace(/[\b]/g, "\\b")
8+
.replace(/[\n]/g, "\\n")
9+
.replace(/[\t]/g, "\\t")
10+
.replace(/[\r]/g, "\\r");
11+
}
12+
13+
return {
14+
version: '1',
15+
load: function (name, require, onLoad, config) {
16+
var converted = converter.convert(name);
17+
buildMap[name] = converted;
18+
onLoad(converted);
19+
},
20+
21+
write: function (pluginName, moduleName, write, config) {
22+
if (moduleName in buildMap) {
23+
var content = jsEscape(buildMap[moduleName]);
24+
write("define('" + pluginName + "!" + moduleName +
25+
"', function () { return '" + content + "';});\n");
26+
}
27+
}
28+
};
29+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy