Skip to content

Commit

Permalink
Fixes #111 introduce skipDirOptimize to skip non-layer optimizations,…
Browse files Browse the repository at this point in the history
… and refs #315, rename normalizeDefines to normalizeDirDefines
  • Loading branch information
jrburke committed Nov 17, 2012
1 parent c4f1619 commit bed7924
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
30 changes: 20 additions & 10 deletions build/example.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,26 @@
//- "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:
//If using "dir" for an output directory, normally the optimize setting is
//used to optimize the build layers (the "modules" section of the config)
//and any other JS file in the directory. However, if the non-build layer
//JS files will not be loaded after a build, you can skip the optimization
//of those files, to speed up builds. Set this value to true if you want
//to skip optimizing those other non-build layer JS files.
skipDirOptimize: false,

//If a full directory optimization ("dir" is used), and optimize is not "none",
//and skipDirOptimize is false, then normally all JS files in the directory
//will be minified, 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".
//after the optimizer runs (so not as part of running the optimizer), 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
Expand All @@ -108,7 +118,7 @@
//automatically get normalization, so this setting does not apply to those
//files.
//Works in r.js 2.1.2+
normalizeDefines: "skip",
normalizeDirDefines: "skip",

//If using UglifyJS for script optimization, these config options can be
//used to pass configuration values to UglifyJS.
Expand Down
76 changes: 43 additions & 33 deletions build/jslib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,43 +515,53 @@ define(function (require) {
//Get rid of the extension
moduleName = moduleName.substring(0, moduleName.length - 3);

//Convert the file to transport format, but without a name
//inserted (by passing null for moduleName) since the files are
//standalone, one module per file.
fileContents = file.readFile(fileName);

//For builds, if wanting cjs translation, do it now, so that
//the individual modules can be loaded cross domain via
//plain script tags.
if (config.cjsTranslate) {
fileContents = commonJs.convert(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);
//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);
}
//Try to avoid extra work if the other files do not need to
//be read. Build layers should be processed at the very
//least for optimization.
if (moduleIndex > -1 || !config.skipDirOptimize ||
config.normalizeDirDefines === "all" ||
config.cjsTranslate) {
//Convert the file to transport format, but without a name
//inserted (by passing null for moduleName) since the files are
//standalone, one module per file.
fileContents = file.readFile(fileName);

//For builds, if wanting cjs translation, do it now, so that
//the individual modules can be loaded cross domain via
//plain script tags.
if (config.cjsTranslate) {
fileContents = commonJs.convert(fileName, fileContents);
}

override = moduleIndex > -1 ?
config.modules[moduleIndex].override : null;
if (override) {
cfg = build.createOverrideConfig(config, override);
} else {
cfg = config;
}
//Only do transport normalization if this is not a build
//layer (since it was already normalized) and if
//normalizeDirDefines indicated all should be done.
if (moduleIndex === -1 && config.normalizeDirDefines === "all") {
fileContents = build.toTransport(config.namespace,
null,
fileName,
fileContents);
}

override = moduleIndex > -1 ?
config.modules[moduleIndex].override : null;
if (override) {
cfg = build.createOverrideConfig(config, override);
} else {
cfg = config;
}

optimize.jsFile(fileName, fileContents, fileName, cfg, pluginCollector);
if (moduleIndex > -1 || !config.skipDirOptimize) {
optimize.jsFile(fileName, fileContents, fileName, cfg, pluginCollector);
}
}
});

//Normalize all the plugin resources.
Expand Down Expand Up @@ -1084,13 +1094,13 @@ define(function (require) {
' pages.');
}

//Set up normalizeDefines. If not explicitly set, if optimize "none",
//Set up normalizeDirDefines. 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';
if (!hasProp(config, 'normalizeDirDefines')) {
if (config.optimize === 'none' || config.skipDirOptimize) {
config.normalizeDirDefines = 'skip';
} else {
config.normalizeDefines = 'all';
config.normalizeDirDefines = 'all';
}
}

Expand Down

0 comments on commit bed7924

Please sign in to comment.