From 3f7fc92bc89fdad4ddcbc3cb7abfde8a2429f0bd Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Mon, 22 Jan 2018 15:08:02 -0700 Subject: [PATCH] Allow plugins to include the slim dynamic loader Closes #931 --- lib/graph/slim_graph.js | 18 ++++++++++++++++-- test/slim/dynamic/foo.plug | 1 + test/slim/dynamic/main.js | 1 + test/slim/dynamic/plug.js | 15 +++++++++++++++ test/slim/dynamic/stealconfig.js | 3 +++ test/slim_build_test.js | 19 +++++++++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/slim/dynamic/foo.plug create mode 100644 test/slim/dynamic/main.js create mode 100644 test/slim/dynamic/plug.js create mode 100644 test/slim/dynamic/stealconfig.js diff --git a/lib/graph/slim_graph.js b/lib/graph/slim_graph.js index b2e66c88..9d748f5c 100644 --- a/lib/graph/slim_graph.js +++ b/lib/graph/slim_graph.js @@ -57,7 +57,10 @@ module.exports = function(options) { target: options.target, slimConfig: options.slimConfig, plugins: !!nonJsBundles.length, - mainModuleId: getMainModuleId(options.graph, options.mains[0]) + mainModuleId: getMainModuleId( + options.graph, + options.mains[0] + ) }) ]) ); @@ -102,7 +105,9 @@ module.exports = function(options) { sharedBundles: sharedBundles, plugins: !!nonJsBundles.length, slimConfig: options.slimConfig, - progressive: !!secondaryBundles.length, + progressive: + needsDynamicLoader(options.slimConfig) || + !!secondaryBundles.length, mainModuleId: getMainModuleId(options.graph, mainName) }) ]; @@ -122,6 +127,15 @@ module.exports = function(options) { return slimmedBundles; }; +/** + * Whether the slim loader should support dynamic loading + * @param {Object} config + * @return {boolean} + */ +function needsDynamicLoader(config) { + return config.needsDynamicLoader === true; +} + /** * Whether there are multiple mains to be built * @param {Array} mains diff --git a/test/slim/dynamic/foo.plug b/test/slim/dynamic/foo.plug new file mode 100644 index 00000000..7e9668e7 --- /dev/null +++ b/test/slim/dynamic/foo.plug @@ -0,0 +1 @@ +"foo" \ No newline at end of file diff --git a/test/slim/dynamic/main.js b/test/slim/dynamic/main.js new file mode 100644 index 00000000..36aab8db --- /dev/null +++ b/test/slim/dynamic/main.js @@ -0,0 +1 @@ +require("foo.plug!plug"); \ No newline at end of file diff --git a/test/slim/dynamic/plug.js b/test/slim/dynamic/plug.js new file mode 100644 index 00000000..3034a5da --- /dev/null +++ b/test/slim/dynamic/plug.js @@ -0,0 +1,15 @@ +"format cjs"; +var loader = require("@loader"); +var local = loader.localLoader || loader; + +if (local.slimConfig) { + local.slimConfig.needsDynamicLoader = true; +} + +exports.translate = function plug(load) { + return ` + define(function() { + return ${load.source}; + }); + `; +}; diff --git a/test/slim/dynamic/stealconfig.js b/test/slim/dynamic/stealconfig.js new file mode 100644 index 00000000..5f703aa0 --- /dev/null +++ b/test/slim/dynamic/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 61be0ef3..150fc7fb 100644 --- a/test/slim_build_test.js +++ b/test/slim_build_test.js @@ -785,4 +785,23 @@ describe("slim builds", function() { close(); }); }); + + it("adds dynamic loading if config flag set", function() { + var base = path.join(__dirname, "slim", "dynamic"); + var config = { config: path.join(base, "stealconfig.js") }; + + return rmdir(path.join(base, "dist")) + .then(function() { + return optimize(config, { quiet: true, minify: false }); + }) + .then(function() { + return readFile(path.join(base, "dist", "bundles", "main.js")); + }) + .then(function(buffer) { + assert.ok( + /stealRequire\.dynamic = function/.test(buffer.toString()), + "slim loader should support dynamic imports" + ); + }); + }); });