Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
legacy build test passes.
  • Loading branch information
jrburke committed Apr 27, 2012
1 parent 1b8bc92 commit 566c4fc
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 55 deletions.
28 changes: 17 additions & 11 deletions build/jslib/build.js
Expand Up @@ -942,7 +942,7 @@ function (lang, logger, file, parse, optimize, pragma,
context = layer.context,
anonDefRegExp = config.anonDefRegExp,
path, reqIndex, fileContents, currContents,
i, moduleName,
i, moduleName, legacy,
parts, builder, writeApi;

//Use override settings, particularly for pragmas
Expand Down Expand Up @@ -1020,16 +1020,13 @@ function (lang, logger, file, parse, optimize, pragma,
//after the module is processed.
//If we have a name, but no defined module, then add in the placeholder.
if (moduleName && !layer.modulesWithNames[moduleName] && !config.skipModuleInsertion) {
//If including jquery, register the module correctly, otherwise
//register an empty function. For jquery, make sure jQuery is
//a real object, and perhaps not some other file mapping, like
//to zepto.
if (moduleName === 'jquery') {
fileContents += '\n(function () {\n' +
'var jq = typeof jQuery !== "undefined" && jQuery;\n' +
namespace +
'define("jquery", [], function () { return jq; });\n' +
'}());\n';
legacy = config.legacy && config.legacy[moduleName];
if (legacy) {
fileContents += '\n' + namespace + 'define("' + moduleName + '", ' +
(legacy.deps && legacy.deps.length ?
build.makeJsArrayString(legacy.deps) + ', ' : '') +
(legacy.exports ? legacy.exports() : 'function(){}') +
');\n';
} else {
fileContents += '\n' + namespace + 'define("' + moduleName + '", function(){});\n';
}
Expand All @@ -1044,6 +1041,15 @@ function (lang, logger, file, parse, optimize, pragma,
};
};

//Converts an JS array of strings to a string representation.
//Not using JSON.stringify() for Rhino's sake.
build.makeJsArrayString = function (ary) {
return '["' + ary.map(function (item) {
//Escape any double quotes, backslashes
return lang.jsEscape(item);
}).join('","') + '"]';
};

/**
* Creates the regexp to find anonymous defines.
* @param {String} namespace an optional namespace to use. The namespace
Expand Down
12 changes: 11 additions & 1 deletion build/jslib/lang.js
Expand Up @@ -109,8 +109,18 @@ define(function () {
return function () {
return fn.apply(obj, arguments);
};
}
},

//Escapes a content string to be be a string that has characters escaped
//for inclusion as part of a JS string.
jsEscape: function (content) {
return content.replace(/(["'\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r");
}
};
return lang;
});
91 changes: 62 additions & 29 deletions build/jslib/requirePatch.js
Expand Up @@ -58,34 +58,6 @@ function (file, pragma, parse, lang) {
//Stored cached file contents for reuse in other layers.
require._cachedFileContents = {};

/** Reset state for each build layer pass. */
require._buildReset = function () {
var oldContext = require.s.contexts._;

//Clear up the existing context.
delete require.s.contexts._;

//Set up new context, so the layer object can hold onto it.
require({});

layer = require._layer = {
buildPathMap: {},
buildFileToModule: {},
buildFilePaths: [],
pathAdded: {},
modulesWithNames: {},
needsDefine: {},
existingRequireUrl: "",
context: require.s.contexts._
};

//Return the previous context in case it is needed, like for
//the basic config object.
return oldContext;
};

require._buildReset();

/**
* Makes sure the URL is something that can be supported by the
* optimization tool.
Expand Down Expand Up @@ -124,6 +96,35 @@ function (file, pragma, parse, lang) {
context.fullExec = {};
context.plugins = {};

//Override the legacy exports function generator to just
//spit out strings that can be used in the stringified
//build output.
context.makeLegacyExports = function (exports) {
var result;
if (typeof exports === 'string') {
result = function () {
return '(function (global) {\n' +
' return function () {\n' +
' return global["' + exports + '"];\n' +
' }\n' +
'}(this))';
};
} else {
result = function () {
return '(function (global) {\n' +
' return function () {\n' +
' var func = ' + exports.toString() + ';\n' +
' return func.apply(global, arguments);\n' +
' }\n' +
'}(this))';
};
}

//Mark the result has being tranformed by the build already.
result.__buildReady = true;
return result;
};

context.enable = function (depMap, parent) {
var id = depMap.id,
parentId = parent && parent.map.id,
Expand All @@ -144,7 +145,7 @@ function (file, pragma, parse, lang) {
};

//Override load so that the file paths can be collected.
context.load = function (context, moduleName, url) {
context.load = function (moduleName, url) {
/*jslint evil: true */
var contents, pluginBuilderMatch, builderName;

Expand Down Expand Up @@ -315,6 +316,38 @@ function (file, pragma, parse, lang) {
return context;
};

//Clear up the existing context so that the newContext modifications
//above will be active.
delete require.s.contexts._;

/** Reset state for each build layer pass. */
require._buildReset = function () {
var oldContext = require.s.contexts._;

//Clear up the existing context.
delete require.s.contexts._;

//Set up new context, so the layer object can hold onto it.
require({});

layer = require._layer = {
buildPathMap: {},
buildFileToModule: {},
buildFilePaths: [],
pathAdded: {},
modulesWithNames: {},
needsDefine: {},
existingRequireUrl: "",
context: require.s.contexts._
};

//Return the previous context in case it is needed, like for
//the basic config object.
return oldContext;
};

require._buildReset();

//Override define() to catch modules that just define an object, so that
//a dummy define call is not put in the build file for them. They do
//not end up getting defined via context.execCb, so we need to catch them
Expand Down
24 changes: 24 additions & 0 deletions build/tests/builds.js
Expand Up @@ -883,4 +883,28 @@ define(['build', 'env!env/file'], function (build, file) {
]
);
doh.run();


doh.register("legacyBasic",
[
function legacyBasic(t) {
var outFile = "../../../requirejs/tests/legacy/built/basic-tests.js";

file.deleteFile(outFile);

build(["lib/legacyBasic/build.js"]);

//Also remove spaces, since rhino and node differ on their
//Function.prototype.toString() output by whitespace, and
//the semicolon on end of A.name
t.is(nol(c("lib/legacyBasic/expected.js")).replace(/\s+/g, '').replace(/A\.name\;/g, 'A.name'),
nol(c(outFile)).replace(/\s+/g, '').replace(/A\.name\;/g, 'A.name'));

require._buildReset();
}

]
);
doh.run();

});
7 changes: 7 additions & 0 deletions build/tests/lib/legacyBasic/build.js
@@ -0,0 +1,7 @@
{
baseUrl: '../../../../../requirejs/tests/legacy',
mainConfigFile: '../../../../../requirejs/tests/legacy/basic-tests.js',
name: 'basic-tests',
out: '../../../../../requirejs/tests/legacy/built/basic-tests.js',
optimize: 'none'
}
74 changes: 74 additions & 0 deletions build/tests/lib/legacyBasic/expected.js
@@ -0,0 +1,74 @@

(function (root) {
root.A = {
name: 'a'
};
}(this));

define("a", (function (global) {
return function () {
var func = function (){return this.A.name};
return func.apply(global, arguments);
}
}(this)));

function D() {
this.name = 'd';
};

define("d", function(){});

var B = {
name: 'b',
aValue: A.name,
dValue: new D()
};

define("b", function(){});

var C = {
name: 'c',
a: A,
b: B
};

define("c", ["a","b"], (function (global) {
return function () {
return global["C"];
}
}(this)));

require({
baseUrl: './',
legacy: {
a: {
exports: function () {
return this.A.name;
}
},
'b': ['a', 'd'],
'c': {
deps: ['a', 'b'],
exports: 'C'
}
}
},
['a', 'c'],
function(a, c) {
doh.register(
'legacyBasic',
[
function legacyBasic(t){
t.is('a', a);
t.is('a', c.b.aValue);
t.is('b', c.b.name);
t.is('c', c.name);
t.is('d', c.b.dValue.name);
}
]
);
doh.run();
}
);

define("basic-tests", function(){});

0 comments on commit 566c4fc

Please sign in to comment.