Skip to content

Commit

Permalink
Strip unused imports, dead modules, and debug modules from prod builds
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Apr 13, 2017
1 parent c99af93 commit a2785b2
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 75 deletions.
11 changes: 0 additions & 11 deletions addon/-private/adapters.js

This file was deleted.

7 changes: 5 additions & 2 deletions addon/-private/initializers/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Store from "../system/store";
import { JSONAPISerializer, JSONSerializer, RESTSerializer } from "../serializers";
import { JSONAPIAdapter, RESTAdapter } from "../adapters";
import JSONAPISerializer from 'ember-data/serializers/json-api';
import JSONSerializer from 'ember-data/serializers/json';
import RESTSerializer from 'ember-data/serializers/rest';
import JSONAPIAdapter from 'ember-data/adapters/json-api';
import RESTAdapter from 'ember-data/adapters/rest';

function has(applicationOrRegistry, fullName) {
if (applicationOrRegistry.has) {
Expand Down
13 changes: 0 additions & 13 deletions addon/-private/serializers.js

This file was deleted.

7 changes: 0 additions & 7 deletions addon/-private/system/debug.js

This file was deleted.

17 changes: 0 additions & 17 deletions addon/-private/system/empty-object.js

This file was deleted.

2 changes: 1 addition & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import ContainerInstanceCache from './store/container-instance-cache';
import InternalModel from "./model/internal-model";
import isEnabled from '../features';

export let badIdFormatAssertion = '`id` passed to `findRecord()` has to be non-empty string or number';
const badIdFormatAssertion = '`id` passed to `findRecord()` has to be non-empty string or number';

const {
A,
Expand Down
16 changes: 6 additions & 10 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Model from "./model";
import Snapshot from "./-private/system/snapshot";
import Adapter from "./adapter";
import Serializer from "./serializer";
import DebugAdapter from "ember-data/-private/debug";
import DebugAdapter from './-private/system/debug/debug-adapter';

import {
AdapterError,
Expand All @@ -61,16 +61,12 @@ import {
} from "./-private/system/record-arrays";
import ManyArray from "./-private/system/many-array";
import RecordArrayManager from "./-private/system/record-array-manager";
import {
JSONAPIAdapter,
RESTAdapter
} from "./-private/adapters";
import JSONAPIAdapter from './adapters/json-api';
import RESTAdapter from './adapters/rest';
import BuildURLMixin from "./-private/adapters/build-url-mixin";
import {
JSONAPISerializer,
JSONSerializer,
RESTSerializer
} from "./-private/serializers";
import JSONAPISerializer from './serializers/json-api';
import JSONSerializer from './serializers/json';
import RESTSerializer from './serializers/rest';
import "ember-inflector";
import EmbeddedRecordsMixin from "./serializers/embedded-records-mixin";

Expand Down
2 changes: 0 additions & 2 deletions app/initializers/data-adapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Ember from 'ember';

/*
This initializer is here to keep backwards compatibility with code depending
on the `data-adapter` initializer (before Ember Data was an addon).
Expand Down
2 changes: 0 additions & 2 deletions app/initializers/injectStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Ember from 'ember';

/*
This initializer is here to keep backwards compatibility with code depending
on the `injectStore` initializer (before Ember Data was an addon).
Expand Down
2 changes: 0 additions & 2 deletions app/initializers/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Ember from 'ember';

/*
This initializer is here to keep backwards compatibility with code depending
on the `store` initializer (before Ember Data was an addon).
Expand Down
2 changes: 0 additions & 2 deletions app/initializers/transforms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Ember from 'ember';

/*
This initializer is here to keep backwards compatibility with code depending
on the `transforms` initializer (before Ember Data was an addon).
Expand Down
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var path = require('path');
var SilentError = require('silent-error');
var Funnel = require('broccoli-funnel');

// allow toggling of heimdall instrumentation
var INSTRUMENT_HEIMDALL = false;
Expand All @@ -14,11 +15,14 @@ for (var i = 0; i < args.length; i++) {
break;
}
}

process.env.INSTRUMENT_HEIMDALL = INSTRUMENT_HEIMDALL;

function add(options, name, array) {
var option = options[name] = options[name] || [];
option.push.apply(option, array);
function isProductionEnv() {
var isProd = /production/.test(process.env.EMBER_ENV);
var isTest = process.env.EMBER_CLI_TEST_COMMAND;

return isProd && !isTest;
}

module.exports = {
Expand Down Expand Up @@ -111,10 +115,21 @@ module.exports = {
var version = require('./lib/version');
var merge = require('broccoli-merge-trees');

return this._super.treeForAddon.call(this, merge([
var tree = this._super.treeForAddon.call(this, merge([
version(),
dir
]));

if (isProductionEnv()) {
console.log('is prod');
tree = new Funnel(tree, {
exclude: [
/-private\/debug\.js/
]
});
}

return tree;
},

_setupBabelOptions: function() {
Expand Down
5 changes: 5 additions & 0 deletions lib/stripped-build-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var FilterImports = require('babel-plugin-filter-imports');
var FeatureFlags = require('babel-plugin-feature-flags');
var StripHeimdall = require('babel6-plugin-strip-heimdall');
var StripClassCallCheck = require('babel6-plugin-strip-class-callcheck');
var StripFilteredImports = require('./transforms/babel-plugin-remove-imports');

function uniqueAdd(obj, key, values) {
var a = obj[key] = obj[key] || [];
Expand Down Expand Up @@ -59,5 +60,9 @@ module.exports = function(environment) {
}
plugins.push([FilterImports, filteredImports]);

if (environment === 'production') {
plugins.push([StripFilteredImports, 'ember-data/-private/debug']);
}

return { plugins, postTransformPlugins };
};
40 changes: 40 additions & 0 deletions lib/transforms/babel-plugin-remove-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var path = require('path');

function removeImports() {
let importDeclarationsToRemove;
let filteredImports;

return {
name: 'remove-filtered-imports',
visitor: {
Program: {
enter: function(_, state) {
filteredImports = state.opts instanceof Array ? state.opts : (state.opts ? [state.opts] : []);
importDeclarationsToRemove = [];
},
exit: function() {
importDeclarationsToRemove.forEach(function(declaration) {
declaration.remove();
});

importDeclarationsToRemove = undefined;
}
},

ImportDeclaration: function(path) {
const name = path.node.source.value;

if (filteredImports.indexOf(name) !== -1) {
importDeclarationsToRemove.push(path);
}
}

}
};
}

removeImports.baseDir = function() {
return path.join(__dirname, '../../');

This comment has been minimized.

Copy link
@stefanpenner

stefanpenner Apr 25, 2017

Member

this causes all of ember-data to be considered as a cache key, but really this is more of an in-repo node_modules. I know that this is all kinda strange, we should likely explore how to improve the ergonomics...

fixed -> #4943

};

module.exports = removeImports;
4 changes: 2 additions & 2 deletions tests/unit/utils/parse-response-headers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const CRLF = '\u000d\u000a';

module('unit/adapters/parse-response-headers');

test('returns an EmptyObject when headersString is undefined', function(assert) {
test('returns an NULL Object when headersString is undefined', function(assert) {
let headers = parseResponseHeaders(undefined);

assert.deepEqual(headers, Object.create(null), 'EmptyObject is returned');
assert.deepEqual(headers, Object.create(null), 'NULL Object is returned');
});

test('header parsing', function(assert) {
Expand Down

0 comments on commit a2785b2

Please sign in to comment.