Giter Site home page Giter Site logo

jonschlinkert / lazy-cache Goto Github PK

View Code? Open in Web Editor NEW
51.0 3.0 12.0 36 KB

Cache requires to be lazy-loaded when needed. Uses node's own require system with tried and true, plain-vanilla JavaScript getters.

License: MIT License

JavaScript 100.00%
require cache object vanilla javascript

lazy-cache's Introduction

lazy-cache NPM version NPM monthly downloads NPM total downloads Linux Build Status

Cache requires to be lazy-loaded when needed.

Install

Install with npm:

$ npm install --save lazy-cache

Heads up!

It's suprising how many libraries are in the average dependency tree that don't belong there for one reason or another. Either because they were accidentally listed as dependencies instead of devDepedencies, or they are required in a file as variables, but the variable is never actually used (poor linting), and so on. Or because the maintainer made the decision to add the deps, even though they will never (or can't ever) be used by 99.9% of users.

Worse, many libraries like chalk and shelljs actually execute code when require() is called!? (shelljs was modifying the String.prototype, and chalk loops over some objects to dynamically create methods). In other words, they do something like this:

// in the main export of a library, if you do this it will
// automatically modify the String.prototype _globally_, 
// the moment node.js loads the dependency tree
String.prototype.foo = function() {};

// same if you do something like this
// (dont' do this, ever. wrap this kind of code in a function
// and allow implementors to decide when to call it)
while (foo) {
  // do stuff
}

In any case, just having these libraries in your dependency tree somewhere means that their code will excecute the moment you run your application even if the libraries are never called by your application or any other code anywhere in the tree.

solution

lazy-cache doesn't use any "magic", it uses native, plain-vanilla, tried and true javascript getters to call node's require() system.

Faster, safer code

There many advantage to this, the main is that requires are loaded on demand, so only code that is actually used will ever be loaded. As a result, applications will load faster (sometimes much faster - we've seen load times drop from ~1 second to less than 50 milliseconds).

Moreover, in some cases this also avoids inadvertently loading libraries that execute code or modifies globals, etc.

webpack users

If you use webpack and are experiencing issues with lazy-cache, this is a known bug caused by webpack, not lazy-cache. There is a solution though, you can use unlazy-loader, a webpack loader that fixes the webpack bug.

Usage

var utils = require('lazy-cache')(require);

Use as a property on lazy

The module is also added as a property to the lazy function so it can be called without having to call a function first.

var utils = require('lazy-cache')(require);

// `npm install glob`
utils('glob');

// glob sync
console.log(utils.glob.sync('*.js'));

// glob async
utils.glob('*.js', function (err, files) {
  console.log(files);
});

Use as a function

var utils = require('lazy-cache')(require);
var glob = utils('glob');

// `glob` is a now a function that may be called when needed
glob().sync('foo/*.js');

Aliases

An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.

Example

var utils = require('lazy-cache')(require);

// alias `ansi-yellow` as `yellow`
utils('ansi-yellow', 'yellow');
console.log(utils.yellow('foo'));

Dot notation may also be used in the alias to create an object hierarchy.

Example

var utils = require('lazy-cache')(require);
utils('ansi-cyan', 'color.cyan');
utils('ansi-yellow', 'color.yellow');
utils('ansi-magenta', 'color.magenta');
console.log(utils.color.cyan('foo'));
console.log(utils.color.yellow('bar'));
console.log(utils.color.magenta('baz'));

Browserify usage

Example

var utils = require('lazy-cache')(require);
// temporarily re-assign `require` to trick browserify
var fn = require;
require = utils;
// list module dependencies (here, `require` is actually `lazy-cache`)
require('glob');
require = fn; // restore the native `require` function

/**
 * Now you can use glob with the `utils.glob` variable
 */

// sync
console.log(utils.glob.sync('*.js'));

// async
utils.glob('*.js', function (err, files) {
  console.log(files.join('\n'));
});

Kill switch

To force lazy-cache to immediately invoke all dependencies, do:

process.env.UNLAZY = true;

About

Related projects

lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
31 jonschlinkert
27 doowb

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb-generate-readme, v0.2.0, on November 07, 2016.

lazy-cache's People

Contributors

doowb avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lazy-cache's Issues

webpack support

Hi Jon,

It's pretty amazing just how many libraries I keep discovering of yours. You have blazed some amazing trails!

This module seems to come up as a bit of a problem unfortunately because it is pulled into by browser/ cross-platform code and I use webpack as my bundling solution. Basically, I emulate node's fs module in the browser and micromatch pulls in braces which pulls in this.

I realize that there isn't really a quick fix for this except maybe to drop this use of this module in the dependent modules which is probably a lot to ask! and I'm assuming that this lazy loading is necessary for either fast initialization time (eg. node-based spawned scripts) or for maybe for optional dependencies or both...but I have to ask...

Given that modules systems already cache the lookup of modules, is this module necessary (I'm assuming there must be some critical use cases that benefit from lazy loading, but am hoping the benefit is marginal)? or from another angle, is there a way webpack's static analysis can be supported?

Right now, I'm having to avoid this part of your modules ecosystem and I'd prefer not to. If you have any suggestions or ideas (besides dropping webpack), I'm all ears...

error when trying to require local file

for example

var fn = require
require = utils

require('quxify')
require('./foobarify')

require = fn
module.exports = utils

throws following

TypeError: Cannot read property 'length' of null
       at camelcase (/home/charlike/latest/lazy-arrayify/node_modules/lazy-cache/index.js:105:10)
       at proxy (/home/charlike/latest/lazy-arrayify/node_modules/lazy-cache/index.js:27:13)

Error using handlebars-helpers with browserify

Trying to use your handlebars-helpers module with browserify and I am getting the following error:

Error: Cannot find module 'extend-shallow'

which, if I understand correctly, is lazy-loaded via lazy-cache.

I tried the recommended browserify approach here (but may not have done it correctly):

var utils = require('lazy-cache')(require);
// temporarily re-assign `require` to trick browserify
var fn = require;
require = utils;
// list module dependencies (here, `require` is actually `lazy-cache`)
require('extend-shallow');
require = fn; // restore the native `require` function

var helpers = require('handlebars-helpers')({
  handlebars: Handlebars
});

I also tried the killswitch ENV variable, but neither has worked for me. Same error as above.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.