shortify
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -0,15 +1,29 @@ | ||
'use strict'; | ||
var through = require('through'); | ||
const QUERY = [ | ||
'require[(]([\'"])([^\'"]*)', '([^\'"]*)([\'"])' | ||
]; | ||
var PAIRS = [ | ||
// `require(""` | ||
{ | ||
query: ['require[(]([\'"])([^\'"]*)', '([^\'"]*)([\'"])'], | ||
replace: ['require($1$2', '$3$4'] | ||
}, | ||
const REPLACE = [ | ||
'require($1$2', '$3$4' | ||
// ` from ""` | ||
{ | ||
query: ['[\\s]from[\\s]([\'"])([^\'"]*)', '([^\'"]*)([\'"])'], | ||
replace: [' from $1$2', '$3$4'] | ||
}, | ||
// `import "";` | ||
{ | ||
query: ['import[\\s]([\'"])([^\'"]*)', '([^\'"]*)([\'"])'], | ||
replace: ['import $1$2', '$3$4'] | ||
} | ||
]; | ||
module.exports = function(options) { | ||
module.exports = function (options) { | ||
options = options || {}; | ||
return function(filename) { | ||
return function (filename) { | ||
var source = ''; | ||
@@ -23,5 +37,13 @@ | ||
for (var key in options) { | ||
var regex = new RegExp(QUERY[0] + key + QUERY[1], 'g'); | ||
source = source.replace(regex, REPLACE[0] + options[key] + REPLACE[1]); | ||
if (!options.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
PAIRS.forEach(function (params) { | ||
var query = params.query; | ||
var replace = params.replace; | ||
var searchTerm = query[0] + key + query[1]; | ||
var replaceTerm = replace[0] + options[key] + replace[1]; | ||
var regex = new RegExp(searchTerm, 'g'); | ||
source = source.replace(regex, replaceTerm); | ||
}); | ||
} | ||
@@ -34,3 +56,3 @@ | ||
return through(read, end); | ||
} | ||
}; | ||
}; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'foo'; | ||
module.exports = { foo: 1 }; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'baz'; | ||
module.exports = { baz: 1 }; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'bar'; | ||
module.exports = { bar: 1 }; |
var chai = require('chai'); | ||
var path = require('path'); | ||
import { foo as fooImport } from 'one/foo'; | ||
import { bar as barImport } from 'two/../two/bar'; | ||
import { baz as bazImport } from '../test/three/baz'; | ||
import 'one/fooGlobal'; | ||
import 'two/../two/barGlobal'; | ||
import '../test/three/bazGlobal'; | ||
describe('shortify', function() { | ||
@@ -9,3 +17,3 @@ | ||
chai.expect(foo).to.equal('foo'); | ||
chai.expect(foo.foo).to.equal(1); | ||
}); | ||
@@ -16,3 +24,3 @@ | ||
chai.expect(bar).to.equal('bar'); | ||
chai.expect(bar.bar).to.equal(1); | ||
}); | ||
@@ -23,5 +31,66 @@ | ||
chai.expect(baz).to.equal('baz'); | ||
chai.expect(baz.baz).to.equal(1); | ||
}); | ||
it('should resolve module "vendor/module"', function() { | ||
var module = require('vendor/module'); | ||
chai.expect(module.vendorModule).to.equal(1); | ||
}); | ||
}); | ||
// ES6 import doesn't work the same way as require, | ||
// so we expect something slightly different. | ||
describe('shortify in ES6', function() { | ||
it('should resolve module "foo"', function() { | ||
chai.expect(fooImport).to.equal(1); | ||
}); | ||
it('should resolve module "bar"', function() { | ||
chai.expect(barImport).to.equal(1); | ||
}); | ||
it('should resolve module "baz"', function() { | ||
chai.expect(bazImport).to.equal(1); | ||
}); | ||
it('should resolve module "fooGlobal"', function() { | ||
if (typeof(window) != "undefined") { | ||
chai.expect(window.foo).to.equal(1); | ||
} | ||
else if (typeof(process) != "undefined") { | ||
chai.expect(process.foo).to.equal(1); | ||
} | ||
else { | ||
chat.fail(); | ||
} | ||
}); | ||
it('should resolve module "barGlobal"', function() { | ||
if (typeof(window) != "undefined") { | ||
chai.expect(window.bar).to.equal(1); | ||
} | ||
else if (typeof(process) != "undefined") { | ||
chai.expect(process.bar).to.equal(1); | ||
} | ||
else { | ||
chat.fail(); | ||
} | ||
}); | ||
it('should resolve module "bazGlobal"', function() { | ||
if (typeof(window) != "undefined") { | ||
chai.expect(window.baz).to.equal(1); | ||
} | ||
else if (typeof(process) != "undefined") { | ||
chai.expect(process.baz).to.equal(1); | ||
} | ||
else { | ||
chat.fail(); | ||
} | ||
}); | ||
}); |
{ | ||
"name": "shortify", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": "Bodo Kaiser <i@bodokaiser.io>", | ||
"license": "BSD", | ||
"description": "rewrites require paths with defined aliases", | ||
"keywords": [ | ||
@@ -18,34 +14,26 @@ "alias", | ||
], | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"engines": { | ||
"node": "0.10.x", | ||
"npm": "1.3.x" | ||
"node": "0.12.x", | ||
"npm": "2.11.x" | ||
}, | ||
"main": "lib", | ||
"dependencies": { | ||
"through": "2.3.4" | ||
}, | ||
"devDependencies": { | ||
"chai": "1.9.1", | ||
"mocha": "1.18.2", | ||
"phantomjs": "1.9.7-1", | ||
"browserify": "3.33.1", | ||
"mocha-phantomjs": "3.3.2" | ||
"babelify": "^6.1.2", | ||
"browserify": "^10.2.4", | ||
"chai": "^3.0.0", | ||
"lodash": "^3.10.0", | ||
"mocha": "^2.2.5" | ||
}, | ||
"repository": { | ||
"url": "git://github.com/bodokaiser/node-shortify" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/bodokaiser/node-shortify/issues" | ||
} | ||
} |
@@ -14,3 +14,3 @@ # shortify | ||
Call shortify with your alias hash and pass it to the `transform` method of | ||
Call shortify with your alias hash and pass it to the `transform` method of | ||
your browserify instance. | ||
@@ -33,3 +33,2 @@ | ||
// your module.exports source code | ||
``` | ||
@@ -46,2 +45,13 @@ | ||
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 | ||
@@ -51,2 +61,7 @@ line limit when requiring templates, configuration, ... files shared | ||
## Testing | ||
Run `npm test` to build the file and and run the Mocha tests defined in | ||
`opt/test/index.js`. | ||
## Credits | ||
@@ -53,0 +68,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
8531
14
136
93