Giter VIP home page Giter VIP logo

best-require's Introduction

Build Status NPM version

Best Require

Best Require is a require() hook plugin for requiring a module in your project elegantly for Node.js.

require(':controllers/posts');
require(':models/Users');
require('~/application/apis/config');

Installation

npm install best-require --save

Introduction

Background

Have you ever coded like this in your application?

// require the 'posts' module
require('./posts');
require('./controllers/posts');
require('../controllers/posts');
require('../../controllers/posts');
require('../../../apis/controllers/posts');

When our project contains many layers of directory, the relative path of each module will become complicated, which not only makes us very confused, but also makes the project difficult to maintain.

When faced with the problem, you might tend to find a unified way to access the posts module, just like me. I used to require modules in this way:

require(ROOT_PATH + '/application/apis/controllers/posts');
// other require()...
require(ROOT_PATH + '/application/apis/controllers/users');
require(ROOT_PATH + '/application/apis/controllers/products');
require(ROOT_PATH + '/application/apis/services/rest');
require(ROOT_PATH + '/application/apis/config');

ummmm... It's more maintainable than before. But, ROOT_PATH is ugly, isn't it?

Solution

Let's try to use Best Require, by adding this at the beginning of the app:

require('best-require')(process.cwd())

Now, we can use ~ to represent process.cwd() in the path:

require('~/application/apis/controllers/posts');
require('~/application/apis/controllers/users');
require('~/application/apis/controllers/products');
require('~/application/apis/services/rest');
require('~/application/apis/config');

However, this directory name is still a bit long, which can be shortened by defining the name mapping:

const ROOT_PATH = process.cwd();
require('best-require')(ROOT_PATH, {
    apis: ROOT_PATH + '/application/apis',
    controllers: ROOT_PATH + '/application/apis/controllers'
});

Then we are able to use :apis for ~/application/apis and :controllers for ~/application/apis/controllers:

require(':controllers/posts');
require(':controllers/users');
require(':controllers/products');
require(':apis/services/rest');
require(':apis/config');

With the release V1.1+, you can also use other keys in the definition of a key-value pair in the name mapping, and our plug-in will automatically handle the keys' dependencies on each other. Therefore, the definition can be simplified as:

require('best-require')(ROOT_PATH, {
    apis: '~/application/apis',
    controllers: ':apis/controllers'
});

Usage

Add this at the beginning of the program:

require('best-require')(
    root_path, // [optional] project root directory, defaults to `process.cwd()`
    name_mapping // [optional] name mapping
);

Then you can use:

require('~/(path)'); // require(root_path + '/(path)');
require(':key/(path)'); // require(name_mapping[key] + '/(path)');

best-require's People

Contributors

zhihanyue avatar

Stargazers

 avatar FungLeo avatar  avatar houfeng avatar  avatar  avatar Charbo avatar 偷偷不偷懒 avatar Ken avatar 侠小然 avatar  avatar  avatar 尚左 mingelz avatar /dev/fd/ran avatar 老王 Willin (v0) avatar Austin Lee avatar loufq avatar Jiajing LU avatar Fangzhou Li avatar Lan Qingyong avatar budlion avatar Steve avatar 悔惜晟 avatar George Cheng avatar Jkin8010 avatar Charles avatar fengmk2 avatar  avatar Three Many avatar  avatar liugaolian avatar CodeLife avatar bccat avatar  avatar Roman Cree avatar  avatar  avatar qingyuanbb avatar

Watchers

侠小然 avatar FungLeo avatar  avatar

Forkers

nine1314 boostbob

best-require's Issues

Error packaging 'pkg' after project uses 'best require'

My project uses 'best require', it's work and good.
my code like this:

const bestRequire = require('best-require')
// best-require config
bestRequire(process.cwd(), {
  '@': '~/app',
  config: ':@/config',
  utils: ':@/utils',
  core: ':@/core',
  query: ':core/query'
})

but, when i use 'pkg' to packaging my project, it's return Error.
like this:

$ pkg .
> [email protected]
> Warning Cannot find module ':config' from '/Users/fungleo/Sites/Github/restful-cms-koa'
  %1: /Users/fungleo/Sites/Github/restful-cms-koa/app.js
> Warning Cannot find module ':utils/tool' from '/Users/fungleo/Sites/Github/restful-cms-koa/app'
  %1: /Users/fungleo/Sites/Github/restful-cms-koa/app/index.js
> Warning Cannot find module ':@/middle/logger' from '/Users/fungleo/Sites/Github/restful-cms-koa/app'
  %1: /Users/fungleo/Sites/Github/restful-cms-koa/app/index.js
> Warning Cannot find module ':@/middle/error' from '/Users/fungleo/Sites/Github/restful-cms-koa/app'
  %1: /Users/fungleo/Sites/Github/restful-cms-koa/app/index.js
> Warning Cannot find module ':config/config-koa-body' from '/Users/fungleo/Sites/Github/restful-cms-koa/app'
  %1: /Users/fungleo/Sites/Github/restful-cms-koa/app/index.js
> Warning Cannot find module ':@/router' from '/Users/fungleo/Sites/Github/restful-cms-koa/app'
  %1: /Users/fungleo/Sites/Github/restful-cms-koa/app/index.js

How can I solve this problem?can you help me?

my package.json

{
  "name": "restful-cms-koa",
  "version": "1.0.0",
  "description": "A small restful CMS based on koa framework",
  "main": "app.js",
  "scripts": {
    "dev": "nodemon app.js",
    "start": "node app.js"
  },
  "keywords": [
    "restful",
    "cms",
    "koa"
  ],
  "author": "fungleo",
  "license": "MIT",
  "dependencies": {
    "best-require": "^1.1.4",
    "koa": "^2.11.0",
    "koa-body": "^4.1.1",
    "koa-json": "^2.0.2",
    "koa-log4": "^2.3.2",
    "koa-router": "^8.0.8",
    "koa-static": "^5.0.0",
    "node-rsa": "^1.0.8",
    "sequelize": "^5.21.6",
    "sqlite3": "^4.1.1"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
    "nodemon": "^2.0.3",
    "standard": "^14.3.3"
  },
  "pkg": {
    "output": "restful-cms-koa",
    "targets": [
      "node10-macos-x64"
    ]
  },
  "bin": "app.js"
}

my English is very poor, -_-|||

小小的建议

首先感谢分享 很好的模块.
其次希望增加一个特定字符的定义接口.比如vue中 是使用@符号来作为相对项目根目录的路径的.这里用的~.如果可以的话这个提供接口改下

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.