Dean Sofer
Follow Along: slides.com/ProLoser/node-plus-ember/live
Redundant bundling breaks tree-shaking
App
- Dependency B
- Library(A+B)
App(B+Library(A+B))
Library
- Dependency A
- Dependency B
Library(A+B)
bundle
library
bundle
app
/*** core-library/package.json ***/
{
...
"main": "compiled/index.js",
"dependencies": {
"babel-preset-es2015": "^6.6.0",
"babel": "^6.5.2"
}
}
/*** core-library/.babelrc ***/
{
"presets": [
"es2015"
]
}
Save the trees!
?
DON'T double compile your code
es6
compile
compile
watchers
sourcemaps
es5
bundled
waiting
watchers
waiting
sourcemaps
build tool A
build tool B
/*** core-library/package.json ***/
{
...
"browser": "src/index.js",
"browserify": {
"transform": [
"babelify"
]
},
"dependencies": {
...
"babel-preset-es2015": "^6.6.0",
"babelify": "^7.2.0"
}
}
/*** core-library/.babelrc ***/
{
"presets": [
"es2015"
]
}
es6
es5 bundled
browserify
/*** core-library/Base.js ***/
class Base {
constructor(data) {
Object.assign(this, data);
}
}
module.exports = Base;
/*** core-library/Project.js ***/
const Base = require('./Base');
const Task = require('./Task');
class Project extends Base {
static get(id) {
return query(`/projects/${id}`).then( project =>
new Project( project );
);
}
constructor(data) {
super(data);
this.tasks = this.tasks.map( task =>
new Task( task )
);
}
isComplete() {
return this.tasks.every( task =>
task.completed
);
}
}
module.exports = Project;
/*** my-addon ***/
import CoreLibrary from 'npm:core-library';
import Ember from 'ember';
const Base = CoreLibrary.Base;
// Monkey-patch CoreLibrary with Ember utils
Base.prototype.set = function(property, value) {
return Ember.set(this, property, value);
};
Base.prototype.get = function(property) {
return Ember.get(this, property);
};
// Configure environment for Ember statically
CoreLibrary.setPromiseLibrary(Ember.RSVP.Promise);
CoreLibrary.configure({ ... });
// Or create Ember configured instance
export default new CoreLibrary({ ... });
/*** my-addon/package.json ***/
{
...
"peerDependencies": {
"ember-browserify": "^1.1.8",
"my-node-library": "^1.0.0"
}
}
/*** my-addon/blueprints/my-addon/index.js ***/
module.exports = {
...
afterInstall: function(options) {
return this.addPackagesToProject([
{ name: 'ember-browserify' },
{ name: 'my-node-library' }
]);
}
};
my-app/
node_modules/
ember-browserify/
my-addon/
addon/
index.js
app/
dependencies.js
blueprints/
my-addon/
index.js
core-library/
library.js
package.json
node_modules/
babelify/
babel-preset-es2015/
/*** my-addon/addon/index.js ***/
import CoreLibrary from 'npm:core-library';
...
/*** my-addon/app/dependencies.js ***/
import coreLibrary from 'npm:core-library';
// Informs ember-cli of npm:core-library
// This file never gets executed
/*** my-app ***/
import CoreLibrary from 'npm:core-library';
import coreLibrary from 'my-addon';
CoreLibrary.Project.get(123).then( project =>
project.set('name', 'Presentation');
);
coreLibrary.Project.get(123).then( ... )
ember install my-addon