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

Only prune when unreachable (not merely unused) and add module ignore option #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ By default, only `true` and `false` identifiers are considered truthy/falsy.

- `falsyExpressions` : `Array<string>` - Expressions (in addition to `false`) to be treated as falsy
- `truthyExpressions` : `Array<string>` - Expressions (in addition to `true`) to be treated as truthy
- `ignoredModules` : `Array<string>` - Packages/modules to ignore (i.e. will never be pruned by this plugin)

### Examples

Expand Down
35 changes: 23 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
type PluginOpts = {
falsyExpressions: Array<string>,
truthyExpressions: Array<string>,
ignoredModules: Array<string>,
};
*/

module.exports = (
api /*: any */,
{
falsyExpressions: falsyOpt = [],
truthyExpressions: truthyOpt = []
truthyExpressions: truthyOpt = [],
ignoredModules: modulesArr = []
} /*: PluginOpts */ = {}
) => {
const { types: t } = api;
Expand All @@ -23,6 +25,8 @@ module.exports = (
throw new Error("truthyExpressions must be an array");
}

const ignoredModules = new Set(modulesArr);

function stringToExpression(expr) {
if (typeof expr !== "string") {
expr = String(expr);
Expand Down Expand Up @@ -132,25 +136,32 @@ module.exports = (
// Imports with no specifiers is probably specifically for side effects
let shakeDeclaration = specifiers.length > 0;

const moduleName = path.node.source.value;
if (ignoredModules.has(moduleName)) {
return;
}

for (const specifier of specifiers) {
let shakeSpecifier = true;

const localPath = specifier.get("local");
const localName = localPath.node.name;
// This should not be hardcoded to React and/or improve compat with JSX transform
if (localName === "React") {
shakeSpecifier = false;
shakeDeclaration = false;
break;
}
const binding = localPath.scope.bindings[localName];
if (binding) {
const refPaths = binding.referencePaths;
for (const path of refPaths) {
const unreachable = isPathCertainlyUnreachable(path);
if (!unreachable) {
shakeSpecifier = false;
shakeDeclaration = false;

if (refPaths.length === 0) {
// If no references exist, then this specifier is almost certainly
// being imported for side effects.
shakeSpecifier = false;
shakeDeclaration = false;
} else {
for (const path of refPaths) {
const unreachable = isPathCertainlyUnreachable(path);
if (!unreachable) {
shakeSpecifier = false;
shakeDeclaration = false;
}
}
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.8.3",
"babel-plugin-tester": "^8.0.1",
"flow-bin": "^0.116.1",
"@babel/plugin-syntax-jsx": "^7.8.3",
"babel-plugin-tester": "^9.0.0",
"flow-bin": "^0.122.0",
"jest": "^25.1.0"
},
"repository": "rtsao/babel-plugin-transform-prune-unused-imports",
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/ignored-modules/ignored-module/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { unreachable } from "some-ignored-package";
import { also_unreachable } from "some-package";

if (false) {
unreachable;
also_unreachable;
}
6 changes: 6 additions & 0 deletions test/fixtures/ignored-modules/ignored-module/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { unreachable } from "some-ignored-package";

if (false) {
unreachable;
also_unreachable;
}
2 changes: 2 additions & 0 deletions test/fixtures/jsx/plain/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from "react";
<div>Hello world!</div>;
2 changes: 2 additions & 0 deletions test/fixtures/jsx/plain/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from "react";
<div>Hello world!</div>;
4 changes: 4 additions & 0 deletions test/fixtures/jsx/unreachable/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from "react";
if (false) {
<div>Hello world!</div>;
}
5 changes: 5 additions & 0 deletions test/fixtures/jsx/unreachable/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react";

if (false) {
<div>Hello world!</div>;
}
2 changes: 0 additions & 2 deletions test/fixtures/vanilla/basic/output.js

This file was deleted.

9 changes: 6 additions & 3 deletions test/fixtures/vanilla/shadowing-block-scope/code.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { unused } from "some-pkg";
import { unreachable } from "some-pkg";

{
let unused = other;
unused;
let unreachable = other;
unreachable;
}
if (false) {
unreachable;
}
8 changes: 6 additions & 2 deletions test/fixtures/vanilla/shadowing-block-scope/output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
let unused = other;
unused;
let unreachable = other;
unreachable;
}

if (false) {
unreachable;
}
9 changes: 6 additions & 3 deletions test/fixtures/vanilla/shadowing-function-scope/code.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { unused } from "some-pkg";
import { unreachable } from "some-pkg";

function foo(unused) {
unused;
function foo(unreachable) {
unreachable;
}
if (false) {
unreachable;
}
8 changes: 6 additions & 2 deletions test/fixtures/vanilla/shadowing-function-scope/output.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function foo(unused) {
unused;
function foo(unreachable) {
unreachable;
}

if (false) {
unreachable;
}
3 changes: 3 additions & 0 deletions test/fixtures/vanilla/unused/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { unused } from "some-pkg";
import { unused2, used } from "another-pkg";
used;
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ pluginTester({
fixtures: __dirname + "/fixtures/vanilla"
});

pluginTester({
plugin: plugin,
fixtures: __dirname + "/fixtures/jsx",
babelOptions: {
plugins: ["@babel/plugin-syntax-jsx"]
}
});

pluginTester({
plugin: plugin,
fixtures: __dirname + "/fixtures/ignored-modules",
pluginOptions: {
ignoredModules: ["some-ignored-package"]
}
});

pluginTester({
plugin: plugin,
fixtures: __dirname + "/fixtures/browser-env",
Expand Down
Loading