Skip to content
This repository has been archived by the owner on Jan 20, 2019. It is now read-only.

Ember CLI: Production code stripping #50

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions active/0000-production-code-stripping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
- Start Date: 2016-04-06
- RFC PR: (leave this empty)
- ember-cli Issue: (leave this empty)

# Summary

A number of Ember framework function calls are no-ops in production. Ember CLI should strip these no-op function invocations from production builds by default.

# Motivation

Removing code that isn't required in production results in smaller and faster applications.

# Detailed design

The following framework function calls will be removed from ember-cli production builds by default:

* [`Ember.assert`](http://emberjs.com/api/#method_assert)
* [`Ember.debug`](http://emberjs.com/api/#method_debug)
* [`Ember.deprecate`](http://emberjs.com/api/#method_deprecate)
* [`Ember.info`](http://emberjs.com/api/#method_info)
* [`Ember.runInDebug`](http://emberjs.com/api/#method_runInDebug)
* [`Ember.warn`](http://emberjs.com/api/#method_warn)
Copy link

@les2 les2 Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it wise to strip warn? if i were new to ember i would assume that info and "higher" were not stripped.

edit: the wip implementation is configurable so this concern is no longer relevant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@les2 Also both warn and info are already no-op in production builds. Stripping them don't change any behavior.

The idea of log level here is mostly related to how these things appear in the JS console in browsers, not to what build of ember that function emits in.


The API documentation will be updated where necessary to indicate that these function calls will be stripped from production builds.

A babel plugin will execute the removal of these function calls based on provided configuration. The plugin will affect the code of the current app or addon only and won't affect code in child or grandchild addons. As this change becomes part of the default ember-cli configuration, addons will adopt the code stripping as they upgrade to newer ember-cli versions.

The plugin configuration will define an array of modules or global functions to remove. Here's an example of what this configuration might look like:

```js
{
removals: [
{
module: 'ember', //eg. import Em from 'ember';
paths: [
'assert', //Em.assert will be removed
'debug', //Em.debug will be removed
'a.b.c' //Em.a.b.c will be removed
]
}, {
global: 'Ember',
paths: [
'deprecate' //Ember.deprecate will be removed
]
}, {
paths: [
'console.log' //console.log will be removed
]
}
]
}
```

The plugin will support removal of destructured and reassigned invocations of these functions and will support both Babel 5 and 6.

An app or addon can disable the code removal by removing the babel plugin.

# How We Teach This

This change doesn't bring any new functionality. Other than updating the Ember API docs, we don't need to make guide or other documentation changes. At the time of releasing, we may want to point out the possible side effects in a release blog post (see the _Drawbacks_ section below).

If we want to expose the configuration options so that application authors can customize the settings, we can include a new section in the Ember CLI docs.

# Drawbacks

This may introduce an unexpected change in production builds as arguments that have side effects will no longer be executed. For example:

```js
Ember.assert('Some assertion', someSideEffect());
```

Currently, the `someSideEffect` function will be executed in production. When this RFC lands, it won't.

# Alternatives

An Ember addon could provide opt-in function stripping for applications that want it. If this RFC isn't deemed a good default for Ember CLI, that option should be explored.