This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sfx optimization via rollup first attempt
- Loading branch information
1 parent
7a71fdb
commit 625c182
Showing
7 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}]; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {b} from './b.js'; | ||
export var a = 'a'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export var b = 'b'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters