Skip to content

Commit

Permalink
Merge pull request #932 from stealjs/autorender
Browse files Browse the repository at this point in the history
Allow plugins to force inclusion of the slim dynamic loader
  • Loading branch information
m-mujica authored Jan 23, 2018
2 parents faba2a4 + 3f7fc92 commit b88db3c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/graph/slim_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
})
])
);
Expand Down Expand Up @@ -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)
})
];
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/slim/dynamic/foo.plug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo"
1 change: 1 addition & 0 deletions test/slim/dynamic/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("foo.plug!plug");
15 changes: 15 additions & 0 deletions test/slim/dynamic/plug.js
Original file line number Diff line number Diff line change
@@ -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};
});
`;
};
3 changes: 3 additions & 0 deletions test/slim/dynamic/stealconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steal.config({
main: "main"
});
19 changes: 19 additions & 0 deletions test/slim_build_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});
});
});

0 comments on commit b88db3c

Please sign in to comment.