Coder Social home page Coder Social logo

node-packagerify's Introduction

node-packagerify

Make it possible to use 'Java-style' package import instead of using require. Add require('./packages.js') to your application root file and everywhere in the project you can use package name instead of require('./../../../someDir/someSubDir/module');

NO MORE: var module = require('./../../../someDir/someSubDir/module')

USE PACKAGERIFY: var module = your.package.name.somePackage.someSubPackage.module;

Packagerify generates packages.js in specified folder. You have to include it into your main application file. It will inject generated packages object into global namespace. Why to generate this file and not to use Proxy and so on? Just because I want my IDE make autocompletion when I type the package name;

IDE autocomplete

As additional benefit - you could enumerate modules in package without filesystem reading. Just use native JS.

for(var i in com.your.pack.module) {
    ...
}

(Especially useful for AngularJS when registering controllers,directives, etc); Note: for client-side usage RequireJS system is a must for now. ###Please, note: If you build your code with BabelJS v6, you must use babel-plugin-add-module-exports plugin. You can download it here https://www.npmjs.com/package/babel-plugin-add-module-exports

From 0.0.7 version the grunt task included. So, to packagerify your project simply add to Gruntfile.js packagerify config:

Example config

Gruntfile.js

grunt.initConfig({
    packagerify: {
        default: {
            options: {
                packageName: 'com.example.packagename',
                packagesFileDir: '',
                watchSourceDirs: ['src'],
                ignored: [**/assets]
            }
        }
    }
});

grunt.loadNpmTasks('packagerify');

grunt.registerTask('default', ['packagerify']);

  • packageName - dot-separated string (usually your domain in reversed order + package name). For example: com.google.myPackage;
  • packagesFileDir - [not required] path to folder where packages.js file will be created (By default - the Gruntfile.js folder);
  • watchSourceDirs - string or array of strings. Each string is a relative path to packagesFileDir. Each watched directory will populate its sources to packages.js
  • ignored - anymatch-compatible expression matching files or paths

Assuming we have project structure:

root_dir/src/lib/MyClass1.js
root_dir/src/lib/MyClass2.js

Grunt task setup:

packagerify: {
    default: {
        options: {
            packageName: 'com.example.pack',
            watchSourceDirs: ['src'],
        }
    }
}

packages.js injects in the global namespace this files and they could be accessed in next way:

var MyClass1 = com.example.pack.lib.MyClass1

var MyClass2 = com.example.pack.lib.MyClass2

For ES6:

class MyDescendantClass extends com.example.pack.lib.MyClass2 {
    ...
}

No 'require('.........')' needed!

First step

npm install packagerify

Second step

  • setup grunt config as showed earlier

    OR

  • use Packagerify class directly (this will also start file watcher):
var Packagerify = require('packagerify');
var packagerify = new Packagerify({
    packageName: 'com.example.packagename',
    packagesFileDir: '',
    watchSourceDirs: ['src']
});

##Whats new

v0.0.10:

  • remove packagerify module dependence from packages.js to avoid packing entire packagerify module with webpack and similar

v0.0.9:

  • add "ignored" option

v0.0.8:

  • add grunt task "packagerify"

node-packagerify's People

Contributors

statyan avatar

Stargazers

bluelovers avatar Sova avatar Stefan avatar

Watchers

 avatar

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.