UNPKG

6.82 kBMarkdownView Raw
1# lazy-cache [![NPM version](https://img.shields.io/npm/v/lazy-cache.svg?style=flat)](https://www.npmjs.com/package/lazy-cache) [![NPM monthly downloads](https://img.shields.io/npm/dm/lazy-cache.svg?style=flat)](https://npmjs.org/package/lazy-cache) [![NPM total downloads](https://img.shields.io/npm/dt/lazy-cache.svg?style=flat)](https://npmjs.org/package/lazy-cache) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/lazy-cache.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/lazy-cache)
2
3> Cache requires to be lazy-loaded when needed.
4
5## Install
6
7Install with [npm](https://www.npmjs.com/):
8
9```sh
10$ npm install --save lazy-cache
11```
12
13## Heads up!
14
15It'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](https://github.com/felixge/node-dateformat/issues/36)) be used by 99.9% of users.
16
17Worse, many libraries like chalk and [shelljs](https://github.com/eslint/eslint/issues/7316) 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:
18
19```js
20// in the main export of a library, if you do this it will
21// automatically modify the String.prototype _globally_,
22// the moment node.js loads the dependency tree
23String.prototype.foo = function() {};
24
25// same if you do something like this
26// (dont' do this, ever. wrap this kind of code in a function
27// and allow implementors to decide when to call it)
28while (foo) {
29 // do stuff
30}
31```
32
33In 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_.
34
35**solution**
36
37lazy-cache doesn't use any "magic", it uses native, plain-vanilla, tried and true javascript getters to call node's `require()` system.
38
39**Faster, safer code**
40
41There main advantage to this, the main is that `require`s 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).
42
43Moreover, in some cases this also avoids inadvertently loading libraries that execute code or modifies globals, etc.
44
45**webpack users**
46
47If 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](https://github.com/doowb/unlazy-loader), a webpack loader that _fixes the webpack bug_.
48
49## Usage
50
51```js
52var utils = require('lazy-cache')(require);
53```
54
55**Use as a property on `lazy`**
56
57The module is also added as a property to the `lazy` function so it can be called without having to call a function first.
58
59```js
60var utils = require('lazy-cache')(require);
61
62// `npm install glob`
63utils('glob');
64
65// glob sync
66console.log(utils.glob.sync('*.js'));
67
68// glob async
69utils.glob('*.js', function (err, files) {
70 console.log(files);
71});
72```
73
74**Use as a function**
75
76```js
77var utils = require('lazy-cache')(require);
78var glob = utils('glob');
79
80// `glob` is a now a function that may be called when needed
81glob().sync('foo/*.js');
82```
83
84## Aliases
85
86An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
87
88**Example**
89
90```js
91var utils = require('lazy-cache')(require);
92
93// alias `ansi-yellow` as `yellow`
94utils('ansi-yellow', 'yellow');
95console.log(utils.yellow('foo'));
96```
97
98Dot notation may also be used in the alias to create an object hierarchy.
99
100**Example**
101
102```js
103var utils = require('lazy-cache')(require);
104utils('ansi-cyan', 'color.cyan');
105utils('ansi-yellow', 'color.yellow');
106utils('ansi-magenta', 'color.magenta');
107console.log(utils.color.cyan('foo'));
108console.log(utils.color.yellow('bar'));
109console.log(utils.color.magenta('baz'));
110```
111
112## Browserify usage
113
114**Example**
115
116```js
117var utils = require('lazy-cache')(require);
118// temporarily re-assign `require` to trick browserify
119var fn = require;
120require = utils;
121// list module dependencies (here, `require` is actually `lazy-cache`)
122require('glob');
123require = fn; // restore the native `require` function
124
125/**
126 * Now you can use glob with the `utils.glob` variable
127 */
128
129// sync
130console.log(utils.glob.sync('*.js'));
131
132// async
133utils.glob('*.js', function (err, files) {
134 console.log(files.join('\n'));
135});
136```
137
138## Kill switch
139
140To force lazy-cache to immediately invoke all dependencies, do:
141
142```js
143process.env.UNLAZY = true;
144```
145
146## About
147
148### Related projects
149
150[lint-deps](https://www.npmjs.com/package/lint-deps): CLI tool that tells you when dependencies are missing from package.json and offers you a… [more](https://github.com/jonschlinkert/lint-deps) | [homepage](https://github.com/jonschlinkert/lint-deps "CLI tool that tells you when dependencies are missing from package.json and offers you a choice to install them. Also tells you when dependencies are listed in package.json but are not being used anywhere in your project. Node.js command line tool and API")
151
152### Contributing
153
154Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
155
156### Contributors
157
158| **Commits** | **Contributor**<br/> |
159| --- | --- |
160| 31 | [jonschlinkert](https://github.com/jonschlinkert) |
161| 27 | [doowb](https://github.com/doowb) |
162
163### Building docs
164
165_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
166
167To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
168
169```sh
170$ npm install -g verb verb-generate-readme && verb
171```
172
173### Running tests
174
175Install dev dependencies:
176
177```sh
178$ npm install -d && npm test
179```
180
181### Author
182
183**Jon Schlinkert**
184
185* [github/jonschlinkert](https://github.com/jonschlinkert)
186* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
187
188### License
189
190Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
191Released under the [MIT license](https://github.com/jonschlinkert/lazy-cache/blob/master/LICENSE).
192
193***
194
195_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on November 07, 2016._
\No newline at end of file