Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to release #5188

Merged
merged 2 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
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
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: 36 additions & 7 deletions lib/transforms/babel-plugin-remove-imports.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
var path = require('path');
'use strict';

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 +26,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);
}));
});
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
"broccoli-funnel": "^1.2.0",
"broccoli-merge-trees": "^1.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",
"ember-cli-babel": "^6.1.0",
"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",
"ember-cli-test-info": "^1.0.0",
Expand Down
Loading