GithubHelp home page GithubHelp logo

node-shortify's Introduction

shortify

browserify transform which can shorten require paths with defined aliases.

Installation

Install with npm(1):

$ npm install shortify

Preview

Call shortify with your alias hash and pass it to the transform method of your browserify instance.

var shortify = require('shortify');

var builder = browserify({ entries: ['app.js'] });
var transform = shortify({ foo: '../../foo' });

builder.transform(transform).bundle().pipe(yourwritestream);

Then require your modules will be rewritten from:

var bar = require('foo/bar');
var baz = require('foo/../baz');

to:

var bar = require('../../foo/bar');
var baz = require('../../foo/../baz');

// your module.exports source code

You can do the same with import in ES6:

import bar from "foo/bar";
import baz from "foo/../baz";

// compiles to:
import bar from "../../foo/bar";
import baz from "../../foo/../baz";

Main motivation behind this is that you can keep the > 80 character per line limit when requiring templates, configuration, ... files shared between your server and client environment.

Testing

Run npm test to build the file and and run the Mocha tests defined in opt/test/index.js.

Credits

Thank you to anubhava and T. J. Crowder on stackoverflow for helping me out with the regex. (Still seems mystical to me ^^).

License

Copyright © 2013 Bodo Kaiser [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-shortify's People

Contributors

alex-pex avatar bodokaiser avatar gpaton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

alex-pex

node-shortify's Issues

Unexpected alias for files

With this configuration:

shortify({
    'myModule': 'modules'
})

I'm using this require:
require 'myModule_view'

Shortify transforms this code into require 'modules/_view', which is not the expected result (it should be require 'myModule_view' directly)

Alias hash can't match folder name

Hi

I would like to alias specific folders like view, ui, vendor, so i don't have to type the full path everytime.
I tried the following:

var myTransform = shortify({ vendor: './lib/vendor/' });

then i'll require it:
var vendorModule = require("vendor/module");

It throws an error
Cannot find module './lib/./lib/vendor///module' from 'path to project\src\js'

If i change to:

var myTransform = shortify({ myvendor: './lib/vendor/' });

then i'll require it:
var vendorModule = require("myvendor/module");

It works

Otherwise nice work, browserify really needs something like this.
/Dennis

Configuration inside package.json

Is it possible to define configuration inside package.json?

Something like:

{
  "browserify": {
    "transform": ["shortify"]
  },
  "shortify": {
    "app/core": "./lib/core"
  }
}

npm version > 2.11.*

Hello,

This packages requires npm in its 2.11.* version, while the latest version is today 2.12.7.

[email protected]: wanted: {"node":"0.12.x","npm":"2.11.x"} (current: {"node":"0.12.7","npm":"2.12.1"})

Is it compatible ? If so, would you please upgrade requirement in package.json ?

Usage with gulp-browserify

This may be an issue for gulp-browserify and not shortify, but I thought I'd see if you had any thoughts.

I've been looking for something like this all day, but I'm not having any luck so far. This transform looks promising though!

Here's what my scripts gulp task looks like right now:

var browserify = require('gulp-browserify');
var concat     = require('gulp-concat');
var gulp       = require('gulp');
var livereload = require('gulp-livereload');
var server     = require('../servers/livereload');

var shortify = require('shortify')({
    tests: '../../tests' // <-- relative to this scripts.js gulp tasks file
});

module.exports = function() {
    gulp.src('src/scripts/**/app.coffee', { read: false })
        .pipe(
            browserify({
                transform: [shortify, 'coffeeify', 'hbsfy'],
                insertGlobals: true,
                debug: true,
                extensions: ['.coffee']
            }))
        .pipe(concat('app.js'))
        .pipe(gulp.dest('./build'))
        .pipe(livereload(server));
};

Then in another file, I'd like to include a file within the tests directory like this:

var response = require('tests/data/response.json');

Instead of require('.../.../.../tests/data/response.json'). Basically I want to set up aliases, so I don't have to use relative paths everywhere. Currently, I'm getting the following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: module "tests/data/response.json" not found from "/Users/greypants/Code/vitae-filepicker/src/scripts/views/layout.coffee"
    at /Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/index.js:592:23
    at /Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/index.js:183:24
    at /Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:44:14
    at process (/Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:110:43)
    at /Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:119:21
    at load (/Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:54:43)
    at /Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:60:22
    at /Users/greypants/Code/vitae-filepicker/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/node_modules/resolve/lib/async.js:16:47
    at Object.oncomplete (fs.js:107:15)

Any ideas?

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.