Skip to content

Commit

Permalink
resolve #5019
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired authored and stefanpenner committed Aug 23, 2017
1 parent d67e27f commit 70f5079
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
5 changes: 1 addition & 4 deletions lib/stripped-build-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ module.exports = function(environment) {

plugins.push(
[FilterImports, filteredImports],
[StripFilteredImports, filteredImports],
[TransformBlockScoping, { 'throwIfClosureRequired': true }]
);

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

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

function removeImports() {
var importDeclarationsToRemove;
var filteredImports;
var filteredImportNames;

return {
name: 'remove-filtered-imports',
visitor: {
Program: {
enter: function(_, state) {
filteredImports = state.opts instanceof Array ? state.opts : (state.opts ? [state.opts] : []);
filteredImports = state.opts || {};
filteredImportNames = Object.keys(filteredImports);
importDeclarationsToRemove = [];
},
exit: function() {
Expand All @@ -24,8 +24,29 @@ function removeImports() {
ImportDeclaration: function(path) {
var name = path.node.source.value;

if (filteredImports.indexOf(name) !== -1) {
importDeclarationsToRemove.push(path);
if (filteredImportNames.indexOf(name) !== -1) {
if (filteredImports[name] === true) {
importDeclarationsToRemove.push(path);
} else {
let removables = [];
let imports = path.node.specifiers;

for (let i = 0; i < imports.length; i++) {
let specifier = imports[i].imported;
if (filteredImports[name].indexOf(specifier.name) !== -1) {
removables.push(imports[i]);
}
}

if (removables.length === imports.length) {
importDeclarationsToRemove.push(path);
} else {
for (let i = 0; i < removables.length; i++) {
let index = imports.indexOf(removables[i]);
imports.splice(index, 1);
}
}
}
}
}

Expand Down

0 comments on commit 70f5079

Please sign in to comment.