From 2e8b957ba791e3b800ffcb35e9cd7affc22e262e Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Fri, 29 Sep 2017 11:04:31 -0600 Subject: [PATCH] Stop plucking @loader from graph in optimized builds Closes #856 --- lib/build/slim.js | 5 ++++- lib/graph/pluck.js | 11 ++++++----- lib/stream/transpile.js | 9 ++++++--- test/slim/at_loader/npm.html | 10 ++++++++++ test/slim/at_loader/package.json | 8 ++++++++ test/slim_build_test.js | 22 ++++++++++++++++++++++ 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 test/slim/at_loader/npm.html create mode 100644 test/slim/at_loader/package.json diff --git a/lib/build/slim.js b/lib/build/slim.js index 5a508940..af1da127 100644 --- a/lib/build/slim.js +++ b/lib/build/slim.js @@ -64,7 +64,10 @@ module.exports = function(cfg, opts) { addModuleIds(), convertSlimConfig(), loadNodeBuilder(), - transpile({ outputFormat: "slim" }), + transpile({ + outputFormat: "slim", + keepInGraph: ["@steal", "@loader"] + }), bundle(), addPluginNames(), addBundleIds(), diff --git a/lib/graph/pluck.js b/lib/graph/pluck.js index 493c86d7..6fbb3773 100644 --- a/lib/graph/pluck.js +++ b/lib/graph/pluck.js @@ -1,19 +1,20 @@ +var includes = require("lodash/includes"); // removes a module and all of its dependencies from the graph. -module.exports = function(graph, name) { +module.exports = function(graph, name, _except) { var modules = []; var visited = {}; + var except = _except == null ? [] : _except; function visit(name) { - if (!visited[name]) { + if (!visited[name] && !includes(except, name)) { visited[name] = true; var node = graph[name]; delete graph[name]; + if (node) { - node.dependencies.forEach(function(moduleName) { - visit(moduleName); - }); + node.dependencies.forEach(visit); modules.push(node); } } diff --git a/lib/stream/transpile.js b/lib/stream/transpile.js index fe5d537a..e33c60a3 100644 --- a/lib/stream/transpile.js +++ b/lib/stream/transpile.js @@ -23,11 +23,14 @@ function transpileGraph(data, transpileOptions) { // Remove @config so it is not transpiled. It is a global, // but we will want it to run ASAP. - var configGraph = pluck(dependencyGraph, - data.loader.configMain || "@config"); + var configGraph = pluck( + dependencyGraph, + data.loader.configMain || "@config", + transpileOptions.keepInGraph || [] + ); // Remove steal dev from production builds. - pluck(dependencyGraph,"@dev"); + pluck(dependencyGraph, "@dev", transpileOptions.keepInGraph || []); // Clean development code if the option was passed if(options.removeDevelopmentCode) { diff --git a/test/slim/at_loader/npm.html b/test/slim/at_loader/npm.html new file mode 100644 index 00000000..40e6dcce --- /dev/null +++ b/test/slim/at_loader/npm.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/slim/at_loader/package.json b/test/slim/at_loader/package.json new file mode 100644 index 00000000..2269753a --- /dev/null +++ b/test/slim/at_loader/package.json @@ -0,0 +1,8 @@ +{ + "name": "at_loader", + "version": "0.0.1", + "main": "main.js", + "steal": { + "serviceBaseURL": "/api/production" + } +} diff --git a/test/slim_build_test.js b/test/slim_build_test.js index e28dcbbc..8832d234 100644 --- a/test/slim_build_test.js +++ b/test/slim_build_test.js @@ -698,4 +698,26 @@ describe("slim builds", function() { close(); }); }); + + it("keeps @loader in the graph when using the npm plugin", function() { + var base = path.join(__dirname, "slim", "at_loader"); + var config = { config: path.join(base, "package.json!npm") }; + + return rmdir(path.join(base, "dist")) + .then(function() { + return optimize(config, { minify: false, quiet: true }); + }) + .then(function() { + return open(path.join("test", "slim", "at_loader", "npm.html")); + }) + .then(function(args) { + return Promise.all([args.close, find(args.browser, "window")]); + }) + .then(function(data) { + var w = data[1]; + var close = data[0]; + assert.equal(w.serviceBaseUrl, "/api/production", "should work"); + close(); + }); + }); });