Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #850 from stealjs/add-at-steal
Support @steal in slim build
- Loading branch information
Showing
9 changed files
with
161 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
` | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var steal = require("@steal"); | ||
|
||
steal.done().then(function() { | ||
window.atSteal = steal; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
steal.config({ | ||
main: "main", | ||
envs: { | ||
"window-production": { | ||
serviceBaseURL: "/api/production" | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters