Skip to content

Commit

Permalink
Multiple fixes to the steal-tool's bundle
Browse files Browse the repository at this point in the history
- Provide graph filter options through glob patterns
- Allow user to set bundle destination folder
- Prevent bundle to create a steal.production.js file
- Fix CSS development bundle generation
  • Loading branch information
Manuel Mujica committed Feb 17, 2017
1 parent a225f52 commit 0b98e16
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 97 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var multiBuild = require("./lib/build/multi");
var transform = require("./lib/build/transform");
var exporter = require("./lib/build/export");
var bundle = require("./lib/build/bundle");

module.exports = {
bundle: bundle,
build: multiBuild,
transform: transform,
graph: {
Expand Down
21 changes: 12 additions & 9 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var assign = require("lodash").assign;
var minify = require("../stream/minify");
var devBundle = require("../stream/dev_bundle");
var transpile = require("../stream/transpile");
var concat = require("../bundle/concat_stream");
var stealWriteStream = require("../stream/steal");
var filterDevBundleGraph = require("../stream/filter_dev_bundle_graph");

var assignDefaultOptions = require("../assign_default_options");
Expand All @@ -13,7 +13,9 @@ module.exports = function(config, options) {
// Use the build-development environment.
if (!options) options = {};

// Minification is on by default
var isDestProvided = !!options.dest;

// minification is on by default
options.minify = options.minify == null ? true : options.minify;

try {
Expand All @@ -22,8 +24,12 @@ module.exports = function(config, options) {
return Promise.reject(err);
}

options.dest = "";
options.defaultBundlesPathName = "";
// if `dest` was not provided in the options object, override the default
// value so the development bundle is written out in the root folder
assign(options, {
defaultBundlesPathName: "",
dest: isDestProvided ? options.dest : ""
});

var graphStream = createBundleGraphStream(config, options);
var filteredGraphStream = graphStream.pipe(filterDevBundleGraph());
Expand All @@ -32,12 +38,8 @@ module.exports = function(config, options) {
var buildStream = minifyStream.pipe(devBundle());
var concatStream = buildStream.pipe(concat());

// Return a Promise that will resolve after bundles have been written
return new Promise(function(resolve, reject) {
// Pipe the build result into a write stream.
var writeStream = buildStream
.pipe(createWriteStream())
.pipe(stealWriteStream());
var writeStream = buildStream.pipe(createWriteStream());

writeStream.on("data", function(data){
this.end();
Expand All @@ -58,3 +60,4 @@ module.exports = function(config, options) {
});
});
};

51 changes: 32 additions & 19 deletions lib/bundle/concat_stream.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
var _ = require("lodash");
var through = require("through2");
var bundleFilename = require("./filename");
var concatSource = require("../bundle/concat_source");
var normalizeSource = require("./normalize_source");
var through = require("through2");

module.exports = function(){
return through.obj(function(data, enc, next){
var bundles = data.bundles;
var configuration = data.configuration;
var bundlesDir = configuration.bundlesPath + "/";
return through.obj(function(data, enc, next) {
try {
var result = concat(data);
next(null, result);
} catch(err) {
next(err);
}
});
};

bundles.forEach(function(bundle){
var bundlePath = bundle.bundlePath =
bundlesDir + "" + bundleFilename(bundle);
function concat(data) {
var bundles = data.bundles;
var configuration = data.configuration;
var bundlesPath = configuration.bundlesPath;

// If the bundle is explicity marked as clean, just resolve.
if(bundle.isDirty === false) {
return;
}
var bundlesDir = _.endsWith(bundlesPath, "/") ?
bundlesPath : bundlesPath + "/";

// Adjusts URLs
normalizeSource(bundle, bundlePath);
bundles.forEach(function(bundle){
var bundlePath = bundle.bundlePath =
bundlesDir + "" + bundleFilename(bundle);

// Combines the source
concatSource(bundle);
});
// If the bundle is explicity marked as clean, just resolve.
if(bundle.isDirty === false) {
return;
}

next(null, data);
// Adjusts URLs
normalizeSource(bundle, bundlePath);

// Combines the source
concatSource(bundle);
});
};

return data;
}
64 changes: 46 additions & 18 deletions lib/stream/dev_bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ var _ = require("lodash");
var through = require("through2");
var hasES6 = require("../graph/has_es6");
var unbundle = require("../graph/unbundle");
var flattenBundle = require("../bundle/flatten");
var makeBundle = require("../bundle/make_bundle");
var splitByBuildType = require("../bundle/split_by_build_type");
var addTraceurRuntime = require("../bundle/add_traceur_runtime");
var makeBundlesConfig = require("../bundle/make_bundles_config");

var BUNDLE_DEPTH = 1;

module.exports = function() {
return through.obj(function(data, enc, done) {
try {
Expand All @@ -19,49 +22,74 @@ module.exports = function() {
};

function bundle(data) {
var graph = data.graph;
var configGraph = data.configGraph;
var configuration = data.configuration;
var graph = includePluginsInBuild(data.graph);

unbundle(graph);

var mainBundle = makeBundle(graph);
flattenBundle(mainBundle, BUNDLE_DEPTH);
var splitMainBundles = nameBundles(splitByBuildType(mainBundle));

splitMainBundles.forEach(function(mainJSBundle) {
splitMainBundles.filter(isJSBundle).forEach(function(bundle) {
var unshift = [].unshift;

unshift.apply(mainJSBundle.nodes, configGraph);
unshift.apply(bundle.nodes, configGraph);

// Make config JS code so System knows where to look for bundles.
var configNode = makeBundlesConfig(
splitMainBundles,
configuration,
mainJSBundle,
{ excludedBundles: [] }
bundle,
{ excludedBundles: {} }
);

mainJSBundle.nodes.unshift(configNode);
bundle.nodes.unshift(configNode);

// Traceur code requires a runtime.
if (hasES6(graph)) {
addTraceurRuntime(mainJSBundle);
addTraceurRuntime(bundle);
}
});

return _.assign(
{},
data,
{ bundles: splitMainBundles }
);
return _.assign({}, data, {
bundles: splitMainBundles
});
}

function isJSBundle(bundle) {
return bundle.buildType !== "css";
}

function nameBundles(bundles) {
return bundles.map(function(bundle) {
return _.assign(
{},
bundle,
{ name: "dev-bundle" }
);
var type = bundle.buildType;

// this is needed so css bundles are written out with the
// right file extension
var suffix = type === "js" ? "" : "." + type + "!";

return _.assign({}, bundle, {
name: "dev-bundle" + suffix
});
});
}

function includePluginsInBuild(graph) {
var cloned = _.assign({}, graph);

function isPlugin(name) {
return cloned[name].isPlugin;
}

function includeInBuild(name) {
var node = cloned[name];
var metadata = node.load.metadata;

if (metadata) metadata.includeInBuild = true;
}

_.keys(cloned).filter(isPlugin).forEach(includeInBuild);

return cloned;
}
40 changes: 24 additions & 16 deletions lib/stream/filter_dev_bundle_graph.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var _ = require("lodash");
var path = require("path");
var winston = require("winston");
var through = require("through2");
var npmUtils = require("steal/ext/npm-utils");

var isNpm = npmUtils.moduleName.isNpm;
var minimatch = require("minimatch");

module.exports = function() {
return through.obj(function(data, enc, done) {
Expand All @@ -20,26 +19,27 @@ function filterGraph(data) {
var filtered = {};
var graph = data.graph;

function visit(name) {
if (!visited[name]) {
_.keys(graph).forEach(function visit(name) {
// don't visit a node twice
if (visited[name]) return;

visited[name] = true;
var node = graph[name];
visited[name] = true;
var node = graph[name];

if (!node) {
if (name && name[0] !== "@") {
winston.warn("Can't find dependency", name, "in graph.");
}
return;
if (!node) {
if (name && name[0] !== "@") {
winston.warn("Can't find dependency", name, "in graph.");
}
return;
}

var address = node.load.address;
var pattern = getGlobPattern(data);

if (minimatch(address, pattern)) {
node.dependencies.forEach(visit);
filtered[name] = node;
}
}

_.keys(graph).forEach(function(name) {
if (isNpm(name)) visit(name);
});

return _.assign(
Expand All @@ -48,3 +48,11 @@ function filterGraph(data) {
{ graph: filtered }
);
}

function getGlobPattern(data) {
var options = data.options;
var baseUrl = data.loader.baseURL;
var filter = options.filter || "node_modules/**/*";

return path.join(baseUrl, filter);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"web": "http://bitovi.com/"
},
"dependencies": {
"bitovi-source-map": "0.4.2-bitovi.2",
"chokidar": "^1.0.1",
"clean-css": "^4.0.5",
"colors": "^1.1.2",
Expand All @@ -17,9 +18,9 @@
"glob": "^7.1.1",
"is-appveyor": "^1.0.0",
"lodash": "^4.3.0",
"minimatch": "^3.0.3",
"moment": "^2.10.2",
"pdenodeify": "^0.1.0",
"bitovi-source-map": "0.4.2-bitovi.2",
"steal": "^1.2.4",
"steal-bundler": "^0.3.0",
"steal-parse-amd": "^1.0.0",
Expand Down
Loading

0 comments on commit 0b98e16

Please sign in to comment.