Used to converted import statements into an inline require statements. Useful when trying to avoid requiring modules that are eventually unused in a particular code path and without using a bundler for tree shaking.
By converting each import statement into it's inlined require statement equivalent your code load only the necessary modules for it's runtime operation.
For example if plugin is applied to the code:
import { mapKeys } from 'lodash'
let myObj = {...}
if (someCondition) {
myObj = mapKeys(myObj, (v, k) => v)
}
the output will be
let myObj = {...}
if (someCondition) {
myObj = require('lodash').mapKeys(myObj, (v, k) => v)
}
and so lodash
module will not be loaded unless someCondition
is actually true.
-
namespace imports
import { namespace } from 'module' const value = namespace
const value = require('module').namespace
-
renamed namespace imports
import { namespace as somethingElse } from 'module' const value = namespace
const value = require('module').namespace
-
all namespaces imports
import * as allNamespace from 'module' const value = allNamespace
const value = require('module')
-
default import
import allNamespace from 'module' const value = allNamespace
const value = require('module').default
name | type | default | description |
---|---|---|---|
verbose | Boolean | false | Toggle verbose logging |
excludeFiles | Array<String/RegExp> | [ ] | Skip replacing import statements in certain files |
excludeModules | Array<String/RegExp> | [ ] | Skip replacing import statements of certain modules in all files |
naiveStringReplace | Boolean | false | Use string literal value when modifying code instead of using AST nodes |