Skip to content

Commit

Permalink
Merge branch 'master' into serviveworker
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
  • Loading branch information
Julian Kern committed Sep 30, 2017
2 parents 0d933cb + 0abee93 commit 157f96d
Show file tree
Hide file tree
Showing 23 changed files with 310 additions and 126 deletions.
11 changes: 6 additions & 5 deletions lib/build/slim.js
Expand Up @@ -18,10 +18,9 @@ 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 @@ -61,12 +60,15 @@ module.exports = function(cfg, opts) {
graph(config, options),
buildType("optimize"),
filterGraph(),
checkSlimSupport(),
addAtStealShim(),
addAtLoaderShim(),
addModuleIds(),
convertSlimConfig(),
loadNodeBuilder(),
transpile({ outputFormat: "slim" }),
transpile({
outputFormat: "slim",
keepInGraph: ["@steal", "@loader"]
}),
bundle(),
addPluginNames(),
addBundleIds(),
Expand All @@ -80,7 +82,6 @@ module.exports = function(cfg, opts) {

var final = pump(
initialStream,
cloneBuildData(),
adjustBundlesPath({ target: target }), // the "" target is relevant for this transform
slimBundles({ target: target || "web" }), // set default target so there is no need to handle ""
concat(),
Expand Down
11 changes: 6 additions & 5 deletions lib/graph/pluck.js
@@ -1,19 +1,20 @@
var includes = require("lodash/includes");

// removes a module and all of its dependencies from the graph.
module.exports = function(graph, name) {
module.exports = function(graph, name, _except) {
var modules = [];
var visited = {};
var except = _except == null ? [] : _except;

function visit(name) {
if (!visited[name]) {
if (!visited[name] && !includes(except, name)) {
visited[name] = true;
var node = graph[name];

delete graph[name];

if (node) {
node.dependencies.forEach(function(moduleName) {
visit(moduleName);
});
node.dependencies.forEach(visit);
modules.push(node);
}
}
Expand Down
20 changes: 12 additions & 8 deletions lib/graph/slim_graph.js
@@ -1,9 +1,10 @@
var omit = require("lodash/omit");
var first = require("lodash/first");
var negate = require("lodash/negate");
var concat = require("lodash/concat");
var assign = require("lodash/assign");
var partial = require("lodash/partial");
var includes = require("lodash/includes");
var cloneDeep = require("lodash/cloneDeep");
var isJavaScriptBundle = require("../bundle/is_js_bundle");
var makeSlimShimNode = require("../node/make_slim_shim_node");
var makeSlimBundleNode = require("../node/make_slim_bundle_node");
Expand Down Expand Up @@ -82,7 +83,7 @@ module.exports = function(options) {
var secondaryBundles = jsBundles.filter(negate(isEntryPointBundle));

// each entry point bundle includes the loader code
entryPointBundles.forEach(function(bundle) {
var slimmedEntryBundles = entryPointBundles.map(function(bundle) {
var mainName = getMainName(bundle);

var sharedBundles = getSharedBundlesOf(
Expand All @@ -92,10 +93,10 @@ module.exports = function(options) {
mainName
);

bundle.nodes = [
var nodes = [
slimConfigNode,
makeSlimShimNode({
nodes: bundle.nodes,
nodes: bundle.nodes.slice(0),
target: options.target,
sharedBundles: sharedBundles,
plugins: !!nonJsBundles.length,
Expand All @@ -105,10 +106,13 @@ module.exports = function(options) {
})
];

return assign({}, omit(bundle, ["nodes"]), {
nodes: nodes
});
});

slimmedBundles = concat(
entryPointBundles,
slimmedEntryBundles,
secondaryBundles.map(partial(toSlimBundle, options.target)),
nonJsBundles
);
Expand Down Expand Up @@ -143,9 +147,9 @@ function getSharedBundlesOf(bundles, mainName) {
}

function toSlimBundle(target, bundle) {
var cloned = cloneDeep(bundle);
cloned.nodes = [makeSlimBundleNode(target, cloned)];
return cloned;
return assign({}, omit(bundle, ["nodes"]), {
nodes: [makeSlimBundleNode(target, bundle)]
});
}

function makeLoaderBundle(nodes) {
Expand Down
2 changes: 1 addition & 1 deletion lib/node/slim/make_shim_template.js
Expand Up @@ -132,7 +132,7 @@ module.exports = function(options) {
${renderProgressivePartial(options)}
${options.resolve ? resolveHook.baseResolve : ""};
${options.resolve ? resolveHook.baseResolve : ""}
${options.extensions ? importSlimExtensionsPartial : ""}
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;
});
`
}
};
}
41 changes: 27 additions & 14 deletions lib/stream/adjust_bundles_path.js
@@ -1,29 +1,42 @@
var through = require("through2");
var omit = require("lodash/omit");
var clone = require("lodash/clone");
var assign = require("lodash/assign");

module.exports = function(options) {
return through.obj(function(data, enc, next) {
try {
next(null, adjustBundlesPath(data, options));
next(null, options.target ? adjustBundlesPath(data, options) : data);
} catch (err) {
next(err);
}
});
};

/**
* Appends the target name to the bundles path
*
* Each target build should be written in its own subfolder, e.g:
*
* dist/bundles/node
* dist/bundles/web
* dist/bundles/worker
*
* This should only happen when target is explicitly set.
*/
function adjustBundlesPath(data, options) {
if (options.target) {
var path = require("path");
var path = require("path");
var configuration = clone(data.configuration);

// override the configuration getter for `bundlesPath` so it
// includes the target name
var bundlesPath = data.configuration.bundlesPath;
Object.defineProperty(data.configuration, "bundlesPath", {
configurable: true,
get: function() {
return path.join(bundlesPath, options.target);
}
});
}
var bundlesPath = configuration.bundlesPath;
Object.defineProperty(configuration, "bundlesPath", {
configurable: true,
get: function() {
return path.join(bundlesPath, options.target);
}
});

return data;
return assign({}, omit(data, ["configuration"]), {
configuration: configuration
});
}
12 changes: 0 additions & 12 deletions lib/stream/clone_build_data.js

This file was deleted.

3 changes: 1 addition & 2 deletions lib/stream/load_node_builder.js
@@ -1,7 +1,6 @@
var keys = require("lodash/keys");
var through = require("through2");
var assign = require("lodash/assign");
var cloneDeep = require("lodash/cloneDeep");

module.exports = function() {
return through.obj(function(data, enc, next) {
Expand All @@ -20,8 +19,8 @@ module.exports = function() {
*/
function loadNodeBuilder(data) {
return new Promise(function(resolve, reject) {
var graph = data.graph;
var loader = data.loader;
var graph = cloneDeep(data.graph);

var promises = keys(graph).map(function(nodeName) {
var node = graph[nodeName];
Expand Down
14 changes: 11 additions & 3 deletions lib/stream/slim.js
@@ -1,4 +1,6 @@
var omit = require("lodash/omit");
var through = require("through2");
var assign = require("lodash/assign");
var slimGraph = require("../graph/slim_graph");

/**
Expand All @@ -23,10 +25,12 @@ module.exports = function(options) {
* @return {Object} The mutated data
*/
function doSlimGrap(data, options) {
data.bundles = slimGraph({
var bundles = data.bundles.slice(0);

var slimmedBundles = slimGraph({
graph: data.graph,
mains: data.mains,
bundles: data.bundles,
bundles: bundles,
target: options.target,
baseUrl: data.loader.baseURL,
slimConfig: data.loader.slimConfig,
Expand All @@ -36,7 +40,11 @@ function doSlimGrap(data, options) {
progressiveBundles: getProgressiveBundles(data.loader, data.graph)
});

return data;
return assign(
{},
omit(data, ["bundles"]),
{ bundles: slimmedBundles }
);
}


Expand Down

0 comments on commit 157f96d

Please sign in to comment.