Skip to content

Commit

Permalink
resolve #5019
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Sep 14, 2017
1 parent cb998fc commit f3bfad8
Show file tree
Hide file tree
Showing 5 changed files with 1,016 additions and 548 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 };
};
43 changes: 35 additions & 8 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() {
function PluginRemoveFilteredImports() {
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,17 +24,44 @@ 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 || filteredImports[name] === '*') {
importDeclarationsToRemove.push(path);
} else {
let removables = [];
let imports = path.node.specifiers;
const hasSpecifiers = imports.length > 0;

for (let i = 0; i < imports.length; i++) {
if (imports[i].type === 'ImportNamespaceSpecifier') {
continue;
}

let specifier = imports[i].imported;

if (filteredImports[name].indexOf(specifier.name) !== -1) {
removables.push(imports[i]);
}
}

if (hasSpecifiers && 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);
}
}
}
}
}

}
};
}

removeImports.baseDir = function() {
PluginRemoveFilteredImports.baseDir = function() {
return __dirname;
};

module.exports = removeImports;
module.exports = PluginRemoveFilteredImports;
108 changes: 108 additions & 0 deletions node-tests/unit/babel-plugin-remove-imports-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const BroccoliTestHelper = require('broccoli-test-helper');
const co = require('co');
const Babel = require('broccoli-babel-transpiler');
const chai = require('ember-cli-blueprint-test-helpers/chai');
const stripIndent = require('common-tags').stripIndent;
const StripFilteredImports = require('../../lib/transforms/babel-plugin-remove-imports');

const { expect } = chai;
const createTempDir = BroccoliTestHelper.createTempDir;
const createBuilder = BroccoliTestHelper.createBuilder;

function stripNewlines(str) {
return str.replace(/[\r\n]/g, '');
}

describe('Unit: babel-plugin-remove-filtered-imports', function() {
let plugins, pluginOptions;
let input, output;

function transform(code) {
return co.wrap(function* () {
input.write({ 'test.js': code });
let babel = new Babel(input.path(), {
plugins
});

output = createBuilder(babel);

yield output.build();

const transpiled = output.read();

return stripNewlines(transpiled['test.js']);
})();
}

beforeEach(co.wrap(function* () {
pluginOptions = {};

plugins = [
[StripFilteredImports, pluginOptions]
];

input = yield createTempDir();
}));

afterEach(co.wrap(function* () {
if (input) {
yield input.dispose();
}
if (output) {
yield output.dispose();
}

input = output = undefined;
}));

it('Returns a plugin', function() {
let plugin = StripFilteredImports();

expect(plugin).to.be.ok;
});

it('Does not alter a file if no imports are meant to be filtered', co.wrap(function*() {
const input = stripIndent`
import Foo from 'bar';
import { baz } from 'none';
import * as drinks from 'drinks';
import 'bem';
`;
const result = yield transform(input);

expect(result).to.equal(stripNewlines(input));
}));

it('Properly strips desired imports and specifiers', co.wrap(function*() {
const input = stripIndent`
import Foo from 'bar';
import { bit } from 'wow';
import { baz, bell } from 'none';
import { foo } from 'happy';
import * as drinks from 'drinks';
import * as dranks from 'dranks';
import 'bem';
import 'bosh';
import 'bell';
`;

pluginOptions.none = ['baz'];
pluginOptions.bar = true;
pluginOptions.drinks = '*';
pluginOptions.wow = ['bit'];
pluginOptions.bem = ['biz'];
pluginOptions.bosh = '*';
pluginOptions.dranks = ['bex'];
pluginOptions.bell = true;

const expectedOutput = stripNewlines(stripIndent`
import { bell } from 'none';
import { foo } from 'happy';
import * as dranks from 'dranks';
import 'bem';
`);
const result = yield transform(input);

expect(result).to.equal(expectedOutput);
}));
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
"broccoli-funnel": "^1.2.0",
"broccoli-merge-trees": "^2.0.0",
"broccoli-rollup": "^1.2.0",
"broccoli-test-helper": "^1.2.0",
"calculate-cache-key-for-tree": "^1.1.0",
"chalk": "^1.1.1",
"co": "^4.6.0",
"common-tags": "^1.4.0",
"ember-cli-babel": "^6.4.1",
"ember-cli-path-utils": "^1.0.0",
"ember-cli-string-utils": "^1.0.0",
Expand Down
Loading

0 comments on commit f3bfad8

Please sign in to comment.