Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-lazy-require

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-lazy-require

Transform CommonJS require statements into lazily evaluated imports

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
168
decreased by-19.62%
Maintainers
1
Weekly downloads
 
Created
Source

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();
}

Keywords

FAQs

Package last updated on 04 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc