From b0cfdeb37e377307c8a008b4c0ecae83a0a8f5f0 Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Mon, 25 Sep 2017 13:30:42 -0600 Subject: [PATCH 1/7] Support @steal in slim build Closes #849 --- lib/build/slim.js | 4 +- lib/slim/checks/steal.js | 36 ----------- lib/stream/add_steal_shim.js | 103 ++++++++++++++++++++++++++++++ test/slim/at_steal/index.html | 10 +++ test/slim/at_steal/main.js | 5 ++ test/slim/at_steal/stealconfig.js | 8 +++ test/slim_build_test.js | 34 +++++++++- test/slim_support_checks_test.js | 27 -------- test/test.js | 1 - 9 files changed, 161 insertions(+), 67 deletions(-) delete mode 100644 lib/slim/checks/steal.js create mode 100644 lib/stream/add_steal_shim.js create mode 100644 test/slim/at_steal/index.html create mode 100644 test/slim/at_steal/main.js create mode 100644 test/slim/at_steal/stealconfig.js delete mode 100644 test/slim_support_checks_test.js diff --git a/lib/build/slim.js b/lib/build/slim.js index 9dc7ba0f..5b7e2b28 100644 --- a/lib/build/slim.js +++ b/lib/build/slim.js @@ -17,10 +17,10 @@ var addModuleIds = require("../stream/add_module_ids"); var addBundleIds = require("../stream/add_bundle_ids"); var filterGraph = require("../stream/filter_slim_graph"); var addPluginNames = require("../stream/add_plugin_names"); +var addAtStealShim = require("../stream/add_steal_shim"); var addAtLoaderShim = require("../stream/add_loader_shim"); var cloneBuildData = require("../stream/clone_build_data"); var loadNodeBuilder = require("../stream/load_node_builder"); -var checkSlimSupport = require("../stream/check_slim_support"); var convertSlimConfig = require("../stream/convert_slim_config"); var adjustBundlesPath = require("../stream/adjust_bundles_path"); var write = require("../bundle/write_bundles").createWriteStream; @@ -60,7 +60,7 @@ module.exports = function(cfg, opts) { graph(config, options), buildType("optimize"), filterGraph(), - checkSlimSupport(), + addAtStealShim(), addAtLoaderShim(), addModuleIds(), convertSlimConfig(), diff --git a/lib/slim/checks/steal.js b/lib/slim/checks/steal.js deleted file mode 100644 index a0294acf..00000000 --- a/lib/slim/checks/steal.js +++ /dev/null @@ -1,36 +0,0 @@ -var keys = require("lodash/keys"); -var isPluginExcludedFromBuild = require("../../node/is_plugin_excluded"); - -/** - * Checks whether the @steal node is in the graph - * @param {string} configMain - The configMain module name - * @throws if @steal is found in the graph - */ -module.exports = function(configMain, graph) { - keys(graph).forEach(function(name) { - var node = graph[name]; - - // the configMain node does depend on @steal/@loader but it won't be - // included in the slim build, we can skip it. Same for plugins that - // will be only be used during the build process. - if (name === configMain || isPluginExcludedFromBuild(node)) { - return; - } - - isAtSteal(name); - node.dependencies.forEach(isAtSteal); - }); -}; - -/** - * Checks whether the module name is @steal - * @param {string} name - The module name to be checked - * @throws if the name is @steal - */ -function isAtSteal(name) { - if (name === "@steal") { - throw new Error( - `Cannot create slim build. "@steal" module is not supported` - ); - } -} diff --git a/lib/stream/add_steal_shim.js b/lib/stream/add_steal_shim.js new file mode 100644 index 00000000..c7b18883 --- /dev/null +++ b/lib/stream/add_steal_shim.js @@ -0,0 +1,103 @@ +var colors = require("colors"); +var through = require("through2"); +var keys = require("lodash/keys"); +var omit = require("lodash/omit"); +var defaultTo = require("lodash/defaultTo"); +var isPluginExcludedFromBuild = require("../node/is_plugin_excluded"); + +module.exports = function() { + return through.obj(function(data, enc, next) { + try { + next(null, addAtLoaderShim(data)); + } catch (err) { + next(err); + } + }); +}; + +/** + * Adds a node to the graph with an "@steal" shim + * @param {Object} data - The slim stream data object + * @return {Object} The mutated stream object + */ +function addAtLoaderShim(data) { + var graph = omit(data.graph, data.loader.configMain); + + if (includesAtSteal(graph)) { + console.log( + colors.yellow( + `Warning: the @steal module is not fully supported in optimized builds` + ) + ); + data.graph["@steal"] = makeShimNode(data.loader.main); + } + + return data; +} + +/** + * Looks for "@steal" in the dependency graph + * @param {Object} graph - The dependency graph + * @return {boolean} true if found, false otherwise + */ +function includesAtSteal(graph) { + var found = false; + + var isAtSteal = function(name) { + return name === "@steal"; + }; + + keys(graph).forEach(function(name) { + var node = graph[name]; + + if (isPluginExcludedFromBuild(node)) { + return; + } + + if (isAtSteal(name)) { + return (found = true); + } + + defaultTo(node.dependencies, []).forEach(function(depName) { + if (isAtSteal(depName)) { + return (found = true); + } + }); + }); + + return found; +} + +/** + * Returns an @steal shim graph node + * @param {string} main - The main module name + * @return {Object} The faux "@steal" graph node + */ +function makeShimNode(main) { + return { + bundles: [main], + dependencies: ["@loader"], + deps: ["@loader"], + load: { + address: "", + metadata: { + format: "amd", + deps: ["@loader"], + dependencies: ["@loader"] + }, + name: "@steal", + source: ` + define("@steal", ["@loader"], function(atLoader) { + var steal = {}; + + steal.loader = atLoader; + steal.done = function() { + return Promise.resolve(); + }; + + return steal; + }); + ` + } + }; +} diff --git a/test/slim/at_steal/index.html b/test/slim/at_steal/index.html new file mode 100644 index 00000000..e92fe852 --- /dev/null +++ b/test/slim/at_steal/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/slim/at_steal/main.js b/test/slim/at_steal/main.js new file mode 100644 index 00000000..f052df0d --- /dev/null +++ b/test/slim/at_steal/main.js @@ -0,0 +1,5 @@ +var steal = require("@steal"); + +steal.done().then(function() { + window.atSteal = steal; +}); diff --git a/test/slim/at_steal/stealconfig.js b/test/slim/at_steal/stealconfig.js new file mode 100644 index 00000000..cdda8858 --- /dev/null +++ b/test/slim/at_steal/stealconfig.js @@ -0,0 +1,8 @@ +steal.config({ + main: "main", + envs: { + "window-production": { + serviceBaseURL: "/api/production" + } + } +}); diff --git a/test/slim_build_test.js b/test/slim_build_test.js index b4490c83..d9bb505b 100644 --- a/test/slim_build_test.js +++ b/test/slim_build_test.js @@ -378,7 +378,6 @@ describe("slim builds", function() { }); }); - it("rejects build promise if unknown target passed in", function(done) { var base = path.join(__dirname, "slim", "basics"); var config = { config: path.join(base, "stealconfig.js") }; @@ -643,4 +642,37 @@ describe("slim builds", function() { }); }); }); + + it("has partial support for @steal usage", function() { + var base = path.join(__dirname, "slim", "at_steal"); + var config = { config: path.join(base, "stealconfig.js") }; + + return rmdir(path.join(base, "dist")) + .then(function() { + return optimize(config, { minify: false, quiet: true }); + }) + .then(function() { + return open(path.join("test", "slim", "at_steal", "index.html")); + }) + .then(function(args) { + return Promise.all([args.close, find(args.browser, "atSteal")]); + }) + .then(function(data) { + var close = data[0]; + var atSteal = data[1]; + + assert.ok( + typeof steal.done === "function", + "should include a .done function" + ); + + assert.equal( + atSteal.loader.serviceBaseURL, + "/api/production", + "should include @loader properties" + ); + + close(); + }); + }); }); diff --git a/test/slim_support_checks_test.js b/test/slim_support_checks_test.js deleted file mode 100644 index 6dabc416..00000000 --- a/test/slim_support_checks_test.js +++ /dev/null @@ -1,27 +0,0 @@ -var assert = require("assert"); - -describe("slim support checks", function() { - describe("checkSteal", function() { - var checkSteal = require("../lib/slim/checks/steal"); - - it("throws if @steal is in the graph", function() { - assert.throws( - function() { - checkSteal("stealconfig.js", { - main: { - dependencies: ["@steal"] - }, - "stealconfig.js": {} - }); - }, - function(err) { - return /"@steal" module is not supported/.test(err.message); - } - ); - }); - - it("does not throw if @steal missing from graph", function() { - assert.equal(checkSteal({}), undefined); - }); - }); -}); diff --git a/test/test.js b/test/test.js index 92a9465d..73d732fc 100644 --- a/test/test.js +++ b/test/test.js @@ -14,7 +14,6 @@ require("./cli/make_outputs_test"); require("./cli/cmd_live_test"); require("./get_es_module_imports_test"); require("./cli/make_build_options_test"); -require("./slim_support_checks_test"); require("./slim_build_conditionals_test"); // Integration tests From fbbfdecd98fdc7abf6f45a12e68c01d067fcb05e Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Mon, 25 Sep 2017 15:07:04 -0600 Subject: [PATCH 2/7] Remove check for Symbol support in test file steal-tool does not support node 0.1x --- test/test.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/test/test.js b/test/test.js index 92a9465d..5ca1038a 100644 --- a/test/test.js +++ b/test/test.js @@ -19,13 +19,7 @@ require("./slim_build_conditionals_test"); // Integration tests require("./test_cli"); - -// Node 0.10 doesn't support Symbols so the live-reload tests will -// not pass on it. -if(typeof Symbol !== "undefined") { - require("./test_live"); -} - +require("./test_live"); require("./bundle_name_test"); require("./dependencygraph_test"); require("./bundle_test"); From 137a64e6305bdd4a67170aef58f277892c8e853a Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Wed, 27 Sep 2017 12:30:13 -0600 Subject: [PATCH 3/7] Support AMD "module" import in slim builds Closes #845 --- package.json | 4 ++-- test/slim/module/index.html | 10 ++++++++++ test/slim/module/main.js | 3 +++ test/slim/module/stealconfig.js | 3 +++ test/slim_build_test.js | 23 +++++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/slim/module/index.html create mode 100644 test/slim/module/main.js create mode 100644 test/slim/module/stealconfig.js diff --git a/package.json b/package.json index 3cfba114..d19c5e86 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "through2": "^2.0.0", "tmp": "0.0.33", "traceur": "0.0.111", - "transpile": "^2.5.3", + "transpile": "^2.5.5", "uglify-es": "^3.0.26", "urix": "^0.1.0", "winston": "^2.2.0", @@ -88,7 +88,7 @@ "coverage": "istanbul cover _mocha -- test/test --timeout 600000", "coverage:upload": "istanbul cover _mocha --report lcovonly -- test/test --timeout 600000 && cat ./coverage/lcov.info | ./node_modules/coveralls-send/bin/coveralls.js", "test:slim-worker-single": "node test/slim/worker/single/build.js && testee test/slim/worker/single/worker.html --browsers firefox --reporter Spec", - "test:slim-worker-progressive": "node test/slim/worker/progressive/build.js && testee test/slim/worker/progressive/worker.html --browsers firefox --reporter Spec", + "test:slim-worker-progressive": "node test/slim/worker/progressive/build.js && testee test/slim/worker/progressive/worker.html --browsers firefox --reporter Spec", "test:exports-worker": "node test/exports_worker/exports.js && testee test/exports_worker/worker.html --browsers firefox --reporter Spec" }, "engines": { diff --git a/test/slim/module/index.html b/test/slim/module/index.html new file mode 100644 index 00000000..6ffe1550 --- /dev/null +++ b/test/slim/module/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/slim/module/main.js b/test/slim/module/main.js new file mode 100644 index 00000000..92c6f9a7 --- /dev/null +++ b/test/slim/module/main.js @@ -0,0 +1,3 @@ +define(["module"], function(module) { + window.moduleId = module.id; +}); diff --git a/test/slim/module/stealconfig.js b/test/slim/module/stealconfig.js new file mode 100644 index 00000000..5f703aa0 --- /dev/null +++ b/test/slim/module/stealconfig.js @@ -0,0 +1,3 @@ +steal.config({ + main: "main" +}); diff --git a/test/slim_build_test.js b/test/slim_build_test.js index d9bb505b..e28dcbbc 100644 --- a/test/slim_build_test.js +++ b/test/slim_build_test.js @@ -675,4 +675,27 @@ describe("slim builds", function() { close(); }); }); + + it("supports 'module' imports in AMD modules", function() { + var base = path.join(__dirname, "slim", "module"); + var config = { config: path.join(base, "stealconfig.js") }; + + return rmdir(path.join(base, "dist")) + .then(function() { + return optimize(config, { minify: false, quiet: true }); + }) + .then(function() { + return open(path.join("test", "slim", "module", "index.html")); + }) + .then(function(args) { + return Promise.all([args.close, find(args.browser, "moduleId")]); + }) + .then(function(data) { + var close = data[0]; + var moduleId = data[1]; + + assert.equal(moduleId, "main", "should set module.id"); + close(); + }); + }); }); From 39f58eeb562b898c106118f37e90161bd5e3160c Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Thu, 28 Sep 2017 17:31:09 -0600 Subject: [PATCH 4/7] Do not deep clone the graph and loader objects Closes #855 --- lib/build/slim.js | 2 -- 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(+), 34 deletions(-) delete mode 100644 lib/stream/clone_build_data.js diff --git a/lib/build/slim.js b/lib/build/slim.js index 5b7e2b28..5a508940 100644 --- a/lib/build/slim.js +++ b/lib/build/slim.js @@ -19,7 +19,6 @@ var filterGraph = require("../stream/filter_slim_graph"); var addPluginNames = require("../stream/add_plugin_names"); var addAtStealShim = require("../stream/add_steal_shim"); var addAtLoaderShim = require("../stream/add_loader_shim"); -var cloneBuildData = require("../stream/clone_build_data"); var loadNodeBuilder = require("../stream/load_node_builder"); var convertSlimConfig = require("../stream/convert_slim_config"); var adjustBundlesPath = require("../stream/adjust_bundles_path"); @@ -79,7 +78,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 } + ); } From b7a296253653c3587bf2cf62653d7cb7f2d6ea0a Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Thu, 28 Sep 2017 18:19:16 -0600 Subject: [PATCH 5/7] Do not deep clone bundles --- lib/graph/slim_graph.js | 20 ++++++++++++-------- lib/stream/slim.js | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/graph/slim_graph.js b/lib/graph/slim_graph.js index 6ed630cb..db1d0d9a 100644 --- a/lib/graph/slim_graph.js +++ b/lib/graph/slim_graph.js @@ -1,9 +1,10 @@ +var omit = require("lodash/omit"); var first = require("lodash/first"); var negate = require("lodash/negate"); var concat = require("lodash/concat"); +var assign = require("lodash/assign"); var partial = require("lodash/partial"); var includes = require("lodash/includes"); -var cloneDeep = require("lodash/cloneDeep"); var isJavaScriptBundle = require("../bundle/is_js_bundle"); var makeSlimShimNode = require("../node/make_slim_shim_node"); var makeSlimBundleNode = require("../node/make_slim_bundle_node"); @@ -82,7 +83,7 @@ module.exports = function(options) { var secondaryBundles = jsBundles.filter(negate(isEntryPointBundle)); // each entry point bundle includes the loader code - entryPointBundles.forEach(function(bundle) { + var slimmedEntryBundles = entryPointBundles.map(function(bundle) { var mainName = getMainName(bundle); var sharedBundles = getSharedBundlesOf( @@ -92,10 +93,10 @@ module.exports = function(options) { mainName ); - bundle.nodes = [ + var nodes = [ slimConfigNode, makeSlimShimNode({ - nodes: bundle.nodes, + nodes: bundle.nodes.slice(0), target: options.target, sharedBundles: sharedBundles, plugins: !!nonJsBundles.length, @@ -105,10 +106,13 @@ module.exports = function(options) { }) ]; + return assign({}, omit(bundle, ["nodes"]), { + nodes: nodes + }); }); slimmedBundles = concat( - entryPointBundles, + slimmedEntryBundles, secondaryBundles.map(partial(toSlimBundle, options.target)), nonJsBundles ); @@ -143,9 +147,9 @@ function getSharedBundlesOf(bundles, mainName) { } function toSlimBundle(target, bundle) { - var cloned = cloneDeep(bundle); - cloned.nodes = [makeSlimBundleNode(target, cloned)]; - return cloned; + return assign({}, omit(bundle, ["nodes"]), { + nodes: [makeSlimBundleNode(target, bundle)] + }); } function makeLoaderBundle(nodes) { diff --git a/lib/stream/slim.js b/lib/stream/slim.js index eed1fa43..8123f0d6 100644 --- a/lib/stream/slim.js +++ b/lib/stream/slim.js @@ -26,7 +26,7 @@ module.exports = function(options) { * @return {Object} The mutated data */ function doSlimGrap(data, options) { - var bundles = cloneDeep(data.bundles); + var bundles = data.bundles.slice(0); var slimmedBundles = slimGraph({ graph: data.graph, From e9fc68dd1e7fbcff38611e73027d492312790264 Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Thu, 28 Sep 2017 18:25:27 -0600 Subject: [PATCH 6/7] Remove unused import --- lib/stream/slim.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/stream/slim.js b/lib/stream/slim.js index 8123f0d6..08de6426 100644 --- a/lib/stream/slim.js +++ b/lib/stream/slim.js @@ -1,7 +1,6 @@ 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"); /** From 2e8b957ba791e3b800ffcb35e9cd7affc22e262e Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Fri, 29 Sep 2017 11:04:31 -0600 Subject: [PATCH 7/7] 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(); + }); + }); });