@startupjs/babel-plugin-eliminator
Cut out specific named exports or do the opposite -- only keep the ones you specify
Install
npm i @startupjs/babel-plugin-eliminator
Usage
Removing specific named exports
Options:
{
"plugins": [
["@startupjs/babel-plugin-eliminator", {"removeExports": ["foo", "default"]}]
]
}
Input:
import usedByFoo from 'used-by-foo'
import usedByDefault from 'used-by-default'
import usedByBar from 'used-by-bar'
const varInFoo = 'var-in-foo'
const varInDefault = 'var-in-default'
const varInBar = 'var-in-bar'
export const foo = () => {
return usedByFoo(varInFoo)
}
export default () => {
return usedByDefault(varInDefault)
}
export function bar () {
return usedByBar(varInBar)
}
Output:
import usedByBar from 'used-by-bar'
const varInBar = 'var-in-bar'
export var foo = 1
export default 1
export function bar () {
return usedByBar(varInBar)
}
Keep only the specified exports
Options:
{
"plugins": [
["@startupjs/babel-plugin-eliminator", {"keepExports": ["foo", "default"]}]
]
}
Input:
import usedByFoo from 'used-by-foo'
import usedByDefault from 'used-by-default'
import usedByBar from 'used-by-bar'
const varInFoo = 'var-in-foo'
const varInDefault = 'var-in-default'
const varInBar = 'var-in-bar'
export const foo = () => {
return usedByFoo(varInFoo)
}
export default () => {
return usedByDefault(varInDefault)
}
export function bar () {
return usedByBar(varInBar)
}
Output:
import usedByFoo from 'used-by-foo'
import usedByDefault from 'used-by-default'
const varInFoo = 'var-in-foo'
const varInDefault = 'var-in-default'
export const foo = () => {
return usedByFoo(varInFoo)
}
export default () => {
return usedByDefault(varInDefault)
}
export var bar = 1
Removing Keys within Objects of Magic Function Calls
This feature in the @startupjs/babel-plugin-eliminator
allows selective retention or removal of keys within objects passed to specific functions. It's particularly useful for customizing the shape of large objects or configurations passed to 'magic' functions.
To utilize this feature, configure the keepObjectKeysOfFunction
option in your Babel plugin settings. This allows you to specify the functions and the particular keys within their object arguments that you want to keep.
Options:
{
"plugins": [
[
"@startupjs/babel-plugin-eliminator",
{
"keepObjectKeysOfFunction": {
"createProject": {
"magicImports": ["startupjs/registry", "@startupjs/registry"],
"targetObjectJsonPath": "$.plugins.*",
"ensureOnlyKeys": ["client", "isomorphic", "server", "build"],
"keepKeys": ["client", "isomorphic"]
}
}
}
]
]
}
In this configuration:
createProject
is the function where object key manipulation occurs.magicImports
specifies the module imports that identify createProject
as a magic function.targetObjectJsonPath
(optional) is the JSON path to the target object path within the function's first argument. By default the top-level object itself is used.keepKeys
specifies which keys in the object should be retained in the output.ensureOnlyKeys
(optional) lists all possible keys in the object, providing a way to validate the object structure.
Input
import { createProject } from 'startupjs/registry'
export default createProject({
plugins: {
'serve-static-promo': {
client: {
redirectUrl: '/promo',
},
server: {
},
build: {
},
isomorphic: {
}
},
permissions: {
client: {
},
server: {
},
build: {
},
isomorphic: {
}
}
}
})
Output
import { createProject } from "startupjs/registry";
export default createProject({
plugins: {
"serve-static-promo": {
client: {
redirectUrl: "/promo",
},
isomorphic: {
}
},
permissions: {
client: {
},
isomorphic: {
}
}
}
});
Options
type PluginOpts = {
removeExports?: string[],
keepExports?: string[],
keepObjectKeysOfFunction?: {
[functionName: string]: {
magicImports: string[],
targetObjectJsonPath?: string,
ensureOnlyKeys?: string[],
keepKeys: string[]
}
},
done?: (state: PluginState) => void
}
Credits
This plugin is a fork of egoist/babel-plugin-eliminator with the following changes:
- refactor to pure JS
- rename
namedExports
to removeExports
- add an alternative
keepExports
option. If specified, only these ones are gonna be kept and all other exports will be removed - handle
default
export - add feature to remove object keys within magic function calls
License
MIT