Skip to content

Commit 93cc4a3

Browse files
committed
Introduce normalizeDefines to speed up builds, Fixes requirejs#315
1 parent b366ad7 commit 93cc4a3

File tree

8 files changed

+49
-13
lines changed

8 files changed

+49
-13
lines changed

build/example.build.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@
9090
//- "none": no minification will be done.
9191
optimize: "uglify",
9292

93+
//If a full directory optimization via "dir", and optimize is not "none",
94+
//then normally all JS files in the directory will be minifized, and this
95+
//value is automatically set to "all". For JS files to properly work after a
96+
//minification, the optimizer will parse for define() calls and insert any
97+
//dependency arrays that are missing. However, this can be a bit slow if
98+
//there are many/larger JS files. So this transport normalization is not
99+
//done (automatically set to "skip") if optimize is set to "none". Cases
100+
//where you may want to manually set this value:
101+
//1) Optimizing later: if you plan on minifying the non-build layer JS files
102+
//after the optimizer runs, then you should explicitly this value to "all".
103+
//2) Optimizing, but not dynamically loading later: you want to do a full
104+
//project optimization, but do not plan on dynamically loading non-build
105+
//layer JS files later. In this case, the normalization just slows down
106+
//builds, so you can explicitly set this value to "skip".
107+
//Finally, all build layers (specified in the "modules" or "out" setting)
108+
//automatically get normalization, so this setting does not apply to those
109+
//files.
110+
normalizeDefines: "skip",
111+
93112
//If using UglifyJS for script optimization, these config options can be
94113
//used to pass configuration values to UglifyJS.
95114
//See https://github.com/mishoo/UglifyJS for the possible values.

build/jslib/build.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ define(function (require) {
508508
//JS optimizations.
509509
fileNames = file.getFilteredFileList(config.dir, /\.js$/, true);
510510
fileNames.forEach(function (fileName, i) {
511-
var cfg, moduleIndex, override;
511+
var cfg, override, moduleIndex;
512512

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

530-
fileContents = build.toTransport(config.namespace,
531-
null,
532-
fileName,
533-
fileContents);
534-
535530
//If there is an override for a specific layer build module,
536531
//and this file is that module, mix in the override for use
537532
//by optimize.jsFile.
538533
moduleIndex = getOwn(config._buildPathToModuleIndex, fileName);
539-
override = moduleIndex === 0 || moduleIndex > 0 ?
534+
//Normalize, since getOwn could have returned undefined
535+
moduleIndex = moduleIndex === 0 || moduleIndex > 0 ? moduleIndex : -1;
536+
537+
//Only do transport normalization if this is not a build layer
538+
//and if normalizeDefines indicated all should be done.
539+
if (moduleIndex === -1 && config.normalizeDefines === "all") {
540+
fileContents = build.toTransport(config.namespace,
541+
null,
542+
fileName,
543+
fileContents);
544+
}
545+
546+
override = moduleIndex > -1 ?
540547
config.modules[moduleIndex].override : null;
541548
if (override) {
542549
cfg = build.createOverrideConfig(config, override);
@@ -1071,6 +1078,16 @@ define(function (require) {
10711078
' pages.');
10721079
}
10731080

1081+
//Set up normalizeDefines. If not explicitly set, if optimize "none",
1082+
//set to "skip" otherwise set to "all".
1083+
if (!hasProp(config, 'normalizeDefines')) {
1084+
if (config.optimize === 'none') {
1085+
config.normalizeDefines = 'skip';
1086+
} else {
1087+
config.normalizeDefines = 'all';
1088+
}
1089+
}
1090+
10741091
//Set file.fileExclusionRegExp if desired
10751092
if (hasProp(config, 'fileExclusionRegExp')) {
10761093
if (typeof config.fileExclusionRegExp === "string") {

build/tests/lib/appDirSrcOverwrite/expected-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ require(['somelib'], function (somelib) {
88

99
});
1010

11-
define("app",[], function(){});
11+
define("app", function(){});

build/tests/lib/empty/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ define('sub1',['empty1'], function () {});
33

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

6-
define("main",[], function(){});
6+
define("main", function(){});
77

88
define('sub2',['empty2'], function () {});

build/tests/lib/mainConfigBaseUrl/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ require(['sub'], function (sub) {
1111
console.log(sub.name);
1212
});
1313

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

build/tests/lib/paths/valid/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ define('../paths',[
2828

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

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

build/tests/lib/requireHoist/perLayer/expectedMain1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ var requirejs, require, define;
20412041
req(cfg);
20422042
}(this));
20432043

2044-
define("requireLib",[], function(){});
2044+
define("requireLib", function(){});
20452045

20462046
define('main1',{
20472047
name: 'main1'

build/tests/lib/requireHoist/perLayer/expectedMain2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ var requirejs, require, define;
20412041
req(cfg);
20422042
}(this));
20432043

2044-
define("requireLib",[], function(){});
2044+
define("requireLib", function(){});
20452045

20462046
define('main2',{
20472047
name: 'main2'

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