Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
sfx optimization via rollup first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 18, 2015
1 parent 7a71fdb commit 625c182
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports.getCanonicalName = getCanonicalName;

var attachCompilers = require('./compile').attachCompilers;
var compileOutputs = require('./compile').compileOutputs;
var wrapSFXOutputs = require('./compile').wrapSFXOutputs;
var writeOutputs = require('./output').writeOutputs;

var traceExpression = require('./arithmetic').traceExpression;
Expand Down Expand Up @@ -280,15 +281,27 @@ Builder.prototype.buildSFX = function(expression, outFile, opts) {
opts.runtime = true;

opts = processOpts(opts, outFile);
var tree;

if (opts.config)
this.config(opts.config);

// trace tree and entry points
var tree, entryPoints;

return traceExpression(this, expression, true)
.then(function(trace) {
tree = trace.tree;
return compileOutputs(loader, tree, opts, trace.entryPoints);
entryPoints = trace.entryPoints;

// attempt optimization for all ES6
return require('./sfx-optimize').optimizeSFXTree(trace.tree, trace.entryPoints, opts);
})
.then(function(optimizedOutputs) {
// was all ES6, has already been compiled separately
if (optimizedOutputs)
return optimizedOutputs;

return compileOutputs(loader, tree, opts, entryPoints);
})
.then(function(outputs) {
return writeOutputs(opts, outputs, loader.baseURL);
Expand Down
55 changes: 55 additions & 0 deletions lib/sfx-optimize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var rollup = require('rollup');
var fromFileURL = require('./utils').fromFileURL;
var toFileURL = require('./utils').toFileURL;

function getLoad(tree, path) {
var address = toFileURL(path);
var load;
Object.keys(tree).some(function(name) {
if (tree[name].address == address || tree[name].name == path) {
load = tree[name];
return true;
}
});
return load;
}

exports.optimizeSFXTree = function(tree, entryPoints, opts) {
var allES = !Object.keys(tree).some(function(name) {
return tree[name].metadata.format !== 'esm';
});

if (!allES || entryPoints.length > 1)
return;

var entryName = entryPoints[0]

// whole tree is ES, we can use rollup to optimize for SFX!
return rollup.rollup({
entry: fromFileURL(tree[entryPoints[0]].address),
resolvePath: function(id, importer, options) {
return getLoad(tree, importer).depMap[id];
},
resolveExternal: function(id, importer, options) {
return getLoad(tree, importer).depMap[id];
},
load: function(path, options) {
return getLoad(tree, path).metadata.originalSource;
}
})
.then(function(bundle) {
var output = bundle.generate({
format: opts.sfxFormat === 'global' ? 'iife' : opts.sfxFormat,
exports: 'auto',

// we dont have this option in builder, as its assumed the user assigns to the global from within the ES6 module
// its probably worth introducing this option
moduleName: 'test'
});

return [{
source: output.code,
sourceMap: output.map
}];
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dependencies": {
"glob": "^5.0.10",
"mkdirp": "^0.5.1",
"rollup": "^0.8.1",
"rsvp": "^3.0.18",
"source-map": "^0.4.2",
"systemjs": "^0.18.0",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/es-tree/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {b} from './b.js';
export var a = 'a';
1 change: 1 addition & 0 deletions test/fixtures/es-tree/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var b = 'b';
24 changes: 24 additions & 0 deletions test/static-optimize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Builder = require('../index');
var builder = new Builder();

var minify = false;

builder.loadConfigSync('./test/fixtures/test-tree.config.js');

builder.config({
transpiler: 'babel',
paths: {
'*': './test/fixtures/es-tree/*'
}
});

suite('SFX Optimizations', function() {
test('All ES6 rollup optimization', function(done) {
builder.buildSFX('a.js', 'test/output/es-sfx.js', { runtime: false, minify: minify })
.then(function(output) {
// TODO actually test
console.log(output);
done();
}, done)
});
});
3 changes: 2 additions & 1 deletion test/test-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ suite('Bundle Format', function() {
})
.then(function() {
testPhantom('test/test-sfx-amd.html', done);
});
})
.catch(done);
});
});

Expand Down

0 comments on commit 625c182

Please sign in to comment.