-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add partial support for @loader module
Closes #721
- Loading branch information
Manuel Mujica
committed
Jul 10, 2017
1 parent
70494c5
commit dd89738
Showing
20 changed files
with
241 additions
and
193 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 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 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 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 @@ | ||
/** | ||
* Whether the node is a plugin that won't be included in the build | ||
* @param {Object} node - A node from the dependency graph | ||
* @return {boolean} | ||
*/ | ||
module.exports = function isPluginExcludedFromBuild(node) { | ||
return Boolean( | ||
node.isPlugin && (!node.value.includeInBuild || node.value.pluginBuilder) | ||
); | ||
}; |
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,36 @@ | ||
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` | ||
); | ||
} | ||
} |
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,106 @@ | ||
var colors = require("colors"); | ||
var through = require("through2"); | ||
var keys = require("lodash/keys"); | ||
var omit = require("lodash/omit"); | ||
var assign = require("lodash/assign"); | ||
var clone = require("lodash/cloneDeep"); | ||
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 "@loader" shim | ||
* The "@loader" shim contains the loader configuration properties | ||
* @param {Object} data - The slim stream data object | ||
* @return {Object} The mutated stream object | ||
*/ | ||
function addAtLoaderShim(data) { | ||
if (includesAtLoader(omit(data.graph, data.loader.configMain))) { | ||
console.log( | ||
colors.yellow( | ||
`Warning: the @loader module is not fully supported in optimized builds` | ||
) | ||
); | ||
|
||
var config = clone( | ||
omit(data.loader.__loaderConfig, ["paths", "stealPath"]) | ||
); | ||
|
||
// make sure the "window-production" config overrides previous values | ||
assign(config, defaultTo(config.envs, {})["window-production"]); | ||
|
||
data.graph["@loader"] = makeShimNode(data.loader.main, config); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/** | ||
* Looks for "@loader" in the dependency graph | ||
* @param {Object} graph - The dependency graph | ||
* @return {boolean} true if found, false otherwise | ||
*/ | ||
function includesAtLoader(graph) { | ||
var found = false; | ||
|
||
var isAtLoader = function(name) { | ||
return name === "@loader"; | ||
}; | ||
|
||
keys(graph).forEach(function(name) { | ||
var node = graph[name]; | ||
|
||
if (isPluginExcludedFromBuild(node)) { | ||
return; | ||
} | ||
|
||
if (isAtLoader(name)) { | ||
return (found = true); | ||
} | ||
|
||
defaultTo(node.dependencies, []).forEach(function(depName) { | ||
if (isAtLoader(depName)) { | ||
return (found = true); | ||
} | ||
}); | ||
}); | ||
|
||
return found; | ||
} | ||
|
||
/** | ||
* Returns an @loader node with the source code returning the config object | ||
* @param {string} main - The main module name | ||
* @param {Object} config - The config object exposed by "@loader" | ||
* @return {Object} The faux "@loader" graph node | ||
*/ | ||
function makeShimNode(main, config) { | ||
return { | ||
bundles: [main], | ||
dependencies: [], | ||
deps: [], | ||
load: { | ||
address: "", | ||
metadata: { | ||
deps: [], | ||
format: "amd", | ||
dependencies: [] | ||
}, | ||
name: "@loader", | ||
source: ` | ||
define("@loader", function() { | ||
return ${JSON.stringify(config)}; | ||
}); | ||
` | ||
} | ||
}; | ||
} |
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 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 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 loader = require("@loader"); | ||
|
||
window.serviceBaseUrl = loader.serviceBaseURL; | ||
window.stealPath = loader.stealPath; | ||
window.paths = loader.paths; |
Oops, something went wrong.