Skip to content

Commit

Permalink
Basics of the development bundle working
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Mujica committed Feb 15, 2017
1 parent ca6ad21 commit a225f52
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 4 deletions.
60 changes: 60 additions & 0 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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");
var createWriteStream = require("../bundle/write_bundles").createWriteStream;
var createBundleGraphStream = require("../graph/make_graph_with_bundles").createBundleGraphStream;

module.exports = function(config, options) {
// Use the build-development environment.
if (!options) options = {};

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

try {
options = assignDefaultOptions(config, options);
} catch(err) {
return Promise.reject(err);
}

options.dest = "";
options.defaultBundlesPathName = "";

var graphStream = createBundleGraphStream(config, options);
var filteredGraphStream = graphStream.pipe(filterDevBundleGraph());
var transpileStream = filteredGraphStream.pipe(transpile());
var minifyStream = transpileStream.pipe(minify());
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());

writeStream.on("data", function(data){
this.end();
resolve(data);
});

var streams = [
graphStream,
filteredGraphStream,
transpileStream,
minifyStream,
buildStream,
concatStream
];

streams.forEach(function(stream) {
stream.on("error", reject);
});
});
};
13 changes: 9 additions & 4 deletions lib/configuration/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ function Configuration(loader, buildLoader, options){
this.loader = loader;
this.buildLoader = buildLoader;
this.options = options;

this.defaultBundlesPathName = options.defaultBundlesPathName != null ?
options.defaultBundlesPathName : "bundles";
}

// full path
// relative to
// relative to

Configuration.prototype = {
join: function(src){
Expand All @@ -28,16 +31,18 @@ Configuration.prototype = {
}
},
get dest() {
var dest = this.options.dest || "dist";
var dest = this.options.dest != null ? this.options.dest : "dist";

if(typeof dest === "function") {
dest = "dist";
}
return this.join(dest);
},
get bundlesPath () {
var dest = this.dest;
var dir = path.join(dest, "bundles");
return dir;
var bundlesPathName = this.defaultBundlesPathName;

return path.join(dest, bundlesPathName);
},
get bundlesPathURL () {
return path.relative( this.loader.baseURL, this.bundlesPath ).replace(/\\/g, '/');
Expand Down
67 changes: 67 additions & 0 deletions lib/stream/dev_bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var _ = require("lodash");
var through = require("through2");
var hasES6 = require("../graph/has_es6");
var unbundle = require("../graph/unbundle");
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");

module.exports = function() {
return through.obj(function(data, enc, done) {
try {
var result = bundle(data);
done(null, result);
} catch(err) {
done(err);
}
});
};

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

unbundle(graph);

var mainBundle = makeBundle(graph);
var splitMainBundles = nameBundles(splitByBuildType(mainBundle));

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

unshift.apply(mainJSBundle.nodes, configGraph);

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

mainJSBundle.nodes.unshift(configNode);

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

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

function nameBundles(bundles) {
return bundles.map(function(bundle) {
return _.assign(
{},
bundle,
{ name: "dev-bundle" }
);
});
}
50 changes: 50 additions & 0 deletions lib/stream/filter_dev_bundle_graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var _ = require("lodash");
var winston = require("winston");
var through = require("through2");
var npmUtils = require("steal/ext/npm-utils");

var isNpm = npmUtils.moduleName.isNpm;

module.exports = function() {
return through.obj(function(data, enc, done) {
try {
done(null, filterGraph(data));
} catch(err) {
done(err);
}
});
};

function filterGraph(data) {
var visited = {};
var filtered = {};
var graph = data.graph;

function visit(name) {
if (!visited[name]) {

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

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

node.dependencies.forEach(visit);
filtered[name] = node;
}
}

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

return _.assign(
{},
_.omit(data, "graph"),
{ graph: filtered }
);
}
58 changes: 58 additions & 0 deletions test/dev_bundle_build_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var path = require("path");
var fs = require("fs-extra");
var assert = require("assert");
var denodeify = require("pdenodeify");
var testHelpers = require("./helpers");
var devBundleBuild = require("../lib/build/bundle");

var open = testHelpers.popen;
var rmdir = denodeify(require("rimraf"));

describe("dev bundle build", function() {

// what should I assert here?
it.skip("app without npm dependencies?", function() {
return rmdir(path.join(__dirname, "bundle", "dist"))
.then(function() {
return devBundleBuild({
config: path.join(__dirname, "bundle", "stealconfig.js"),
main: "bundle"
});
});
});

// what should I assert here?
it("should work", function() {
this.timeout(5000);

var config = {
config: path.join(__dirname, "npm", "package.json!npm"),
main: "src/main"
};

var options = {
minify: false
};

var devBundlePath = path.join(__dirname, "npm", "dev-bundle.js");

return devBundleBuild(config, options)
.then(function() {
var exists = fs.existsSync(devBundlePath);
assert(exists, "dev bundle should be created");
})
.then(function() {
return open(path.join(__dirname, "npm"), "dev-bundle-main.html");
})
.then(function({ browser, close }) {
var h1s = browser.window.document.getElementsByTagName('h1');
assert.equal(h1s.length, 1, "Wrote H!.");
close();
})
.then(function() {
return rmdir(devBundlePath);
});
});

});

22 changes: 22 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ exports.open = function(url, callback, done){
});
});
};

// Uses promises instead of callbacks, better for chaining
exports.popen = function(root, url) {
if (server && server.address()) {
return server.close(function() {
exports.popen(url);
});
}

server = connect()
.use(serveStatic(root))
.listen(8081);

var browser = new Browser();
return browser.visit("http://localhost:8081/" + url)
.then(function() {
return {
browser: browser,
close: server.close.bind(server)
};
});
};
21 changes: 21 additions & 0 deletions test/npm/dev-bundle-main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
window.QUnit = window.parent.QUnit;
window.removeMyself = window.parent.removeMyself;

window.steal = {
map: {
"@dev": "dev-bundle"
}
};
</script>

<script type="text/javascript" src="node_modules/steal/steal.js"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions test/npm/steal.production.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ require("./graph_stream_test");
require("./transpile_test");
require("./write_stream_test");
require("./build_conditionals_test");
require("./dev_bundle_build_test");

0 comments on commit a225f52

Please sign in to comment.