Checkout changelog also
I was so tired to write a lot constiable definitions in my build scripts, so I have decided to write a tiny library for including them automatically. So it's basically the library for those purposes, but you can use it everywhere.
- Requires and caches modules
- Avoid including all packages from
node_modulesinstead include packages specified inpackages.jsononly globalyoption, so you can avoid using any var (like$in examples)
npm install auto-require
There are examples using gulp, but you can use this package with anything else.
// get all modules from 'node_modules/' only
const $ = require('auto-require')()
// Do you see any gulp, plumber, pug const definitions here?
$.gulp.task('default', () => {
$.gulp.src('src/*.plumber')
.pipe($.plumber())
.pipe($.pug())
.pipe($.gulp.dest('dest/'))
})const options = {
only: ['gulp', 'gulp-stylus', 'gulp-plumber']
}
const $ = require('auto-require')(options)
// $.gulp, $.stylus, $.plumber only avaliableNote that paths are absolute and begins from the root of your project (it's where the package.json is)
const options = {
search: ['src/my-folder/'],
only: ['customize']
}
const $ = require('auto-require')(options)
// $.customize only avaliableconst options = {
without: ['kaktuz', 'zepto']
}
const $ = require('auto-require')(options)
// $.kaktuz and $.zepto are not avaiable at allconst options = {
without: ['gulp', 'zepto']
only: ['gulp', 'zepto']
}
const $ = require('auto-require')(options)
// $.zepto and $.gulp avaliable onlyBy using it that way you can get all vars as you specified them, but be careful as it's global namespace.
const options = {
only: ['gulp', 'gulp-notify'],
globaly: true
}
require('auto-require')(options)
// gulp and notify avaliable globaly (only)
//All global imports are guarded and can't be overriden:
gulp = {}
// Error: gulp is already defined
}You can rename module before import.
const options = {
only: ['gulp', 'gulp-notify'],
as: {gulp: 'g', 'gulp-notify': 'gn'}
}
const $ = require('auto-require')(options)
// You can access gulp and gulp-notify as $.g and $.gnYou can import all functions of module into root object.
const options = {
only: ['request'],
toRoot: ['request']
}
const $ = require('auto-require')(options)
// $.get, $.post, $.head (all function from request). But not $.requestIt takes all modules in your node_modules folder by default using data from package.json and exports it as one module.
Quick example
expressvia$.expressorexpressifglobalyset totrue(and in next examples as well)browser-syncvia$.browserSyncgulpvia$.gulpgulp-inline-cssvia$.inlineCss
No, you'll not need to write full module name.
The first part of the module name will be cut in this case.
For example we have gulp-pug module. How we can access it?
Actually it's pretty straightforward - via $.pug.
As you can see the first part of the gulp-pug has been cut.
This tool works fine with gulp, grunt, broccoli.
gulpvia$.gulpgulp-plumbervia$.plumbergulp-inline-cssvia$.inlineCssgruntvia$.gruntgrunt-shellvia$.shellgrunt-conventional-changelogvia$.conventionalChangelogbroccoli-file-contents-to-jsonvia$.fileContentsToJsonexpressvia$.expressbrowser-syncvia$.browserSyncandyet-express-authvia$.andyetExpressAuth
npm install
npm test
Licensed under MIT.