Skip to content

Commit

Permalink
Merge pull request #779 from stealjs/@loader
Browse files Browse the repository at this point in the history
Add partial support for @loader module
  • Loading branch information
Manuel Mujica committed Jul 10, 2017
2 parents 929a8f7 + dd89738 commit ce89504
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 193 deletions.
2 changes: 2 additions & 0 deletions lib/build/slim.js
Expand Up @@ -13,6 +13,7 @@ var streams = {
addBundleIds: require("../stream/add_bundle_ids"),
filterGraph: require("../stream/filter_slim_graph"),
addPluginNames: require("../stream/add_plugin_names"),
addAtLoaderShim: require("../stream/add_loader_shim"),
checkSlimSupport: require("../stream/check_slim_support"),
write: require("../bundle/write_bundles").createWriteStream,
writeBundlesManifest: require("../stream/write_bundle_manifest"),
Expand All @@ -39,6 +40,7 @@ module.exports = function(config, options) {
streams.graph(config, buildOptions),
streams.filterGraph(),
streams.checkSlimSupport(),
streams.addAtLoaderShim(),
streams.addModuleIds(),
streams.loadOptimizedPlugins(),
streams.transpile({ outputFormat: "slim" }),
Expand Down
16 changes: 9 additions & 7 deletions lib/graph/make_graph.js
@@ -1,10 +1,12 @@
var winston = require('winston');
var trace = require("../trace");
var steal = require("steal");
var _ = require("lodash");
var clone = _.cloneDeep;
var omit = require("lodash/omit");
var assign = require("lodash/assign");
var clone = require("lodash/cloneDeep");
var addParseAMD = require("steal-parse-amd");
var makeDeferred = require("../make-deferred");
var isUndefined = require("lodash/isUndefined");
var makePredefinedPluginDependencies = require('./make_predefined_plugin_dependencies');

var formatMap = {
Expand All @@ -27,7 +29,7 @@ module.exports = function(config, options){
addParseAMD( localSteal.System );

localSteal.System.config({ env: "build-development" });
localSteal.System.config(_.omit(config, ["meta"]));
localSteal.System.config(omit(config, ["meta"]));
if(options.localStealConfig) {
localSteal.System.config(options.localStealConfig);
}
Expand All @@ -42,7 +44,7 @@ module.exports = function(config, options){
// Special types don't work that write to AMD if we add parseAMD to this.
//addParseAMD( buildSteal.System );
// It should be confured exactly like `localSteal`
buildSteal.System.config(_.omit(config, ["meta"]));
buildSteal.System.config(omit(config, ["meta"]));
buildSteal.System.systemName = (buildSteal.System.systemName ||"")+ "-build";

if(options.system) {
Expand Down Expand Up @@ -110,16 +112,16 @@ module.exports = function(config, options){

// set config one more time on startup. This is to make sure the final values
// are these values.
var localConfig = _.omit(config, ["config","systemName"]);
_.assign(localConfig, ignoredMetas(config.meta, buildSteal.System.meta));
var localConfig = omit(config, ["config","systemName"]);
assign(localConfig, ignoredMetas(config.meta, buildSteal.System.meta));

var appPromise = localSteal.startup(localConfig);
startupCalledDfd.resolve();

return appPromise.then(function(){
var main = localSteal.System.main;

if (_.isUndefined(main)) {
if (isUndefined(main)) {
return Promise.reject(
new Error(
"Attribute 'main' is required\n" +
Expand Down
10 changes: 6 additions & 4 deletions lib/graph/make_graph_with_bundles.js
@@ -1,5 +1,7 @@
var clone = require("lodash/clone");
var assign = require("lodash/assign");
var includes = require("lodash/includes");
var dependencyGraph = require("./make_graph");
var _ = require("lodash");
var normalizeBundle = require("../loader/normalize_bundle");
var through = require("through2");
var fs = require("fs-extra");
Expand Down Expand Up @@ -52,7 +54,7 @@ function mergeIntoMasterAndAddBundle(opts) {

// do not track bundles of entry point modules (a.k.a mains) unless
// it is its own bundle.
if (name === bundleName || !_.includes(mains, name)) {
if (name === bundleName || !includes(mains, name)) {
addBundle(masterGraph[name], bundleName);
}
}
Expand Down Expand Up @@ -90,7 +92,7 @@ var makeBundleGraph = module.exports = function(config, options){
// the names of everything we are going to load
var bundleNames = [];

var cfg = _.clone(config, true);
var cfg = clone(config, true);
if( Array.isArray(cfg.main) ) {
bundleNames = slice.call(cfg.main);
cfg.main = bundleNames.shift();
Expand Down Expand Up @@ -144,7 +146,7 @@ var makeBundleGraph = module.exports = function(config, options){
}
else {
var nmains;
var copy = _.assign(_.clone(cfg, true), { main: nextBundle });
var copy = assign(clone(cfg), { main: nextBundle });

return mainsPromise
.then(function(normalised) {
Expand Down
10 changes: 10 additions & 0 deletions lib/node/is_plugin_excluded.js
@@ -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)
);
};
2 changes: 1 addition & 1 deletion lib/node/make_node.js
Expand Up @@ -3,7 +3,7 @@ var nodeSource = require("./source");
module.exports = function(name, source, type){
return {
load: {
metadata: {format: type || global},
metadata: {format: type || "global"},
source: source || "",
name: name
},
Expand Down
17 changes: 0 additions & 17 deletions lib/slim/checks/production_env_config.js

This file was deleted.

36 changes: 36 additions & 0 deletions lib/slim/checks/steal.js
@@ -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`
);
}
}
47 changes: 0 additions & 47 deletions lib/slim/checks/steal_and_loader.js

This file was deleted.

106 changes: 106 additions & 0 deletions lib/stream/add_loader_shim.js
@@ -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)};
});
`
}
};
}
6 changes: 2 additions & 4 deletions lib/stream/check_slim_support.js
@@ -1,9 +1,8 @@
var through = require("through2");

// individual checks/rules for slim builds
var checkStealAndLoader = require("../slim/checks/steal_and_loader");
var checkAtSteal = require("../slim/checks/steal");
var checkStealConditional = require("../slim/checks/steal_conditional");
var checkProductionEnvConfig = require("../slim/checks/production_env_config");

module.exports = function() {
return through.obj(function(data, enc, done) {
Expand All @@ -18,9 +17,8 @@ module.exports = function() {
function checkSupport(data) {
var configMain = data.loader.configMain || "package.json!npm";

checkStealAndLoader(configMain, data.graph);
checkAtSteal(configMain, data.graph);
checkStealConditional(data.graph[configMain]);
checkProductionEnvConfig(data.steal);

return data;
}
8 changes: 8 additions & 0 deletions lib/trace.js
Expand Up @@ -6,6 +6,14 @@ var trace = function(System, BuildSystem, startupCalledPromise, onFulfilled){
System.pluginLoader = BuildSystem;
BuildSystem.localLoader = System;

// override System.config to keep track of the configuration properties set
var systemConfig = System.config;
System.config = function(cfg) {
System.__loaderConfig = System.__loaderConfig || {};
assign(System.__loaderConfig, cfg);
systemConfig.apply(System, arguments);
};

// The BuildSystem loader will execute modules, but wait for the value to come through
var buildInstantiate = BuildSystem.instantiate;
BuildSystem.instantiate = function(load){
Expand Down
10 changes: 10 additions & 0 deletions test/slim/at_loader/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_loader/main.js
@@ -0,0 +1,5 @@
var loader = require("@loader");

window.serviceBaseUrl = loader.serviceBaseURL;
window.stealPath = loader.stealPath;
window.paths = loader.paths;

0 comments on commit ce89504

Please sign in to comment.