babel-plugin-filter-imports
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,18 +6,16 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _isRemovablePath = require('./isRemovablePath'); | ||
var _isRemovablePath = _interopRequireDefault(require("./isRemovablePath")); | ||
var _isRemovablePath2 = _interopRequireDefault(_isRemovablePath); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var findReferenceRemovalPath = function findReferenceRemovalPath(referencePath) { | ||
var parent = referencePath.findParent(_isRemovablePath2.default); | ||
const findReferenceRemovalPath = referencePath => { | ||
const parent = referencePath.findParent(_isRemovablePath.default); | ||
if (parent) return parent; | ||
if ((0, _isRemovablePath2.default)(referencePath)) return referencePath; | ||
if ((0, _isRemovablePath.default)(referencePath)) return referencePath; | ||
throw new Error(['Cannot find the path for removal, please open issue with code example and the stack trace on Github:', 'https://github.com/ember-cli/babel-plugin-filter-imports'].join(' ')); | ||
}; | ||
exports.default = findReferenceRemovalPath; | ||
var _default = findReferenceRemovalPath; | ||
exports.default = _default; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,32 +6,26 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
var _lodash = _interopRequireDefault(require("lodash")); | ||
var _lodash = require('lodash'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
const getSpecifiersForRemoval = (members, specifiers) => _lodash.default.transform(specifiers, (result, specifier) => { | ||
if (_lodash.default.includes(members, '*')) { | ||
result.push(...specifiers); | ||
return false; | ||
} | ||
var getSpecifiersForRemoval = function getSpecifiersForRemoval(members, specifiers) { | ||
return _lodash2.default.transform(specifiers, function (result, specifier) { | ||
if (_lodash2.default.includes(members, '*')) { | ||
result.push.apply(result, _toConsumableArray(specifiers)); | ||
return false; | ||
} | ||
if (t.isImportDefaultSpecifier(specifier) && _lodash.default.includes(members, 'default')) { | ||
result.push(specifier); | ||
} | ||
if (t.isImportDefaultSpecifier(specifier) && _lodash2.default.includes(members, 'default')) { | ||
result.push(specifier); | ||
} | ||
if (_lodash.default.includes(members, _lodash.default.get(specifier, 'local.name'))) result.push(specifier); | ||
}); | ||
if (_lodash2.default.includes(members, _lodash2.default.get(specifier, 'local.name'))) result.push(specifier); | ||
}); | ||
}; | ||
exports.default = getSpecifiersForRemoval; | ||
var _default = getSpecifiersForRemoval; | ||
exports.default = _default; |
109
lib/index.js
@@ -1,86 +0,71 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _lodash = require('lodash'); | ||
var _lodash = _interopRequireDefault(require("lodash")); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _path = require('path'); | ||
var _getSpecifierNames = _interopRequireDefault(require("./getSpecifierNames")); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _removeReferences = _interopRequireDefault(require("./removeReferences")); | ||
var _getSpecifierNames = require('./getSpecifierNames'); | ||
var _getSpecifierNames2 = _interopRequireDefault(_getSpecifierNames); | ||
var _removeReferences = require('./removeReferences'); | ||
var _removeReferences2 = _interopRequireDefault(_removeReferences); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
module.exports = function () { | ||
return { | ||
manipulateOptions: function manipulateOptions(opts, parserOptions) { | ||
parserOptions.plugins.push('decorators'); | ||
parserOptions.plugins.push('exportExtensions'); | ||
}, | ||
visitor: { | ||
ImportDeclaration: function ImportDeclaration(path, _ref) { | ||
var opts = _ref.opts; | ||
var imports = opts.imports, | ||
module.exports = () => ({ | ||
manipulateOptions: (opts, parserOptions) => { | ||
parserOptions.plugins.push('decorators'); | ||
parserOptions.plugins.push('exportDefaultFrom'); | ||
parserOptions.plugins.push('exportNamespaceFrom'); | ||
}, | ||
visitor: { | ||
ImportDeclaration: (path, { | ||
opts | ||
}) => { | ||
const imports = opts.imports, | ||
_opts$keepImports = opts.keepImports, | ||
keepImports = _opts$keepImports === undefined ? false : _opts$keepImports; | ||
var _path$node = path.node, | ||
keepImports = _opts$keepImports === void 0 ? false : _opts$keepImports; | ||
const _path$node = path.node, | ||
source = _path$node.source, | ||
specifiers = _path$node.specifiers; | ||
var members = _lodash2.default.get(imports, _lodash2.default.get(source, 'value')); | ||
const members = _lodash.default.get(imports, _lodash.default.get(source, 'value')); | ||
/* | ||
* Heads up! This condition omits processing of non-filtered imports. | ||
*/ | ||
/* | ||
* Heads up! This condition omits processing of non-filtered imports. | ||
*/ | ||
if (!members) return; | ||
var specifiersForRemoval = (0, _getSpecifierNames2.default)(members, specifiers); | ||
var specifierNames = _lodash2.default.map(specifiersForRemoval, 'local.name'); | ||
if (!members) return; | ||
const specifiersForRemoval = (0, _getSpecifierNames.default)(members, specifiers); | ||
_lodash2.default.forEach(specifierNames, function (specifier) { | ||
return (0, _removeReferences2.default)(path, specifier); | ||
}); | ||
const specifierNames = _lodash.default.map(specifiersForRemoval, 'local.name'); | ||
if (keepImports) return false; | ||
if (specifiers.length === specifierNames.length) { | ||
path.remove(); | ||
return; | ||
} | ||
_lodash.default.forEach(specifierNames, specifier => (0, _removeReferences.default)(path, specifier)); | ||
_lodash2.default.set(path, 'node.specifiers', _lodash2.default.without.apply(_lodash2.default, [specifiers].concat(_toConsumableArray(specifiersForRemoval)))); | ||
}, | ||
if (keepImports) return false; | ||
ExportNamedDeclaration: function ExportNamedDeclaration(path, _ref2) { | ||
var opts = _ref2.opts; | ||
if (specifiers.length === specifierNames.length) { | ||
path.remove(); | ||
return; | ||
} | ||
var declaration = _lodash2.default.get(path, 'node.declaration'); | ||
_lodash.default.set(path, 'node.specifiers', _lodash.default.without(specifiers, ...specifiersForRemoval)); | ||
}, | ||
ExportNamedDeclaration: (path, { | ||
opts | ||
}) => { | ||
const declaration = _lodash.default.get(path, 'node.declaration'); // Heads up! Exports that have decrations will be handled | ||
// by the ImportDeclaration visitor | ||
// Heads up! Exports that have decrations will be handled | ||
// by the ImportDeclaration visitor | ||
if (declaration) return; | ||
var imports = opts.imports; | ||
if (declaration) return; | ||
const imports = opts.imports; | ||
var source = _lodash2.default.get(path, 'node.source.value'); | ||
const source = _lodash.default.get(path, 'node.source.value'); | ||
if (_lodash2.default.has(imports, source)) path.remove(); | ||
} | ||
if (_lodash.default.has(imports, source)) path.remove(); | ||
} | ||
}; | ||
}; | ||
} | ||
}); // Provide the path to the package's base directory for caching with broccoli | ||
// Ref: https://github.com/babel/broccoli-babel-transpiler#caching | ||
// Provide the path to the package's base directory for caching with broccoli | ||
// Ref: https://github.com/babel/broccoli-babel-transpiler#caching | ||
module.exports.baseDir = function () { | ||
return _path2.default.resolve(__dirname, '..'); | ||
}; | ||
module.exports.baseDir = () => _path.default.resolve(__dirname, '..'); |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,13 +6,11 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
const isRemovablePath = path => t.isArrowFunctionExpression(path) || t.isDecorator(path) || t.isExpressionStatement(path) || t.isExportSpecifier(path) || t.isExportNamedDeclaration(path) || t.isReturnStatement(path) || t.isVariableDeclarator(path); | ||
var isRemovablePath = function isRemovablePath(path) { | ||
return t.isArrowFunctionExpression(path) || t.isDecorator(path) || t.isExpressionStatement(path) || t.isExportSpecifier(path) || t.isExportNamedDeclaration(path) || t.isReturnStatement(path) || t.isVariableDeclarator(path); | ||
}; | ||
exports.default = isRemovablePath; | ||
var _default = isRemovablePath; | ||
exports.default = _default; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,19 +6,19 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _lodash = require('lodash'); | ||
var _lodash = _interopRequireDefault(require("lodash")); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var removeExportSpecifier = function removeExportSpecifier(path) { | ||
var parent = path.parentPath; | ||
var specifiers = _lodash2.default.get(parent, 'node.specifiers'); | ||
const removeExportSpecifier = path => { | ||
const parent = path.parentPath; | ||
path.remove(); | ||
const specifiers = _lodash.default.get(parent, 'node.specifiers'); | ||
// Heads up! We should also remove ExportNamedDeclaration if it become empty | ||
path.remove(); // Heads up! We should also remove ExportNamedDeclaration if it become empty | ||
if (specifiers.length === 0) parent.remove(); | ||
}; | ||
exports.default = removeExportSpecifier; | ||
var _default = removeExportSpecifier; | ||
exports.default = _default; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,33 +6,26 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
var _lodash = _interopRequireDefault(require("lodash")); | ||
var _lodash = require('lodash'); | ||
var _findReferenceRemovalPath = _interopRequireDefault(require("./findReferenceRemovalPath")); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _removeExportSpecifier = _interopRequireDefault(require("./removeExportSpecifier")); | ||
var _findReferenceRemovalPath = require('./findReferenceRemovalPath'); | ||
var _findReferenceRemovalPath2 = _interopRequireDefault(_findReferenceRemovalPath); | ||
var _removeExportSpecifier = require('./removeExportSpecifier'); | ||
var _removeExportSpecifier2 = _interopRequireDefault(_removeExportSpecifier); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
var removeReferences = function removeReferences(path, specifier) { | ||
const removeReferences = (path, specifier) => { | ||
if (!path.scope.getBinding(specifier)) return; | ||
var _path$scope$getBindin = path.scope.getBinding(specifier), | ||
referencePaths = _path$scope$getBindin.referencePaths; | ||
const _path$scope$getBindin = path.scope.getBinding(specifier), | ||
referencePaths = _path$scope$getBindin.referencePaths; | ||
_lodash2.default.forEach(referencePaths, function (referencePath) { | ||
var removalPath = (0, _findReferenceRemovalPath2.default)(referencePath); | ||
_lodash.default.forEach(referencePaths, referencePath => { | ||
const removalPath = (0, _findReferenceRemovalPath.default)(referencePath); | ||
if (removalPath.removed) return; | ||
if (removalPath.removed) return; | ||
if (t.isArrowFunctionExpression(removalPath)) { | ||
@@ -42,7 +35,9 @@ removalPath.get('body').remove(); | ||
} | ||
if (t.isExportSpecifier(removalPath)) { | ||
(0, _removeExportSpecifier2.default)(removalPath); | ||
(0, _removeExportSpecifier.default)(removalPath); | ||
return; | ||
} | ||
if (t.isVariableDeclarator(removalPath)) removeReferences(removalPath, _lodash2.default.get(removalPath, 'node.id.name')); | ||
if (t.isVariableDeclarator(removalPath)) removeReferences(removalPath, _lodash.default.get(removalPath, 'node.id.name')); | ||
removalPath.remove(); | ||
@@ -52,2 +47,3 @@ }); | ||
exports.default = removeReferences; | ||
var _default = removeReferences; | ||
exports.default = _default; |
{ | ||
"name": "babel-plugin-filter-imports", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "A babel transform for filtering out imports", | ||
@@ -15,4 +15,4 @@ "main": "lib/index.js", | ||
"lint:fix": "prettier --write \"src/**/*.js\" \"test/test.js\" && eslint --fix \"src/**/*.js\" \"test/test.js\"", | ||
"test": "mocha --require babel-register \"test/*.js\"", | ||
"test:watch": "npm run test -- --watch --watch-extensions js" | ||
"test": "mocha --require @babel/register \"test/*.js\"", | ||
"test:watch": "yarn test --watch --watch-extensions js" | ||
}, | ||
@@ -34,11 +34,11 @@ "repository": { | ||
"dependencies": { | ||
"babel-types": "^6.26.0", | ||
"@babel/types": "^7.0.0-beta.46", | ||
"lodash": "^4.17.10" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"@babel/cli": "^7.0.0-beta.46", | ||
"@babel/core": "^7.0.0-beta.46", | ||
"@babel/preset-env": "^7.0.0-beta.46", | ||
"@babel/register": "^7.0.0-beta.46", | ||
"babel-eslint": "^8.2.3", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.26.0", | ||
"eslint": "^4.19.1", | ||
@@ -45,0 +45,0 @@ "eslint-config-prettier": "^2.9.0", |
@@ -6,6 +6,15 @@ # babel-plugin-filter-imports | ||
*This plugin is for Babel 6. If you need to support Babel 5 use the [v0.2.x](https://github.com/ember-cli/babel-plugin-filter-imports/tree/v0.2.x) branch.* | ||
This babel plugin is used to removed references to imports within a module. This can be useful for removing debugging statements when doing a production build of your code. It is often used in conjunction with other tools like Uglify that perform dead code elimination. | ||
## Installation | ||
```sh | ||
$ yarn add --dev babel-plugin-filter-imports | ||
``` | ||
*This plugin is for Babel 7. If you need to support:* | ||
- *Babel 6 use the [babel6](https://github.com/ember-cli/babel-plugin-filter-imports/tree/babel6) branch* | ||
- *Babel 5 use the [v0.2.x](https://github.com/ember-cli/babel-plugin-filter-imports/tree/v0.2.x) branch* | ||
## Example | ||
@@ -54,3 +63,3 @@ | ||
## Upgrade to `1.x` | ||
## Upgrade to `1.x`/`2.x` | ||
@@ -57,0 +66,0 @@ There were breaking changes in the plugin configuration, you must update it to work correctly. |
@@ -1,2 +0,2 @@ | ||
import * as t from 'babel-types' | ||
import * as t from '@babel/types' | ||
import _ from 'lodash' | ||
@@ -3,0 +3,0 @@ |
@@ -10,3 +10,4 @@ import _ from 'lodash' | ||
parserOptions.plugins.push('decorators') | ||
parserOptions.plugins.push('exportExtensions') | ||
parserOptions.plugins.push('exportDefaultFrom') | ||
parserOptions.plugins.push('exportNamespaceFrom') | ||
}, | ||
@@ -13,0 +14,0 @@ |
@@ -1,2 +0,2 @@ | ||
import * as t from 'babel-types' | ||
import * as t from '@babel/types' | ||
@@ -3,0 +3,0 @@ const isRemovablePath = path => |
@@ -1,2 +0,2 @@ | ||
import * as t from 'babel-types' | ||
import * as t from '@babel/types' | ||
import _ from 'lodash' | ||
@@ -3,0 +3,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86
22649
254
+ Added@babel/types@^7.0.0-beta.46
+ Added@babel/helper-string-parser@7.24.8(transitive)
+ Added@babel/helper-validator-identifier@7.24.7(transitive)
+ Added@babel/types@7.25.6(transitive)
+ Addedto-fast-properties@2.0.0(transitive)
- Removedbabel-types@^6.26.0
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedesutils@2.0.3(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedto-fast-properties@1.0.3(transitive)