1.0.1 • Published 1 year ago

import-to-inlined-require-babel-plugin v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Import to Inline Require - babel plugin

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.

conversion examples

  1. namespace imports

    import { namespace } from 'module'
    
    const value = namespace
    const value = require('module').namespace
  2. renamed namespace imports

    import { namespace as somethingElse } from 'module'
    
    const value = namespace
    const value = require('module').namespace
  3. all namespaces imports

    import * as allNamespace from 'module'
    
    const value = allNamespace
    const value = require('module')
  4. default import

    import allNamespace from 'module'
    
    const value = allNamespace
    const value = require('module').default

Configuration options

nametypedefaultdescription
verboseBooleanfalseToggle verbose logging
excludeFilesArray<String/RegExp> Skip replacing import statements in certain files
excludeModulesArray<String/RegExp> Skip replacing import statements of certain modules in all files
naiveStringReplaceBooleanfalseUse string literal value when modifying code instead of using AST nodes