Skip to content

Commit

Permalink
Merge pull request #4048 from pangratz/fix-ember-addon-production
Browse files Browse the repository at this point in the history
Strip stuff from addon before it is added to app
  • Loading branch information
bmac committed Jan 9, 2016
2 parents 8b3b3a6 + a11303d commit 4db5365
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 82 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,25 @@ module.exports = {
return { inputTree: dir, rebuild: function() { return []; } };
}

var version = require('./lib/version');
var merge = require('broccoli-merge-trees');
var version = require('./lib/version');
var merge = require('broccoli-merge-trees');
var addonTree = merge([version(), dir]);

return this._super.treeForAddon.call(this, merge([version(), dir]));
if (process.env.EMBER_ENV === 'production') {
var strippedBuild = require('./lib/stripped-build');

// blacklist es6.modules so the modules are not compiled but simply the
// debug statements / features are stripped; this is taken from
// ember-cli-babel:
// https://github.com/babel/ember-cli-babel/blob/master/index.js#L71
var strippedAddon = strippedBuild('ember-data', addonTree, {
blacklist: ['es6.modules', 'useStrict']
});

return this._super.treeForAddon.call(this, strippedAddon);
}

return this._super.treeForAddon.call(this, addonTree);
},

included: function(app) {
Expand Down
48 changes: 48 additions & 0 deletions lib/babel-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var babel = require('broccoli-babel-transpiler');
var path = require('path');

function babelOptions(libraryName, _options) {
_options = _options || {};

var options = {
whitelist: [
'es6.templateLiterals',
'es6.parameters',
'es6.arrowFunctions',
'es6.destructuring',
'es6.spread',
'es6.properties.computed',
'es6.properties.shorthand',
'es6.blockScoping',
'es6.constants',
'es6.modules'
],
sourceMaps: false,
modules: 'amdStrict',
moduleRoot: libraryName,
moduleId: true,
// Transforms /index.js files to use their containing directory name
getModuleId: function (name) {
return name.replace(/\/index$/g, '');
},
resolveModuleSource: function(name, filename) {
if (name.indexOf('.') === 0) {
return libraryName + '/' + path.join(path.dirname(filename), name);
} else {
return name;
}
}
};

Object.keys(_options).forEach(function(opt) {
options[opt] = _options[opt];
});

return options;
}

module.exports = function(packageName, tree, _options) {
var options = babelOptions(packageName, _options);

return babel(tree, options);
}
93 changes: 17 additions & 76 deletions lib/javascripts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* jshint node:true */

var filterImports = require('babel-plugin-filter-imports');
var featureFlags = require('babel-plugin-feature-flags');
var babel = require('broccoli-babel-transpiler');
var merge = require('broccoli-merge-trees');
var concat = require('broccoli-concat');
var uglify = require('broccoli-uglify-sourcemap');
Expand All @@ -13,84 +10,23 @@ var path = require('path');
var Funnel = require('broccoli-funnel');
var versionReplace = require('./version-replace');
var fileCreator = require('broccoli-file-creator');

function babelOptions(libraryName, _options) {
_options = _options || {};

var options = {
whitelist: [
'es6.templateLiterals',
'es6.parameters',
'es6.arrowFunctions',
'es6.destructuring',
'es6.spread',
'es6.properties.computed',
'es6.properties.shorthand',
'es6.blockScoping',
'es6.constants',
'es6.modules'
],
sourceMaps: false,
modules: 'amdStrict',
moduleRoot: libraryName,
moduleId: true,
// Transforms /index.js files to use their containing directory name
getModuleId: function (name) {
return name.replace(/\/index$/g, '');
},
resolveModuleSource: function(name, filename) {
if (name.indexOf('.') === 0) {
return libraryName + '/' + path.join(path.dirname(filename), name);
} else {
return name;
}
}
};

Object.keys(_options).forEach(function(opt) {
options[opt] = _options[opt];
});

return options;
}
var babelBuild = require('./babel-build');
var strippedBuild = require('./stripped-build');

function debugBuild(packageName, tree) {
var compiled = babel(tree, babelOptions(packageName));
var compiled = babelBuild(packageName, tree);

return stew.mv(compiled, packageName);
}

function strippedBuild(packageName, tree) {
var featuresJson = fs.readFileSync('config/features.json', { encoding: 'utf8' });
var features = JSON.parse(featuresJson);

var plugins = [
featureFlags({
import: { module: 'ember-data/-private/features' },
features: features
}),

filterImports({
'ember-data/-private/debug': [
'assert',
'debug',
'deprecate',
'info',
'runInDebug',
'warn',
'debugSeal'
]
})
];

function makeStrippedBuild(packageName, tree) {
var withoutDebug = new Funnel(tree, {
exclude: ['ember-data/-private/debug.js']
});

var compiled = babel(withoutDebug , babelOptions(packageName, {
plugins: plugins
}));
var stripped = strippedBuild(packageName, withoutDebug);

return stew.mv(compiled, packageName);
return stew.mv(stripped, packageName);
}

function collapse(tree, outputFileName) {
Expand Down Expand Up @@ -130,21 +66,26 @@ function minify(tree) {
});
}


module.exports = function(tree) {
function buildEmberInflector() {
var emberInflector = new Funnel(path.dirname(require.resolve('ember-inflector/addon')), {
include: ['**/*.js']
});

return debugBuild('ember-inflector', emberInflector);
}

module.exports = function(tree) {
var emberInflector = buildEmberInflector();
var emberData = merge([tree, version()]);

var javascripts = merge([
debugBuild('ember-inflector', emberInflector),
emberInflector,
debugBuild('ember-data', emberData)
]);

var strippedJavascripts = merge([
strippedBuild('ember-inflector', emberInflector),
strippedBuild('ember-data', emberData)
emberInflector,
makeStrippedBuild('ember-data', emberData)
]);

var debug = collapse(javascripts, 'ember-data.js');
Expand Down
44 changes: 44 additions & 0 deletions lib/stripped-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var fs = require('fs');
var path = require('path');
var filterImports = require('babel-plugin-filter-imports');
var featureFlags = require('babel-plugin-feature-flags');
var babelBuild = require('./babel-build');

module.exports = function(packageName, tree, _options) {
var featuresJsonPath = path.join(__dirname, '../config/features.json');
var featuresJson = fs.readFileSync(featuresJsonPath, { encoding: 'utf8' });
var features = JSON.parse(featuresJson);

// TODO explicitly set all features which are not enabled to `false`, so
// they are stripped --> make this configurable or pass features
//
// for (var feature in features) {
// if (features[feature] !== true) {
// features[feature] = false;
// }
// }

var plugins = [
featureFlags({
import: { module: 'ember-data/-private/features' },
features: features
}),

filterImports({
'ember-data/-private/debug': [
'assert',
'debug',
'deprecate',
'info',
'runInDebug',
'warn',
'debugSeal'
]
})
];

var options = _options || {};
options.plugins = plugins;

return babelBuild(packageName, tree, options);
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"author": "",
"license": "MIT",
"dependencies": {
"babel-plugin-feature-flags": "^0.2.0",
"babel-plugin-filter-imports": "^0.2.0",
"broccoli-babel-transpiler": "^5.5.0",
"broccoli-file-creator": "^1.0.0",
"broccoli-merge-trees": "^1.0.0",
"chalk": "^1.1.1",
Expand All @@ -35,11 +38,8 @@
"silent-error": "^1.0.0"
},
"devDependencies": {
"babel-plugin-feature-flags": "^0.2.0",
"babel-plugin-filter-imports": "^0.2.0",
"bower": "^1.6.5",
"broccoli-asset-rev": "^2.1.2",
"broccoli-babel-transpiler": "^5.5.0",
"broccoli-concat": "0.0.13",
"broccoli-funnel": "^1.0.0",
"broccoli-jscs": "^1.1.0",
Expand Down

0 comments on commit 4db5365

Please sign in to comment.