Skip to content

Commit c411867

Browse files
committed
Related to requirejs#81, allow mixing of config object values that are objects themselves in a generic fashion. Also have parse.findConfig throw if invalid config in a build context.
1 parent f27b86c commit c411867

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ build/tests/lib/plugins/main-built.js
99
build/tests/lib/plugins/main-builtPluginFirst.js
1010
build/tests/lib/dotpackage/built
1111
build/tests/lib/mainConfigFile/basic/main-built.js
12+
build/tests/lib/mainConfigFile/mergeConfig/main-built.js
1213
build/tests/lib/moduleThenPlugin/built.js
1314
build/tests/lib/nameInsertion/built.js
1415
build/tests/lib/nested/main-built.js

build/jslib/build.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ function (lang, logger, file, parse, optimize, pragma,
570570
if (config.paths) {
571571
for (prop in config.paths) {
572572
if (config.paths.hasOwnProperty(prop)) {
573-
config.paths[prop] = build.makeAbsPath(config.paths[prop], config.baseUrl);
573+
config.paths[prop] = build.makeAbsPath(config.paths[prop],
574+
(config.baseUrl || absFilePath));
574575
}
575576
}
576577
}
@@ -592,11 +593,16 @@ function (lang, logger, file, parse, optimize, pragma,
592593
* nested config, like paths, correctly.
593594
*/
594595
function mixConfig(target, source) {
595-
var prop;
596+
var prop, value;
596597

597598
for (prop in source) {
598599
if (source.hasOwnProperty(prop)) {
599-
if (build.nestedMix[prop]) {
600+
//If the value of the property is a plain object, then
601+
//allow a one-level-deep mixing of it.
602+
value = source[prop];
603+
if (typeof value === 'object' && value &&
604+
!lang.isArray(value) && !lang.isFunction(value) &&
605+
!lang.isRegExp(value)) {
600606
if (!target[prop]) {
601607
target[prop] = {};
602608
}
@@ -657,7 +663,17 @@ function (lang, logger, file, parse, optimize, pragma,
657663
mainConfigFile = config.mainConfigFile || (buildFileConfig && buildFileConfig.mainConfigFile);
658664
if (mainConfigFile) {
659665
mainConfigFile = build.makeAbsPath(mainConfigFile, absFilePath);
660-
mainConfig = parse.findConfig(mainConfigFile, file.readFile(mainConfigFile));
666+
try {
667+
mainConfig = parse.findConfig(mainConfigFile, file.readFile(mainConfigFile));
668+
} catch (configError) {
669+
throw new Error('The config in mainConfigFile ' +
670+
mainConfigFile +
671+
' cannot be used because it cannot be evaluated' +
672+
' correctly while running in the optimizer. Try only' +
673+
' using a config that is also valid JSON, or do not use' +
674+
' mainConfigFile and instead copy the config values needed' +
675+
' into a build file or command line arguments given to the optimizer.');
676+
}
661677
if (mainConfig) {
662678
//If no baseUrl, then use the directory holding the main config.
663679
if (!mainConfig.baseUrl) {

build/jslib/lang.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ define(function () {
1616
return lang.ostring.call(it) === "[object Array]";
1717
},
1818

19+
isFunction: function(it) {
20+
return lang.ostring.call(it) === "[object Function]";
21+
},
22+
23+
isRegExp: function(it) {
24+
return it && it instanceof RegExp;
25+
},
26+
1927
/**
2028
* Simple function to mix in properties from source into target,
2129
* but only if target does not already have a property of the same name.

build/jslib/parse.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ define(['uglifyjs/index'], function (uglify) {
398398
* @param {String} fileContents
399399
*
400400
* @returns {Object} a config object. Will be null if no config.
401+
* Can throw an error if the config in the file cannot be evaluated in
402+
* a build context to valid JavaScript.
401403
*/
402404
parse.findConfig = function (fileName, fileContents) {
403405
/*jslint evil: true */
@@ -412,14 +414,8 @@ define(['uglifyjs/index'], function (uglify) {
412414

413415
if (!foundConfig && configNode) {
414416
jsConfig = parse.nodeToString(configNode);
415-
if (jsConfig) {
416-
try {
417-
foundConfig = eval('(' + jsConfig + ')');
418-
} catch (e) {
419-
foundConfig = null;
420-
}
421-
return foundConfig;
422-
}
417+
foundConfig = eval('(' + jsConfig + ')');
418+
return foundConfig;
423419
}
424420
return undefined;
425421
}, null, parse.parseConfigNode);

build/tests/builds.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,21 @@ define(['build', 'env!env/file'], function (build, file) {
576576
);
577577
doh.run();
578578

579+
doh.register("mainConfigFileMerged",
580+
[
581+
function mainConfigFileMerged(t) {
582+
file.deleteFile("lib/mainConfigFile/mergeConfig/main-built.js");
583+
584+
build(["lib/mainConfigFile/mergeConfig/tools/build.js"]);
585+
586+
t.is(nol(c("lib/mainConfigFile/mergeConfig/expected.js")),
587+
nol(c("lib/mainConfigFile/mergeConfig/main-built.js")));
588+
589+
require._buildReset();
590+
}
591+
592+
]
593+
);
594+
doh.run();
595+
579596
});

build/tests/parse.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ define(['parse', 'env!env/file'], function (parse, file) {
66
doh.register("parseConfig",
77
[
88
function parseConfig(t) {
9+
var hasError = false;
910
t.is('rconfig', parse.findConfig('rconfig.js', file.readFile('parse/rconfig.js')).baseUrl);
1011
t.is('rjsconfig', parse.findConfig('rjsconfig.js', file.readFile('parse/rjsconfig.js')).baseUrl);
1112
t.is('requirec', parse.findConfig('requirec.js', file.readFile('parse/requirec.js')).baseUrl);
1213
t.is('requirejsc', parse.findConfig('requirejsc.js', file.readFile('parse/requirejsc.js')).baseUrl);
1314
t.is(null, parse.findConfig('missing1.js', file.readFile('parse/missing1.js')));
1415
t.is(null, parse.findConfig('missing1.js', file.readFile('parse/missing1.js')));
15-
t.is(null, parse.findConfig('bad1.js', file.readFile('parse/bad1.js')));
16+
17+
try {
18+
parse.findConfig('bad1.js', file.readFile('parse/bad1.js'));
19+
} catch (e) {
20+
hasError = true;
21+
}
22+
t.is(true, hasError);
1623
}
1724
]
1825
);

0 commit comments

Comments
 (0)