Skip to content

Commit

Permalink
Introduce normalizeDefines to speed up builds, Fixes #315
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Nov 17, 2012
1 parent b366ad7 commit 93cc4a3
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 13 deletions.
19 changes: 19 additions & 0 deletions build/example.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@
//- "none": no minification will be done.
optimize: "uglify",

//If a full directory optimization via "dir", and optimize is not "none",
//then normally all JS files in the directory will be minifized, and this
//value is automatically set to "all". For JS files to properly work after a
//minification, the optimizer will parse for define() calls and insert any
//dependency arrays that are missing. However, this can be a bit slow if
//there are many/larger JS files. So this transport normalization is not
//done (automatically set to "skip") if optimize is set to "none". Cases
//where you may want to manually set this value:
//1) Optimizing later: if you plan on minifying the non-build layer JS files
//after the optimizer runs, then you should explicitly this value to "all".
//2) Optimizing, but not dynamically loading later: you want to do a full
//project optimization, but do not plan on dynamically loading non-build
//layer JS files later. In this case, the normalization just slows down
//builds, so you can explicitly set this value to "skip".
//Finally, all build layers (specified in the "modules" or "out" setting)
//automatically get normalization, so this setting does not apply to those
//files.
normalizeDefines: "skip",

//If using UglifyJS for script optimization, these config options can be
//used to pass configuration values to UglifyJS.
//See https://github.com/mishoo/UglifyJS for the possible values.
Expand Down
31 changes: 24 additions & 7 deletions build/jslib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ define(function (require) {
//JS optimizations.
fileNames = file.getFilteredFileList(config.dir, /\.js$/, true);
fileNames.forEach(function (fileName, i) {
var cfg, moduleIndex, override;
var cfg, override, moduleIndex;

//Generate the module name from the config.dir root.
moduleName = fileName.replace(config.dir, '');
Expand All @@ -527,16 +527,23 @@ define(function (require) {
fileContents = commonJs.convert(fileName, fileContents);
}

fileContents = build.toTransport(config.namespace,
null,
fileName,
fileContents);

//If there is an override for a specific layer build module,
//and this file is that module, mix in the override for use
//by optimize.jsFile.
moduleIndex = getOwn(config._buildPathToModuleIndex, fileName);
override = moduleIndex === 0 || moduleIndex > 0 ?
//Normalize, since getOwn could have returned undefined
moduleIndex = moduleIndex === 0 || moduleIndex > 0 ? moduleIndex : -1;

//Only do transport normalization if this is not a build layer
//and if normalizeDefines indicated all should be done.
if (moduleIndex === -1 && config.normalizeDefines === "all") {
fileContents = build.toTransport(config.namespace,
null,
fileName,
fileContents);
}

override = moduleIndex > -1 ?
config.modules[moduleIndex].override : null;
if (override) {
cfg = build.createOverrideConfig(config, override);
Expand Down Expand Up @@ -1071,6 +1078,16 @@ define(function (require) {
' pages.');
}

//Set up normalizeDefines. If not explicitly set, if optimize "none",
//set to "skip" otherwise set to "all".
if (!hasProp(config, 'normalizeDefines')) {
if (config.optimize === 'none') {
config.normalizeDefines = 'skip';
} else {
config.normalizeDefines = 'all';
}
}

//Set file.fileExclusionRegExp if desired
if (hasProp(config, 'fileExclusionRegExp')) {
if (typeof config.fileExclusionRegExp === "string") {
Expand Down
2 changes: 1 addition & 1 deletion build/tests/lib/appDirSrcOverwrite/expected-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ require(['somelib'], function (somelib) {

});

define("app",[], function(){});
define("app", function(){});
2 changes: 1 addition & 1 deletion build/tests/lib/empty/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ define('sub1',['empty1'], function () {});

require(['sub1'], function () {});

define("main",[], function(){});
define("main", function(){});

define('sub2',['empty2'], function () {});
2 changes: 1 addition & 1 deletion build/tests/lib/mainConfigBaseUrl/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ require(['sub'], function (sub) {
console.log(sub.name);
});

define("main",[], function(){});
define("main", function(){});
2 changes: 1 addition & 1 deletion build/tests/lib/paths/valid/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ define('../paths',[

require(['../paths']);

define("main",[], function(){});
define("main", function(){});
2 changes: 1 addition & 1 deletion build/tests/lib/requireHoist/perLayer/expectedMain1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ var requirejs, require, define;
req(cfg);
}(this));

define("requireLib",[], function(){});
define("requireLib", function(){});

define('main1',{
name: 'main1'
Expand Down
2 changes: 1 addition & 1 deletion build/tests/lib/requireHoist/perLayer/expectedMain2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ var requirejs, require, define;
req(cfg);
}(this));

define("requireLib",[], function(){});
define("requireLib", function(){});

define('main2',{
name: 'main2'
Expand Down

0 comments on commit 93cc4a3

Please sign in to comment.