Have ideas to improve npm?Join in the discussion! »

    babel-plugin-lazy-require

    1.0.3 • Public • Published

    Build Status NPM version Coverage Status

    babel-plugin-lazy-require

    Transform global require statements that run on module load to lazily evaluated statements that get evaluted when first accessed later on in the file.

    This is particularly useful when you have modules that are only needed under certain conditions and when startup time and/or memory footprint are important.

    Usage

    To install:

    npm install --save-dev babel-cli@6 babel-plugin-lazy-require
    

    To run:

    babel <your-code> --plugins babel-plugin-lazy-require
    

    Example

    Input

    // Module is imported here
    const someModule = require('some-module');
     
    function myCode() { 
        // Module is only actually used here
        someModule.doSomething();
    }

    Output

    const _someModule = {
        initialized: false
    };
     
    const _imports = {
        get someModule() {
            if (!_someModule.initialized) {
                _someModule.value = require('some-module');
                _someModule.initialized = true;
            }
            return _someModule.value;
        }
    }
     
    function myCode() {
        // Module is imported and used here
        _imports.someModule.doSomething();
    }

    Install

    npm i babel-plugin-lazy-require

    DownloadsWeekly Downloads

    243

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    10.5 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar