From 9403b95417d1f8dff6fec23cb554582568158c12 Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Thu, 28 Sep 2017 17:24:28 -0600 Subject: [PATCH] Do not deep clone the graph and loader objects Closes #855 --- lib/build/slim.js | 1 - lib/node/slim/make_shim_template.js | 2 +- lib/stream/adjust_bundles_path.js | 41 +++++++++++++++++++---------- lib/stream/clone_build_data.js | 12 --------- lib/stream/load_node_builder.js | 3 +-- lib/stream/slim.js | 15 ++++++++--- 6 files changed, 41 insertions(+), 33 deletions(-) delete mode 100644 lib/stream/clone_build_data.js diff --git a/lib/build/slim.js b/lib/build/slim.js index 5b7e2b28..c8158723 100644 --- a/lib/build/slim.js +++ b/lib/build/slim.js @@ -79,7 +79,6 @@ module.exports = function(cfg, opts) { var final = pump( initialStream, - cloneBuildData(), adjustBundlesPath({ target: target }), // the "" target is relevant for this transform slimBundles({ target: target || "web" }), // set default target so there is no need to handle "" concat(), diff --git a/lib/node/slim/make_shim_template.js b/lib/node/slim/make_shim_template.js index 65263ae5..f9a77686 100644 --- a/lib/node/slim/make_shim_template.js +++ b/lib/node/slim/make_shim_template.js @@ -132,7 +132,7 @@ module.exports = function(options) { ${renderProgressivePartial(options)} - ${options.resolve ? resolveHook.baseResolve : ""}; + ${options.resolve ? resolveHook.baseResolve : ""} ${options.extensions ? importSlimExtensionsPartial : ""} diff --git a/lib/stream/adjust_bundles_path.js b/lib/stream/adjust_bundles_path.js index b0327dc1..fdf9043c 100644 --- a/lib/stream/adjust_bundles_path.js +++ b/lib/stream/adjust_bundles_path.js @@ -1,29 +1,42 @@ var through = require("through2"); +var omit = require("lodash/omit"); +var clone = require("lodash/clone"); +var assign = require("lodash/assign"); module.exports = function(options) { return through.obj(function(data, enc, next) { try { - next(null, adjustBundlesPath(data, options)); + next(null, options.target ? adjustBundlesPath(data, options) : data); } catch (err) { next(err); } }); }; +/** + * Appends the target name to the bundles path + * + * Each target build should be written in its own subfolder, e.g: + * + * dist/bundles/node + * dist/bundles/web + * dist/bundles/worker + * + * This should only happen when target is explicitly set. + */ function adjustBundlesPath(data, options) { - if (options.target) { - var path = require("path"); + var path = require("path"); + var configuration = clone(data.configuration); - // override the configuration getter for `bundlesPath` so it - // includes the target name - var bundlesPath = data.configuration.bundlesPath; - Object.defineProperty(data.configuration, "bundlesPath", { - configurable: true, - get: function() { - return path.join(bundlesPath, options.target); - } - }); - } + var bundlesPath = configuration.bundlesPath; + Object.defineProperty(configuration, "bundlesPath", { + configurable: true, + get: function() { + return path.join(bundlesPath, options.target); + } + }); - return data; + return assign({}, omit(data, ["configuration"]), { + configuration: configuration + }); } diff --git a/lib/stream/clone_build_data.js b/lib/stream/clone_build_data.js deleted file mode 100644 index b3f056de..00000000 --- a/lib/stream/clone_build_data.js +++ /dev/null @@ -1,12 +0,0 @@ -var through = require("through2"); -var clone = require("lodash/cloneDeep"); - -module.exports = function() { - return through.obj(function(data, enc, next) { - try { - next(null, clone(data)); - } catch (err) { - next(err); - } - }); -}; diff --git a/lib/stream/load_node_builder.js b/lib/stream/load_node_builder.js index cafaf555..0642b6d7 100644 --- a/lib/stream/load_node_builder.js +++ b/lib/stream/load_node_builder.js @@ -1,7 +1,6 @@ var keys = require("lodash/keys"); var through = require("through2"); var assign = require("lodash/assign"); -var cloneDeep = require("lodash/cloneDeep"); module.exports = function() { return through.obj(function(data, enc, next) { @@ -20,8 +19,8 @@ module.exports = function() { */ function loadNodeBuilder(data) { return new Promise(function(resolve, reject) { + var graph = data.graph; var loader = data.loader; - var graph = cloneDeep(data.graph); var promises = keys(graph).map(function(nodeName) { var node = graph[nodeName]; diff --git a/lib/stream/slim.js b/lib/stream/slim.js index 7cdf2546..eed1fa43 100644 --- a/lib/stream/slim.js +++ b/lib/stream/slim.js @@ -1,4 +1,7 @@ +var omit = require("lodash/omit"); var through = require("through2"); +var assign = require("lodash/assign"); +var cloneDeep = require("lodash/cloneDeep"); var slimGraph = require("../graph/slim_graph"); /** @@ -23,10 +26,12 @@ module.exports = function(options) { * @return {Object} The mutated data */ function doSlimGrap(data, options) { - data.bundles = slimGraph({ + var bundles = cloneDeep(data.bundles); + + var slimmedBundles = slimGraph({ graph: data.graph, mains: data.mains, - bundles: data.bundles, + bundles: bundles, target: options.target, baseUrl: data.loader.baseURL, slimConfig: data.loader.slimConfig, @@ -36,7 +41,11 @@ function doSlimGrap(data, options) { progressiveBundles: getProgressiveBundles(data.loader, data.graph) }); - return data; + return assign( + {}, + omit(data, ["bundles"]), + { bundles: slimmedBundles } + ); }