Comparing version 0.0.3 to 0.1.0
@@ -0,1 +1,4 @@ | ||
## 0.1.0 | ||
- It exposes the compiler. | ||
## 0.0.3 | ||
@@ -2,0 +5,0 @@ - Removed pointless missing globals code, we can just use `global` variable as |
53
index.js
@@ -17,10 +17,20 @@ 'use strict'; | ||
*/ | ||
module.exports = function require(location) { | ||
function load(location) { | ||
if (!path.extname(location)) location = location +'.js'; | ||
location = path.resolve(path.dirname(module.parent.filename), location); | ||
var context = { load: require } | ||
, name = path.basename(location) | ||
, code = read(location); | ||
return compiler(read(location), path.basename(location)); | ||
} | ||
/** | ||
* The module compiler. | ||
* | ||
* @param {String} code The source code that needs to be compiled | ||
* @param {String} name The name of the file. | ||
* @returns {Mixed} Things. | ||
* @api public | ||
*/ | ||
function compiler(code, name) { | ||
var context = { load: require }; | ||
// Add the missing globals that are not present in vm module. | ||
@@ -53,21 +63,9 @@ Object.keys(missing).forEach(function missingInVM(global) { | ||
}, Object.create(null)); | ||
}; | ||
} | ||
/** | ||
* The following properties are missing when loading plain ol files. | ||
* | ||
* @private | ||
*/ | ||
var missing = { | ||
require: require | ||
}; | ||
Object.keys(global).forEach(function add(prop) { | ||
missing[prop] = global[prop]; | ||
}); | ||
/** | ||
* Code reading and cleaning up. | ||
* | ||
* @param {String} location | ||
* @api private | ||
*/ | ||
@@ -77,5 +75,7 @@ function read(location) { | ||
// | ||
// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) | ||
// because the buffer-to-string conversion in `fs.readFileSync()` | ||
// translates it to FEFF, the UTF-16 BOM. | ||
// | ||
if (code.charCodeAt(0) === 0xFEFF) { | ||
@@ -87,1 +87,18 @@ code = code.slice(1); | ||
} | ||
/** | ||
* The following properties are missing when loading plain ol files. | ||
* | ||
* @type {Object} | ||
* @private | ||
*/ | ||
var missing = Object.keys(global).reduce(function add(missing, prop) { | ||
missing[prop] = global[prop]; | ||
return missing; | ||
}, { require: require }); | ||
// | ||
// Expose the module. | ||
// | ||
load.compiler = compiler; | ||
module.exports = load; |
{ | ||
"name": "load", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Load JavaScript files that do not use the bloat module patterns", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# load | ||
Because fuck your module patterns, your module loader and other kinds of pointless | ||
codebloat that require me to use a damned JavaScript file on the server. | ||
Because fuck dedicated module patterns, module loaders, compilers and other kind | ||
of pointless code bloat that requires me to wrap my client-side JavaScript for | ||
server usage. | ||
I named it load because it loads files, thats it. | ||
People need to understand that the Node.js module system is nothing more then a | ||
`vm` that reads our a pre-defined `module` variable. We don't need to be stuck | ||
in this pattern, we can just get all the globals that are introduced while we | ||
load the script and tada, we're running the snippet on the server. | ||
## Installation | ||
Load is available in `npm` so you can install it by running: | ||
``` | ||
npm --save load | ||
``` | ||
## API | ||
```js | ||
@@ -29,2 +43,5 @@ var load = require('load'); | ||
var moo = load.compiler('function cow() { console.log("moo") }', 'moo.js'); | ||
moo(); // console.log('moo'); | ||
// And that it. | ||
@@ -31,0 +48,0 @@ ``` |
@@ -6,12 +6,31 @@ 'use strict'; | ||
var test = load('./fixtures/file.js'); | ||
assert.ok(typeof test === 'function'); | ||
assert.ok(test() === 'foo:bar'); | ||
var lib = load('./fixtures/file2'); | ||
assert.ok(typeof lib === 'object'); | ||
assert.ok(typeof lib.foo === 'function'); | ||
assert.ok(typeof lib.bar === 'function'); | ||
var stream = load('./fixtures/globals.js'); | ||
assert.ok(stream instanceof require('stream')); | ||
[{ | ||
it: 'exposes one single global? Assume module.exports.', | ||
does: function does() { | ||
var test = load('./fixtures/file.js'); | ||
assert.ok(typeof test === 'function'); | ||
assert.ok(test() === 'foo:bar'); | ||
} | ||
}, { | ||
it: 'exposes more globals ? Assume exports.<key> pattern.', | ||
does: function does() { | ||
var lib = load('./fixtures/file2'); | ||
assert.ok(typeof lib === 'object'); | ||
assert.ok(typeof lib.foo === 'function'); | ||
assert.ok(typeof lib.bar === 'function'); | ||
} | ||
}, { | ||
it: 'adds nodejs globals to the code.', | ||
does: function () { | ||
var stream = load('./fixtures/globals.js'); | ||
assert.ok(stream instanceof require('stream')); | ||
} | ||
}, { | ||
it: 'exposes the compiler function for compiling source code', | ||
does: function () { | ||
assert.ok(typeof load.compiler === 'function'); | ||
} | ||
}].forEach(function compiling(test, index) { | ||
console.log('('+ index +') it '+ test.it); | ||
test.does(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6007
142
52