load-on-demand
v8.0.6
Published
on demand modules loading
Downloads
18
Maintainers
Readme
load-on-demand
Install
$ npm install --save-dev load-on-demand
Description
What is this module for? If you used modules gulp-load-plugins or lazy-modules or if you are looking for solution for on demand modules loading or you just hate lots of "require" on the top of files then this module is what you need. Using this module, you can load on demand modules from package.json file or your project modules.
In other words:
- on demand modules
- no lots of "require" on the top of files
- no relative or absolute paths, all modules are available as "modules"
Usage
Imagine that you have the next files structure:
package.json
gulpfiles.js
node_modules/
on-demand-modules/
index.js
gulp/
configs/
webpack.js
gulp.js
tasks/
webpack.js
jshint.js
Here is content of package.json:
{
"dependencies": {
"gulp-jshint": "*",
"gulp-concat": "*",
"glob": "^6.0.3",
"lodash": "^3.10.1",
"require-dir": "1.0.0"
},
"_additionalDependencies": {
"path": true
}
}
Here is content of node_modules/on-demand-modules/index.js(in this file you configure on demand modules):
'use strict';
/*
var $ = {} will contain all on demand modules. you can use global object instead and all on demand modules will
be available as global object properties, but I think it is better to use local object for avoiding any conflicts.
*/
var $ = {},
projectPrefix = 'sp-', // this prefix will be added to your project modules, for example $.spWebpackConfig
loadOnDemand = require('load-on-demand'),
loadOnDemandSettingsList = {
modulesInJsonFiles: [{ // here is settings for modulesInJsonFiles(modules from package.json)
destination: $, // module which will contain all on demand modules
/*
there is also option modulesProperties. The default value is:
defaultModulesProperties = [
'dependencies',
'devDependencies',
'peerDependencies',
'_additionalDependencies'
]
using this option you can set json file properties names which contain modules lists
there is also option filePath. The default value is:
defaultFilePath = findup('package.json', {cwd: process.cwd()})
using this option you can set json file(e.g. package.json)
*/
formatting: { // options for formatting modules names
renaming: {
/*
renaming is running before replacing and other formatting options.
*/
'lodash': '_'
},
replacing: {
/*
replacing substrings in modules names(str.replace method is used). for example gulp-concat module
will be now available as $.concat
*/
'/^gulp(-|\.)/': ''
}
/*
there is also option camelize(true by default). if set to true, then for example module my-module
will be available as $.myModule, if set to false then $['my-module']
*/
}
}],
modulesInFilesNames: [{ // here is settings for modulesInFilesNames(your project modules)
destination: $,
filesPathsPattern: './gulp/configs/*', // glob pattern of your project modules files
formatting: {
prefix: projectPrefix, // prefix and postfix will be added to modules names
postfix: '-config'
}
}]
};
loadOnDemand(loadOnDemandSettingsList);
module.exports = $;
/*
so, after using loadOnDemand, $ object looks like:
$.jshint
$.concat
$.glob
$._
$.path
$.requireDir
$.spWebpackConfig
$.spGulpConfig
*/
Now you can require on-demand-modules in any file of your project. For example here is content of gulpfile.js:
'use strict';
var $ = require('on-demand-modules');
$.requireDir($.spGulpConfig.tasksFolder);
and here is content of gulp/configs/gulp.js:
'use strict';
var $ = require('on-demand-modules'),
config = {
tasksFolder: $.path.resolve(process.cwd(), './gulp/tasks')
};
module.exports = config;
as you can see the module version is already 5.0.0(or maybe even more). It is just because it is almost my first npm module and I was making lots of changes when I wrote it. Lots of times I had to republish it, but when I did unpublish, anyway it was not allowed to republish module with the same version... :)