Skip to content

Commit

Permalink
Merge pull request #850 from stealjs/add-at-steal
Browse files Browse the repository at this point in the history
Support @steal in slim build
  • Loading branch information
m-mujica committed Sep 26, 2017
2 parents e373f48 + b0cfdeb commit 072cbf2
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 67 deletions.
4 changes: 2 additions & 2 deletions lib/build/slim.js
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = function(cfg, opts) {
graph(config, options),
buildType("optimize"),
filterGraph(),
checkSlimSupport(),
addAtStealShim(),
addAtLoaderShim(),
addModuleIds(),
convertSlimConfig(),
Expand Down
36 changes: 0 additions & 36 deletions lib/slim/checks/steal.js

This file was deleted.

103 changes: 103 additions & 0 deletions 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;
});
`
}
};
}
10 changes: 10 additions & 0 deletions test/slim/at_steal/index.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="./dist/bundles/main.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions test/slim/at_steal/main.js
@@ -0,0 +1,5 @@
var steal = require("@steal");

steal.done().then(function() {
window.atSteal = steal;
});
8 changes: 8 additions & 0 deletions test/slim/at_steal/stealconfig.js
@@ -0,0 +1,8 @@
steal.config({
main: "main",
envs: {
"window-production": {
serviceBaseURL: "/api/production"
}
}
});
34 changes: 33 additions & 1 deletion test/slim_build_test.js
Expand Up @@ -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") };
Expand Down Expand Up @@ -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();
});
});
});
27 changes: 0 additions & 27 deletions test/slim_support_checks_test.js

This file was deleted.

1 change: 0 additions & 1 deletion test/test.js
Expand Up @@ -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
Expand Down

0 comments on commit 072cbf2

Please sign in to comment.