Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aichholzer/attract
Requiring libraries with love. ❤️
https://github.com/aichholzer/attract
Last synced: 25 days ago
JSON representation
Requiring libraries with love. ❤️
- Host: GitHub
- URL: https://github.com/aichholzer/attract
- Owner: aichholzer
- License: mit
- Created: 2017-02-09T19:31:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-23T12:15:23.000Z (over 4 years ago)
- Last Synced: 2024-10-08T02:04:58.442Z (about 1 month ago)
- Language: JavaScript
- Size: 54.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# attract
[![Greenkeeper badge](https://badges.greenkeeper.io/aichholzer/attract.svg)](https://greenkeeper.io/)
[![Build Status](https://travis-ci.org/aichholzer/attract.svg?branch=master)](https://travis-ci.org/aichholzer/attract)
[![Downloads](https://img.shields.io/npm/dt/attract.svg)](https://www.npmjs.com/package/attract)
[![Always useful](https://img.shields.io/badge/always-useful-ff6400.svg)](https://github.com/aichholzer/attract)Requiring libraries with love. ❤
`attract` basically replaces `require()`, providing a more flexible way of requiring libraries and modules in your project.
(It still uses `require()` under the hood, which provides caching all the things you need)### Features
* Require `attract` only once, at your application's start, and it will be globally available, everywhere.
* Say goodbye to those ugly relative paths.### Install
```
npm i attract
```### Usage
##### Before:
```js
const fs = require('fs');
const express = require('express');
const mongoose = require('mongoose');
```##### After:
```js
require('attract')();
const [
fs,
express,
mongoose
] = attract('fs', 'express', 'mongoose');
````attract` is also registered as `required`, so you could use it like:
```js
const [
fs,
express,
mongoose
] = required('fs', 'express', 'mongoose');
```You can also get rid of all those horrible relative path requires throughout your application,
and require third-party modules easily with `attract`, just define your `basePath` at start and you are good to go.##### Before:
```js
const moduleOne = require('../../modules/path/to/moduleOne');
const moduleTwo = require('../../../modules/path/to/moduleTwo');
```##### After:
```js
// Set `basePath` to your project's root folder, for example.
require('attract')({ basePath: __dirname })
const [
moduleOne,
moduleTwo
] = attract('path/to/moduleOne', 'path/to/moduleTwo');
```### Contribute
```
fork https://github.com/aichholzer/attract/
```### License
[MIT](https://github.com/aichholzer/attract/blob/master/LICENSE)