├── .gitignore ├── .travis.yml ├── lib ├── camelize.js └── insert-features.js ├── package.json ├── appveyor.yml ├── LICENSE.md ├── tests ├── insert-features-test.js └── camelize-test.js ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # misc 12 | /.sass-cache 13 | /connect.lock 14 | /coverage/* 15 | /libpeerconnection.log 16 | npm-debug.log 17 | testem.log 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | 4 | sudo: false 5 | 6 | cache: 7 | directories: 8 | - node_modules 9 | 10 | node_js: 11 | - "0.10" 12 | - "0.12" 13 | - "iojs" 14 | 15 | before_install: 16 | - "npm config set spin false" 17 | - "npm install -g npm@^2" 18 | 19 | install: 20 | - npm install 21 | 22 | script: 23 | - npm test 24 | -------------------------------------------------------------------------------- /lib/camelize.js: -------------------------------------------------------------------------------- 1 | var STRING_CAMELIZE_REGEXP = (/(\-|_|\.|\s)+(.)?/g); 2 | 3 | function camelize(key) { 4 | return key.replace(STRING_CAMELIZE_REGEXP, function(match, separator, chr) { 5 | return chr ? chr.toUpperCase() : ''; 6 | }).replace(/^([A-Z])/, function(match, separator, chr) { 7 | return match.toLowerCase(); 8 | }); 9 | } 10 | 11 | module.exports = camelize; 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-cli-defeatureify", 3 | "version": "1.1.2", 4 | "main": "index.js", 5 | "description": "Ember-CLI addon to defeatureify your apps", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/jkarsrud/ember-cli-defeatureify.git" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/jkarsrud/ember-cli-defeatureify/issues" 12 | }, 13 | "author": "Jesper Haug Karsrud", 14 | "license": "MIT", 15 | "scripts": { 16 | "test": "node_modules/.bin/nodeunit tests" 17 | }, 18 | "dependencies": { 19 | "broccoli-defeatureify": "1.0.0", 20 | "ember-cli-version-checker": "^1.0.1" 21 | }, 22 | "keywords": [ 23 | "ember-addon" 24 | ], 25 | "ember-addon": { 26 | "before": [ 27 | "ember-cli-uglify" 28 | ] 29 | }, 30 | "devDependencies": { 31 | "nodeunit": "0.9.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # scripts that are called at very beginning, before repo cloning 2 | init: 3 | - git config --global core.autocrlf true 4 | 5 | # Test against these versions of Node.js. 6 | environment: 7 | matrix: 8 | # node.js 9 | - nodejs_version: "0.10" 10 | #io.js 11 | - nodejs_version: "1.0" 12 | 13 | # Install scripts. (runs after repo cloning) 14 | install: 15 | # Get the latest stable version of Node 0.STABLE.latest 16 | - ps: Install-Product node $env:nodejs_version 17 | - "npm config set spin false" 18 | - "npm install -g npm@^2" 19 | - "npm install" 20 | 21 | # Post-install test scripts. 22 | test_script: 23 | # Output useful info for debugging. 24 | - node --version 25 | - npm --version 26 | # run tests 27 | - npm test 28 | 29 | # Don't actually build. 30 | build: off 31 | 32 | # Set build version format here instead of in the admin panel. 33 | version: "{build}" 34 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /tests/insert-features-test.js: -------------------------------------------------------------------------------- 1 | var insertFeatures = require('../lib/insert-features'); 2 | 3 | exports.testInsertFeaturesWithOneFeature = function(test) { 4 | var expected = '(function(w) {w["TEST"] = w["TEST"] || {};w["TEST"].FEATURES = {"test":true};w["TEST"].FEATURES.isEnabled = function(feature) {return this[feature];};})(window);'; 5 | var options = { 6 | namespace: 'TEST', 7 | features: { 8 | test: true 9 | } 10 | }; 11 | 12 | var actual = insertFeatures(options); 13 | 14 | test.equal(actual, expected); 15 | test.done(); 16 | }; 17 | 18 | exports.testInsertFeaturesWithMultipleFeatures = function(test) { 19 | var expected = '(function(w) {w["TEST"] = w["TEST"] || {};w["TEST"].FEATURES = {"test":true,"anotherFeature":false};w["TEST"].FEATURES.isEnabled = function(feature) {return this[feature];};})(window);'; 20 | var options = { 21 | namespace: 'TEST', 22 | features: { 23 | test: true, 24 | anotherFeature: false 25 | } 26 | }; 27 | 28 | var actual = insertFeatures(options); 29 | 30 | test.equal(actual, expected); 31 | test.done(); 32 | }; 33 | -------------------------------------------------------------------------------- /lib/insert-features.js: -------------------------------------------------------------------------------- 1 | function featuresPrefix(namespace) { 2 | var prefix = [ 3 | '(function(w) {', 4 | 'w["' + namespace + '"] = w["' + namespace + '"] || {};', 5 | 'w["' + namespace + '"].FEATURES = {' 6 | ]; 7 | 8 | return prefix.join(''); 9 | } 10 | 11 | function featuresSuffix(namespace) { 12 | var isEnabled = function() { 13 | return [ 14 | 'w["' + namespace + '"].FEATURES.isEnabled = function(feature) {', 15 | 'return this[feature];', 16 | '};', 17 | ].join(''); 18 | }; 19 | 20 | var suffix = [ 21 | '};', 22 | isEnabled(), 23 | '})(window);' 24 | ]; 25 | 26 | return suffix.join(''); 27 | } 28 | 29 | function featuresContent(features) { 30 | var _features = []; 31 | 32 | Object.keys(features).forEach(function(feature) { 33 | var value = features[feature]; 34 | _features.push('"' + feature + '"' + ':' + value); 35 | }); 36 | 37 | return _features.join(','); 38 | } 39 | 40 | function insertFeatures(options) { 41 | var features = [ 42 | featuresPrefix(options.namespace), 43 | featuresContent(options.features), 44 | featuresSuffix(options.namespace) 45 | ]; 46 | 47 | return features.join(''); 48 | } 49 | 50 | module.exports = insertFeatures; 51 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true, sub: true */ 2 | 'use strict'; 3 | 4 | var defeatureify = require('broccoli-defeatureify'); 5 | var insertContent = require('./lib/insert-features'); 6 | var checker = require('ember-cli-version-checker'); 7 | var camelize = require('./lib/camelize'); 8 | 9 | module.exports = { 10 | name: 'ember-cli-defeatureify', 11 | init: function() { 12 | this._super.init && this._super.init.apply(this, arguments); 13 | checker.assertAbove(this, '0.1.15'); 14 | }, 15 | included: function(app) { 16 | this._super.included.apply(this, arguments); 17 | this.app = app; 18 | this.options = getOptions(app, app.options['defeatureify']); 19 | }, 20 | contentFor: function(type, config) { 21 | if(this.app.env !== 'production' && type === 'app-prefix') { 22 | return insertContent(this.options); 23 | } 24 | return ''; 25 | }, 26 | postprocessTree: function(type, tree) { 27 | if(this.app.env === 'production' && type === 'all') { 28 | tree = defeatureify(tree, this.options); 29 | } 30 | return tree; 31 | } 32 | }; 33 | 34 | function getOptions(app, options) { 35 | options = options || {}; 36 | 37 | if(!options.namespace) { 38 | options.namespace = app.name; 39 | } 40 | 41 | if(options.namespace) { 42 | options.namespace = camelize(options.namespace); 43 | } 44 | 45 | if(!options.features) { 46 | throw new Error("Features are not specified"); 47 | } 48 | 49 | return options; 50 | } 51 | -------------------------------------------------------------------------------- /tests/camelize-test.js: -------------------------------------------------------------------------------- 1 | var camelize = require('../lib/camelize'); 2 | 3 | exports.shouldCamelizeNormal = function(test) { 4 | var expected = 'aNormalString'; 5 | var actual = camelize('a normal string'); 6 | 7 | test.equal(actual, expected); 8 | test.done(); 9 | }; 10 | 11 | exports.shouldCamelizeCapitalized = function(test) { 12 | var expected = 'aCapitalizedString'; 13 | var actual = camelize('A Capitalized String'); 14 | 15 | test.equal(actual, expected); 16 | test.done(); 17 | }; 18 | 19 | exports.shouldCamelizeDashes = function(test) { 20 | var expected = 'aDasherizedString'; 21 | var actual = camelize('a-dasherized-string'); 22 | 23 | test.equal(actual, expected); 24 | test.done(); 25 | }; 26 | 27 | exports.shouldCamelizeDotNotation = function(test) { 28 | var expected = 'dotNotation'; 29 | var actual = camelize('dot.notation'); 30 | 31 | test.equal(actual, expected); 32 | test.done(); 33 | }; 34 | 35 | exports.shouldCamelizeUnderscores = function(test) { 36 | var expected = 'iAmUnderscored'; 37 | var actual = camelize('i_am_underscored'); 38 | 39 | test.equal(actual, expected); 40 | test.done(); 41 | }; 42 | 43 | exports.shouldIgnoreAlreadyCamelizedStrings = function(test) { 44 | var expected = 'iAmCamelized'; 45 | var actual = camelize('iAmCamelized'); 46 | 47 | test.equal(actual, expected); 48 | test.done(); 49 | }; 50 | 51 | exports.shouldIgnoreOneWordString = function(test) { 52 | var expected = 'string'; 53 | var actual = camelize('string'); 54 | 55 | test.equal(actual, expected); 56 | test.done(); 57 | }; 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ember-cli-defeatureify 2 | 3 | [![Travis Build Status](https://travis-ci.org/jkarsrud/ember-cli-defeatureify.svg)](https://travis-ci.org/jkarsrud/ember-cli-defeatureify) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/jkarsrud/ember-cli-defeatureify?svg=true)](https://ci.appveyor.com/project/jkarsrud/ember-cli-defeatureify) 4 | 5 | Addon for Ember-CLI that allows you to [defeatureify](https://github.com/thomasboyt/defeatureify) your code when building for production. 6 | 7 | ## Installation 8 | 9 | ```bash 10 | ember install:addon ember-cli-defeatureify 11 | ``` 12 | 13 | ## Usage 14 | 15 | Specify features in your project's `Brocfile.js`: 16 | 17 | ```js 18 | var app = new EmberApp({ 19 | defeatureify: { 20 | namespace: 'myNamespace', 21 | features: { 22 | "propertyBraceExpansion": true, 23 | "ember-metal-run-bind": true, 24 | "with-controller": true, 25 | "query-params-new": false, 26 | "string-humanize": false 27 | } 28 | } 29 | }) 30 | ``` 31 | 32 | When building in `development`, these features will be inlined in `my-app.js`, while they're only used for defeatureifying your code when building in `production`. The features are available to you in your application code under `myNamespace.FEATURES`. 33 | 34 | To use the feature flags, you would wrap the code you want to enable like this: 35 | 36 | ```js 37 | if(myNamespace.FEATURES.isEnabled('propertyBraceExpansion')) { 38 | // Your code here 39 | } else { 40 | // What to do if feature is disabled 41 | } 42 | ``` 43 | 44 | **Note:** If you want to remove Ember debug statements, you will need to use `Ember.default` instead of just `Ember` in the `debugStatements` list. 45 | 46 | ## Options 47 | 48 | ### options.namespace 49 | Namespace defaults to your application name from `package.json`, but you can specify your own through the `namespace` option. 50 | 51 | The namespace is `camelized` if it contains dashes, underscores or spaces to make sure it's valid JavaScript and parseable by `defeatureify`. 52 | 53 | **Example**: 54 | ```js 55 | 'my-app-namespace' // myAppNamespace 56 | 'app_namespace' // appNamespace 57 | 'awesome namespace' // awesomeNamespace 58 | ``` 59 | 60 | See [grunt-ember-defeatureify](https://github.com/craigteegarden/grunt-ember-defeatureify#options) for more documentation of options. 61 | --------------------------------------------------------------------------------