├── .editorconfig
├── .eslintrc.json
├── .gitattributes
├── .gitignore
├── .travis.yml
├── .verb.md
├── LICENSE
├── README.md
├── example-utils.js
├── example.js
├── index.js
├── package.json
└── test
├── fixtures
└── utils.js
└── test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | end_of_line = lf
6 | charset = utf-8
7 | indent_size = 2
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | [{**/{actual,fixtures,expected,templates}/**,*.md}]
12 | trim_trailing_whitespace = false
13 | insert_final_newline = false
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "ecmaFeatures": {
3 | "modules": true,
4 | "experimentalObjectRestSpread": true
5 | },
6 |
7 | "env": {
8 | "browser": false,
9 | "es6": true,
10 | "node": true,
11 | "mocha": true
12 | },
13 |
14 | "globals": {
15 | "document": false,
16 | "navigator": false,
17 | "window": false
18 | },
19 |
20 | "rules": {
21 | "accessor-pairs": 2,
22 | "arrow-spacing": [2, { "before": true, "after": true }],
23 | "block-spacing": [2, "always"],
24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }],
25 | "comma-dangle": [2, "never"],
26 | "comma-spacing": [2, { "before": false, "after": true }],
27 | "comma-style": [2, "last"],
28 | "constructor-super": 2,
29 | "curly": [2, "multi-line"],
30 | "dot-location": [2, "property"],
31 | "eol-last": 2,
32 | "eqeqeq": [2, "allow-null"],
33 | "generator-star-spacing": [2, { "before": true, "after": true }],
34 | "handle-callback-err": [2, "^(err|error)$" ],
35 | "indent": [2, 2, { "SwitchCase": 1 }],
36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
37 | "keyword-spacing": [2, { "before": true, "after": true }],
38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
39 | "new-parens": 2,
40 | "no-array-constructor": 2,
41 | "no-caller": 2,
42 | "no-class-assign": 2,
43 | "no-cond-assign": 2,
44 | "no-const-assign": 2,
45 | "no-control-regex": 2,
46 | "no-debugger": 2,
47 | "no-delete-var": 2,
48 | "no-dupe-args": 2,
49 | "no-dupe-class-members": 2,
50 | "no-dupe-keys": 2,
51 | "no-duplicate-case": 2,
52 | "no-empty-character-class": 2,
53 | "no-eval": 2,
54 | "no-ex-assign": 2,
55 | "no-extend-native": 2,
56 | "no-extra-bind": 2,
57 | "no-extra-boolean-cast": 2,
58 | "no-extra-parens": [2, "functions"],
59 | "no-fallthrough": 2,
60 | "no-floating-decimal": 2,
61 | "no-func-assign": 2,
62 | "no-implied-eval": 2,
63 | "no-inner-declarations": [2, "functions"],
64 | "no-invalid-regexp": 2,
65 | "no-irregular-whitespace": 2,
66 | "no-iterator": 2,
67 | "no-label-var": 2,
68 | "no-labels": 2,
69 | "no-lone-blocks": 2,
70 | "no-mixed-spaces-and-tabs": 2,
71 | "no-multi-spaces": 2,
72 | "no-multi-str": 2,
73 | "no-multiple-empty-lines": [2, { "max": 1 }],
74 | "no-native-reassign": 0,
75 | "no-negated-in-lhs": 2,
76 | "no-new": 2,
77 | "no-new-func": 2,
78 | "no-new-object": 2,
79 | "no-new-require": 2,
80 | "no-new-wrappers": 2,
81 | "no-obj-calls": 2,
82 | "no-octal": 2,
83 | "no-octal-escape": 2,
84 | "no-proto": 0,
85 | "no-redeclare": 2,
86 | "no-regex-spaces": 2,
87 | "no-return-assign": 2,
88 | "no-self-compare": 2,
89 | "no-sequences": 2,
90 | "no-shadow-restricted-names": 2,
91 | "no-spaced-func": 2,
92 | "no-sparse-arrays": 2,
93 | "no-this-before-super": 2,
94 | "no-throw-literal": 2,
95 | "no-trailing-spaces": 0,
96 | "no-undef": 2,
97 | "no-undef-init": 2,
98 | "no-unexpected-multiline": 2,
99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }],
100 | "no-unreachable": 2,
101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }],
102 | "no-useless-call": 0,
103 | "no-with": 2,
104 | "one-var": [0, { "initialized": "never" }],
105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
106 | "padded-blocks": [0, "never"],
107 | "quotes": [2, "single", "avoid-escape"],
108 | "radix": 2,
109 | "semi": [2, "always"],
110 | "semi-spacing": [2, { "before": false, "after": true }],
111 | "space-before-blocks": [2, "always"],
112 | "space-before-function-paren": [2, "never"],
113 | "space-in-parens": [2, "never"],
114 | "space-infix-ops": 2,
115 | "space-unary-ops": [2, { "words": true, "nonwords": false }],
116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
117 | "use-isnan": 2,
118 | "valid-typeof": 2,
119 | "wrap-iife": [2, "any"],
120 | "yoda": [2, "never"]
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Enforce Unix newlines
2 | * text eol=lf
3 |
4 | # binaries
5 | *.ai binary
6 | *.psd binary
7 | *.jpg binary
8 | *.gif binary
9 | *.png binary
10 | *.jpeg binary
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # always ignore files
2 | *.DS_Store
3 | *.sublime-*
4 |
5 | # test related, or directories generated by tests
6 | test/actual
7 | actual
8 | coverage
9 |
10 | # npm
11 | node_modules
12 | npm-debug.log
13 |
14 | # misc
15 | _gh_pages
16 | bower_components
17 | vendor
18 | temp
19 | tmp
20 | TODO.md
21 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | os:
3 | - linux
4 | - osx
5 | language: node_js
6 | node_js:
7 | - node
8 | - '6'
9 | - '4'
10 | - '0.12'
11 | - '0.10'
12 |
--------------------------------------------------------------------------------
/.verb.md:
--------------------------------------------------------------------------------
1 | ## Heads up!
2 |
3 | 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](https://github.com/felixge/node-dateformat/issues/36)) be used by 99.9% of users.
4 |
5 | Worse, 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:
6 |
7 | ```js
8 | // in the main export of a library, if you do this it will
9 | // automatically modify the String.prototype _globally_,
10 | // the moment node.js loads the dependency tree
11 | String.prototype.foo = function() {};
12 |
13 | // same if you do something like this
14 | // (dont' do this, ever. wrap this kind of code in a function
15 | // and allow implementors to decide when to call it)
16 | while (foo) {
17 | // do stuff
18 | }
19 | ```
20 |
21 | 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_.
22 |
23 | **solution**
24 |
25 | lazy-cache doesn't use any "magic", it uses native, plain-vanilla, tried and true javascript getters to call node's `require()` system.
26 |
27 | **Faster, safer code**
28 |
29 | There many 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).
30 |
31 | Moreover, in some cases this also avoids inadvertently loading libraries that execute code or modifies globals, etc.
32 |
33 | **webpack users**
34 |
35 | 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](https://github.com/doowb/unlazy-loader), a webpack loader that _fixes the webpack bug_.
36 |
37 | ## Usage
38 |
39 | ```js
40 | var utils = require('{%= name %}')(require);
41 | ```
42 |
43 | **Use as a property on `lazy`**
44 |
45 | The module is also added as a property to the `lazy` function so it can be called without having to call a function first.
46 |
47 | ```js
48 | var utils = require('{%= name %}')(require);
49 |
50 | // `npm install glob`
51 | utils('glob');
52 |
53 | // glob sync
54 | console.log(utils.glob.sync('*.js'));
55 |
56 | // glob async
57 | utils.glob('*.js', function (err, files) {
58 | console.log(files);
59 | });
60 | ```
61 |
62 | **Use as a function**
63 |
64 | ```js
65 | var utils = require('{%= name %}')(require);
66 | var glob = utils('glob');
67 |
68 | // `glob` is a now a function that may be called when needed
69 | glob().sync('foo/*.js');
70 | ```
71 |
72 | ## Aliases
73 |
74 | An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
75 |
76 | **Example**
77 |
78 | ```js
79 | var utils = require('{%= name %}')(require);
80 |
81 | // alias `ansi-yellow` as `yellow`
82 | utils('ansi-yellow', 'yellow');
83 | console.log(utils.yellow('foo'));
84 | ```
85 |
86 | Dot notation may also be used in the alias to create an object hierarchy.
87 |
88 | **Example**
89 |
90 | ```js
91 | var utils = require('{%= name %}')(require);
92 | utils('ansi-cyan', 'color.cyan');
93 | utils('ansi-yellow', 'color.yellow');
94 | utils('ansi-magenta', 'color.magenta');
95 | console.log(utils.color.cyan('foo'));
96 | console.log(utils.color.yellow('bar'));
97 | console.log(utils.color.magenta('baz'));
98 | ```
99 |
100 | ## Browserify usage
101 |
102 | **Example**
103 |
104 | ```js
105 | var utils = require('{%= name %}')(require);
106 | // temporarily re-assign `require` to trick browserify
107 | var fn = require;
108 | require = utils;
109 | // list module dependencies (here, `require` is actually `lazy-cache`)
110 | require('glob');
111 | require = fn; // restore the native `require` function
112 |
113 | /**
114 | * Now you can use glob with the `utils.glob` variable
115 | */
116 |
117 | // sync
118 | console.log(utils.glob.sync('*.js'));
119 |
120 | // async
121 | utils.glob('*.js', function (err, files) {
122 | console.log(files.join('\n'));
123 | });
124 | ```
125 |
126 | ## Kill switch
127 |
128 | To force lazy-cache to immediately invoke all dependencies, do:
129 |
130 | ```js
131 | process.env.UNLAZY = true;
132 | ```
133 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015-2016, Jon Schlinkert.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # lazy-cache [](https://www.npmjs.com/package/lazy-cache) [](https://npmjs.org/package/lazy-cache) [](https://npmjs.org/package/lazy-cache) [](https://travis-ci.org/jonschlinkert/lazy-cache)
2 |
3 | > Cache requires to be lazy-loaded when needed.
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | ```sh
10 | $ npm install --save lazy-cache
11 | ```
12 |
13 | ## Heads up!
14 |
15 | 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](https://github.com/felixge/node-dateformat/issues/36)) be used by 99.9% of users.
16 |
17 | Worse, 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
23 | String.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)
28 | while (foo) {
29 | // do stuff
30 | }
31 | ```
32 |
33 | 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_.
34 |
35 | **solution**
36 |
37 | lazy-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 |
41 | There many 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 |
43 | Moreover, in some cases this also avoids inadvertently loading libraries that execute code or modifies globals, etc.
44 |
45 | **webpack users**
46 |
47 | 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](https://github.com/doowb/unlazy-loader), a webpack loader that _fixes the webpack bug_.
48 |
49 | ## Usage
50 |
51 | ```js
52 | var utils = require('lazy-cache')(require);
53 | ```
54 |
55 | **Use as a property on `lazy`**
56 |
57 | The 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
60 | var utils = require('lazy-cache')(require);
61 |
62 | // `npm install glob`
63 | utils('glob');
64 |
65 | // glob sync
66 | console.log(utils.glob.sync('*.js'));
67 |
68 | // glob async
69 | utils.glob('*.js', function (err, files) {
70 | console.log(files);
71 | });
72 | ```
73 |
74 | **Use as a function**
75 |
76 | ```js
77 | var utils = require('lazy-cache')(require);
78 | var glob = utils('glob');
79 |
80 | // `glob` is a now a function that may be called when needed
81 | glob().sync('foo/*.js');
82 | ```
83 |
84 | ## Aliases
85 |
86 | An 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
91 | var utils = require('lazy-cache')(require);
92 |
93 | // alias `ansi-yellow` as `yellow`
94 | utils('ansi-yellow', 'yellow');
95 | console.log(utils.yellow('foo'));
96 | ```
97 |
98 | Dot notation may also be used in the alias to create an object hierarchy.
99 |
100 | **Example**
101 |
102 | ```js
103 | var utils = require('lazy-cache')(require);
104 | utils('ansi-cyan', 'color.cyan');
105 | utils('ansi-yellow', 'color.yellow');
106 | utils('ansi-magenta', 'color.magenta');
107 | console.log(utils.color.cyan('foo'));
108 | console.log(utils.color.yellow('bar'));
109 | console.log(utils.color.magenta('baz'));
110 | ```
111 |
112 | ## Browserify usage
113 |
114 | **Example**
115 |
116 | ```js
117 | var utils = require('lazy-cache')(require);
118 | // temporarily re-assign `require` to trick browserify
119 | var fn = require;
120 | require = utils;
121 | // list module dependencies (here, `require` is actually `lazy-cache`)
122 | require('glob');
123 | require = fn; // restore the native `require` function
124 |
125 | /**
126 | * Now you can use glob with the `utils.glob` variable
127 | */
128 |
129 | // sync
130 | console.log(utils.glob.sync('*.js'));
131 |
132 | // async
133 | utils.glob('*.js', function (err, files) {
134 | console.log(files.join('\n'));
135 | });
136 | ```
137 |
138 | ## Kill switch
139 |
140 | To force lazy-cache to immediately invoke all dependencies, do:
141 |
142 | ```js
143 | process.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 |
154 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
155 |
156 | ### Contributors
157 |
158 | | **Commits** | **Contributor**
|
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 |
167 | To 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 |
175 | Install 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 |
190 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
191 | Released 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._
--------------------------------------------------------------------------------
/example-utils.js:
--------------------------------------------------------------------------------
1 | /**
2 | * The easiest way to add this file is to do:
3 | *
4 | * ```sh
5 | * $ npm i -g lazy-cache-cli && lazy
6 | * ```
7 | * To do it manually:
8 | *
9 | * 1. Add a file named `utils.js` to your project.
10 | * 2. Copy the contents of this file (below this comment)
11 | * into `utils.js`
12 | * 3. Change the dependencies where it says
13 | * "your dependencies here" your project's actual dependencies
14 | */
15 | 'use strict';
16 |
17 | /**
18 | * Module dependencies
19 | */
20 |
21 | var utils = require('lazy-cache')(require);
22 |
23 | /**
24 | * Temporarily re-assign `require` to trick browserify and
25 | * webpack into reconizing lazy dependencies.
26 | *
27 | * This tiny bit of ugliness has the huge dual advantage of
28 | * only loading modules that are actually called at some
29 | * point in the lifecycle of the application, whilst also
30 | * allowing browserify and webpack to find modules that
31 | * are depended on but never actually called.
32 | */
33 |
34 | var fn = require;
35 | require = utils;
36 |
37 | /**
38 | * Lazily required module dependencies
39 | */
40 |
41 | // __(your dependencies here)__
42 | require('ansi-yellow')
43 |
44 | /**
45 | * reset `require`
46 | */
47 |
48 | require = fn;
49 |
50 | /**
51 | * Expose `utils` modules
52 | */
53 |
54 | module.exports = utils;
55 |
--------------------------------------------------------------------------------
/example.js:
--------------------------------------------------------------------------------
1 | var utils = require('./')(require);
2 | utils('glob');
3 |
4 | /**
5 | * `utils.glob` is a getter that when called
6 | * will require in `glob`
7 | */
8 |
9 | console.log(utils.glob.sync('*.js'));
10 | utils.glob('*.js', function(err, files) {
11 | console.log(files.join('\n'));
12 | });
13 |
14 | /**
15 | * Browserify-friendly example
16 | */
17 |
18 | var utils = require('./')(require);
19 | var fn = require;
20 | require = utils;
21 | require('ansi-yellow', 'yellow');
22 | require('glob');
23 | require = fn;
24 |
25 | console.log(utils.glob.sync('*.js'));
26 | utils.glob('*.js', function(err, files) {
27 | console.log(files.join('\n'));
28 | });
29 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var set = require('set-getter');
4 |
5 | /**
6 | * Cache results of the first function call to ensure only calling once.
7 | *
8 | * ```js
9 | * var utils = require('lazy-cache')(require);
10 | * // cache the call to `require('ansi-yellow')`
11 | * utils('ansi-yellow', 'yellow');
12 | * // use `ansi-yellow`
13 | * console.log(utils.yellow('this is yellow'));
14 | * ```
15 | *
16 | * @param {Function} `fn` Function that will be called only once.
17 | * @return {Function} Function that can be called to get the cached function
18 | * @api public
19 | */
20 |
21 | function lazyCache(requireFn) {
22 | var cache = {};
23 |
24 | return function proxy(name, alias) {
25 | var key = alias;
26 |
27 | // camel-case the module `name` if `alias` is not defined
28 | if (typeof key !== 'string') {
29 | key = camelcase(name);
30 | }
31 |
32 | // create a getter to lazily invoke the module the first time it's called
33 | function getter() {
34 | return cache[key] || (cache[key] = requireFn(name));
35 | }
36 |
37 | // trip the getter if `process.env.UNLAZY` is defined
38 | if (unlazy(process.env)) {
39 | getter();
40 | }
41 |
42 | set(proxy, key, getter);
43 | return getter;
44 | };
45 | }
46 |
47 | /**
48 | * Return true if `process.env.LAZY` is true, or travis is running.
49 | */
50 |
51 | function unlazy(env) {
52 | return env.UNLAZY === 'true' || env.UNLAZY === true || env.TRAVIS;
53 | }
54 |
55 | /**
56 | * Camelcase the the given module `name`.
57 | */
58 |
59 | function camelcase(str) {
60 | if (str.length === 1) {
61 | return str.toLowerCase();
62 | }
63 | str = str.replace(/^[\W_]+|[\W_]+$/g, '').toLowerCase();
64 | return str.replace(/[\W_]+(\w|$)/g, function(_, ch) {
65 | return ch.toUpperCase();
66 | });
67 | }
68 |
69 | /**
70 | * Expose `lazyCache`
71 | */
72 |
73 | module.exports = lazyCache;
74 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lazy-cache",
3 | "description": "Cache requires to be lazy-loaded when needed.",
4 | "version": "2.0.2",
5 | "homepage": "https://github.com/jonschlinkert/lazy-cache",
6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
7 | "contributors": [
8 | "Brian Woodward (https://github.com/doowb)",
9 | "Jon Schlinkert (http://twitter.com/jonschlinkert)"
10 | ],
11 | "repository": "jonschlinkert/lazy-cache",
12 | "bugs": {
13 | "url": "https://github.com/jonschlinkert/lazy-cache/issues"
14 | },
15 | "license": "MIT",
16 | "files": [
17 | "index.js"
18 | ],
19 | "main": "index.js",
20 | "engines": {
21 | "node": ">=0.10.0"
22 | },
23 | "scripts": {
24 | "test": "mocha"
25 | },
26 | "dependencies": {
27 | "set-getter": "^0.1.0"
28 | },
29 | "devDependencies": {
30 | "ansi-cyan": "^0.1.1",
31 | "ansi-magenta": "^0.1.1",
32 | "ansi-yellow": "^0.1.1",
33 | "glob": "^7.0.3",
34 | "gulp-format-md": "^0.1.8",
35 | "mocha": "^2.4.5",
36 | "object.omit": "^2.0.0",
37 | "object.pick": "^1.1.2"
38 | },
39 | "keywords": [
40 | "cache",
41 | "caching",
42 | "dependencies",
43 | "dependency",
44 | "lazy",
45 | "require",
46 | "requires"
47 | ],
48 | "verb": {
49 | "related": {
50 | "list": [
51 | "lint-deps"
52 | ]
53 | },
54 | "plugins": [
55 | "gulp-format-md"
56 | ],
57 | "toc": false,
58 | "layout": "default",
59 | "tasks": [
60 | "readme"
61 | ],
62 | "lint": {
63 | "reflinks": true
64 | },
65 | "reflinks": [
66 | "verb"
67 | ]
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/test/fixtures/utils.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | /**
4 | * Module dependencies
5 | */
6 |
7 | var utils = require('../../')(require);
8 |
9 | /**
10 | * Temporarily re-assign `require` to trick browserify and
11 | * webpack into reconizing lazy dependencies.
12 | *
13 | * This tiny bit of ugliness has the huge dual advantage of
14 | * only loading modules that are actually called at some
15 | * point in the lifecycle of the application, whilst also
16 | * allowing browserify and webpack to find modules that
17 | * are depended on but never actually called.
18 | */
19 |
20 | var fn = require;
21 | require = utils;
22 |
23 | /**
24 | * Lazily required module dependencies
25 | */
26 |
27 | // __(your dependencies here)__
28 | require('ansi-yellow')
29 |
30 | /**
31 | * reset `require`
32 | */
33 |
34 | require = fn;
35 |
36 | /**
37 | * Expose `utils` modules
38 | */
39 |
40 | module.exports = utils;
41 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | var assert = require('assert');
5 | var lazyCache = require('../');
6 |
7 | describe('lazy-cache', function() {
8 | afterEach(function() {
9 | delete require.cache[require.resolve('../index.js')];
10 | process.env.UNLAZY = false;
11 | lazyCache = require('../');
12 | });
13 |
14 | it('should return a function that lazyily requires a module', function() {
15 | var lazy = lazyCache(require);
16 | var yellow = lazy('ansi-yellow');
17 | assert.deepEqual(typeof yellow, 'function');
18 | assert.deepEqual(typeof yellow(), 'function');
19 | });
20 |
21 | it('should not invoke require until the module is used', function() {
22 | var calls = {};
23 | var lazy = lazyCache(function(name) {
24 | calls[name] = (calls[name] || 0) + 1;
25 | return require(name);
26 | });
27 |
28 | assert.deepEqual(calls, {});
29 |
30 | var yellow = lazy('ansi-yellow');
31 |
32 | if (process.env.TRAVIS) {
33 | assert.deepEqual(calls, {'ansi-yellow': 1});
34 | } else {
35 | assert.deepEqual(calls, {});
36 | }
37 |
38 | yellow('one');
39 | assert.deepEqual(calls, {'ansi-yellow': 1});
40 | yellow('two');
41 | assert.deepEqual(calls, {'ansi-yellow': 1});
42 | yellow('three');
43 | assert.deepEqual(calls, {'ansi-yellow': 1});
44 | yellow('four');
45 | assert.deepEqual(calls, {'ansi-yellow': 1});
46 | });
47 |
48 | it('should add a property to the lazy function with the camelcased name', function() {
49 | var lazy = lazyCache(require);
50 | lazy('ansi-yellow');
51 | assert.deepEqual(typeof lazy.ansiYellow, 'function');
52 | });
53 |
54 | it('should only require a dependency once', function() {
55 | var calls = {};
56 | var lazy = lazyCache(function(name) {
57 | calls[name] = (calls[name] || 0) + 1;
58 | return require(name);
59 | });
60 |
61 | assert.deepEqual(calls, {});
62 | lazy('ansi-yellow');
63 | if (process.env.TRAVIS) {
64 | assert.deepEqual(calls, {'ansi-yellow': 1});
65 | } else {
66 | assert.deepEqual(calls, {});
67 | }
68 |
69 | lazy.ansiYellow('one');
70 | assert.deepEqual(calls, {'ansi-yellow': 1});
71 | lazy.ansiYellow('two');
72 | assert.deepEqual(calls, {'ansi-yellow': 1});
73 | lazy.ansiYellow('three');
74 | assert.deepEqual(calls, {'ansi-yellow': 1});
75 | lazy.ansiYellow('four');
76 | assert.deepEqual(calls, {'ansi-yellow': 1});
77 | });
78 |
79 | it('should allow loading a dependency immediately with `process.env.UNLAZY` setting', function() {
80 | process.env.UNLAZY = true;
81 | var calls = {};
82 | var lazy = lazyCache(function(name) {
83 | calls[name] = (calls[name] || 0) + 1;
84 | return require(name);
85 | });
86 |
87 | assert.deepEqual(calls, {});
88 |
89 | lazy('ansi-yellow');
90 | assert.deepEqual(calls, {'ansi-yellow': 1});
91 | lazy.ansiYellow('one');
92 | assert.deepEqual(calls, {'ansi-yellow': 1});
93 | lazy.ansiYellow('two');
94 | assert.deepEqual(calls, {'ansi-yellow': 1});
95 | lazy.ansiYellow('three');
96 | assert.deepEqual(calls, {'ansi-yellow': 1});
97 | lazy.ansiYellow('four');
98 | assert.deepEqual(calls, {'ansi-yellow': 1});
99 | });
100 |
101 | it('should support an alias as the second argument:', function() {
102 | var calls = {};
103 | var lazy = lazyCache(function(name) {
104 | calls[name] = (calls[name] || 0) + 1;
105 | return require(name);
106 | });
107 |
108 | assert.deepEqual(calls, {});
109 | lazy('ansi-yellow', 'yellow');
110 | if (process.env.TRAVIS) {
111 | assert.deepEqual(calls, {'ansi-yellow': 1});
112 | } else {
113 | assert.deepEqual(calls, {});
114 | }
115 |
116 | lazy.yellow('one');
117 | assert.deepEqual(calls, {'ansi-yellow': 1});
118 | lazy.yellow('two');
119 | assert.deepEqual(calls, {'ansi-yellow': 1});
120 | lazy.yellow('three');
121 | assert.deepEqual(calls, {'ansi-yellow': 1});
122 | lazy.yellow('four');
123 | assert.deepEqual(calls, {'ansi-yellow': 1});
124 | });
125 |
126 | it('should support dot-notation in aliases', function() {
127 | var lazy = lazyCache(require);
128 | lazy('ansi-cyan', 'color.cyan');
129 | lazy('ansi-yellow', 'color.yellow');
130 | lazy('ansi-magenta', 'color.magenta');
131 |
132 | assert.deepEqual(typeof lazy.color, 'object');
133 | assert.deepEqual(typeof lazy.color.cyan, 'function');
134 | assert.deepEqual(typeof lazy.color.yellow, 'function');
135 | assert.deepEqual(typeof lazy.color.magenta, 'function');
136 | });
137 |
138 | it('should not use dot-notation in non-aliases', function() {
139 | var lazy = lazyCache(require);
140 | lazy('object.omit');
141 | lazy('object.pick');
142 | assert.deepEqual(typeof lazy.objectOmit, 'function');
143 | assert.deepEqual(typeof lazy.objectPick, 'function');
144 | });
145 |
146 | it('should load local files', function() {
147 | var lazy = lazyCache(require);
148 | lazy('./fixtures/utils');
149 | assert.deepEqual(typeof lazy.fixturesUtils, 'function');
150 | assert.deepEqual(typeof lazy.fixturesUtils.ansiYellow, 'function');
151 | });
152 |
153 | it('should load local files with an alias', function() {
154 | var lazy = lazyCache(require);
155 | lazy('./fixtures/utils', 'utils');
156 | assert.deepEqual(typeof lazy.utils, 'function');
157 | assert.deepEqual(typeof lazy.utils.ansiYellow, 'function');
158 | });
159 |
160 | it('should support dot-notation when `process.env.UNLAZY` is used', function() {
161 | process.env.UNLAZY = true;
162 |
163 | var calls = {};
164 | var lazy = lazyCache(function(name) {
165 | calls[name] = (calls[name] || 0) + 1;
166 | return require(name);
167 | });
168 |
169 | assert.deepEqual(calls, {});
170 |
171 | lazy('ansi-cyan', 'color.cyan');
172 | lazy('ansi-yellow', 'color.yellow');
173 | lazy('ansi-magenta', 'color.magenta');
174 |
175 | assert.deepEqual(calls, {
176 | 'ansi-cyan': 1,
177 | 'ansi-yellow': 1,
178 | 'ansi-magenta': 1
179 | });
180 | lazy.color.cyan('one');
181 | lazy.color.yellow('one');
182 | lazy.color.magenta('one');
183 | assert.deepEqual(calls, {
184 | 'ansi-cyan': 1,
185 | 'ansi-yellow': 1,
186 | 'ansi-magenta': 1
187 | });
188 | lazy.color.cyan('two');
189 | lazy.color.yellow('two');
190 | lazy.color.magenta('two');
191 | assert.deepEqual(calls, {
192 | 'ansi-cyan': 1,
193 | 'ansi-yellow': 1,
194 | 'ansi-magenta': 1
195 | });
196 | lazy.color.cyan('three');
197 | lazy.color.yellow('three');
198 | lazy.color.magenta('three');
199 | assert.deepEqual(calls, {
200 | 'ansi-cyan': 1,
201 | 'ansi-yellow': 1,
202 | 'ansi-magenta': 1
203 | });
204 | lazy.color.cyan('four');
205 | lazy.color.yellow('four');
206 | lazy.color.magenta('four');
207 | assert.deepEqual(calls, {
208 | 'ansi-cyan': 1,
209 | 'ansi-yellow': 1,
210 | 'ansi-magenta': 1
211 | });
212 |
213 | assert.deepEqual(typeof lazy.color, 'object');
214 | assert.deepEqual(typeof lazy.color.cyan, 'function');
215 | assert.deepEqual(typeof lazy.color.yellow, 'function');
216 | assert.deepEqual(typeof lazy.color.magenta, 'function');
217 | });
218 | });
219 |
--------------------------------------------------------------------------------