import-to-inlined-require-babel-plugin

1.0.1 • Public • Published

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

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

Readme

Keywords

none

Package Sidebar

Install

npm i import-to-inlined-require-babel-plugin

Weekly Downloads

2

Version

1.0.1

License

ISC

Unpacked Size

26.2 kB

Total Files

11

Last publish

Collaborators

  • lirancr