node-hook-filename
Advanced tools
Comparing version 0.2.0 to 0.3.0
29
index.js
@@ -1,25 +0,20 @@ | ||
var Module = require('module'); | ||
const Module = require('module') | ||
const originalLoad = Module._load | ||
var originalLoad = Module._load; | ||
module.exports = function(extensions, override) { | ||
override = override ? override : (r) => r | ||
override = override ? override : function(r) { return r }; | ||
extensions.forEach(function(ext) { | ||
Module._extensions[ext] = function(module, filename) { | ||
module.exports = filename; | ||
extensions.forEach((ext) => { | ||
Module._extensions[ext] = (module, filename) => { | ||
module.exports = filename | ||
} | ||
}); | ||
}) | ||
var regexes = extensions.map(function(ext){ return new RegExp(ext, '') }); | ||
Module._load = function(request, parent, isMain) { | ||
if (regexes.some(function(regex){ return request.match(regex) })) { | ||
return override(request); | ||
const requestMatchesExt = (ext) => request.includes(ext) | ||
if (extensions.some(requestMatchesExt)) { | ||
return override(request) | ||
} | ||
return originalLoad.call(this, request, parent, isMain); | ||
return originalLoad.call(this, request, parent, isMain) | ||
} | ||
}; | ||
} |
{ | ||
"name": "node-hook-filename", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "\"Hooking node require calls for specific extensions to only return the filename\"", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node_modules/.bin/mocha test/**/*", | ||
"lint": "eslint . --ext .js", | ||
"test": "node_modules/.bin/mocha test/**/*.spec.js", | ||
"test:watch": "node_modules/.bin/mocha --watch test/**/*" | ||
@@ -21,4 +22,6 @@ }, | ||
"devDependencies": { | ||
"mocha": "2.4.5" | ||
"eslint": "^2.2.0", | ||
"mocha": "2.4.5", | ||
"sinon": "^1.17.3" | ||
} | ||
} |
@@ -0,4 +1,6 @@ | ||
[![Circle CI](https://circleci.com/gh/tomatau/node-hook-filename.svg?style=svg)](https://circleci.com/gh/tomatau/node-hook-filename) | ||
# node-hook-filename | ||
Hooking node require calls for specific extensions to only return the filename or a specified module | ||
Hooking node require calls for specific extensions to only return the filename or the return value of a callback. | ||
@@ -10,12 +12,10 @@ ## Usage | ||
```js | ||
var nhf = require('node-hook-filename'); | ||
const nhf = require('node-hook-filename') | ||
nhf(['.scss', '.svg']) | ||
nhf(['.scss', '.svg']); | ||
const scssAsset = require('../path/too/filename.scss') | ||
const svgAsset = require('../path/too/filename.svg') | ||
const normalRequire = require('../path/too/js/file') | ||
var scssAsset = require('../path/too/filename.scss'); | ||
var svgAsset = require('../path/too/filename.svg'); | ||
var normalRequire = require('../path/too/js/file'); | ||
// scssAsset === '../path/too/filename.scss' | ||
@@ -30,11 +30,11 @@ // svgAsset === '../path/too/filename.svg' | ||
```js | ||
var nhf = require('node-hook-filename'); | ||
nhf(['config'], () => 'foo'); | ||
const nhf = require('node-hook-filename') | ||
nhf(['config'], (filename) => 'foo ' + filename) | ||
var configRequire = require('config'); | ||
// configRequire will return 'foo' | ||
const configRequire = require('config') | ||
// configRequire will return 'foo config' | ||
``` | ||
Useful if you are running a universal/isomorphic app that requires asset files. | ||
Handy if you are running a universal/isomorphic app that requires asset files. | ||
@@ -41,0 +41,0 @@ This is mostly useful for **testing** and not production ready! |
@@ -1,33 +0,55 @@ | ||
'use strict'; | ||
const assert = require('assert'), | ||
nhf = require('../index'); | ||
const assert = require('assert') | ||
const sinon = require('sinon') | ||
const nhf = require('../index') | ||
describe('node-hook-filename module overrides', () => { | ||
context('Given Override Extensions', ()=> { | ||
before(()=> { | ||
nhf([ '.json', 'dummy' ]) | ||
}) | ||
it('require should return filename instead of module', () => { | ||
it('should modify require to return the filenames', ()=> { | ||
const filename = './fixtures/config.json' | ||
const actual = require(filename) | ||
assert.equal(actual, filename) | ||
}) | ||
nhf(['.json']); | ||
it('should not modify require for non matched files', ()=> { | ||
const actual = require('./fixtures/test') | ||
assert.deepEqual(actual, { not: 'affected' }) | ||
}) | ||
const filename = './config.json'; | ||
const config = require(filename); | ||
it('should only modify require for a complete string match', ()=> { | ||
const actual = require('./fixtures/configjson.js') | ||
assert.deepEqual(actual, { also: 'not affected' }) | ||
}) | ||
}) | ||
assert.equal(config, filename); | ||
}); | ||
context('Given Override Extension With Callback', ()=> { | ||
const callback = sinon.spy(() => ({ y: 'z' })) | ||
it('require should call function and return specified string', () => { | ||
before(()=> { | ||
nhf([ '.ext', '.ext2' ], callback) | ||
}) | ||
const obj = { 'y' : 'z'}; | ||
nhf(['config'], () => obj); | ||
afterEach(()=> { | ||
callback.reset() | ||
}) | ||
const configFile = require('./config'); | ||
it('should invoke the callback with the matching filename', ()=> { | ||
const filename = './fixtures/matching.ext' | ||
require(filename) | ||
assert.ok(callback.calledWith(filename)) | ||
callback.reset() | ||
const filename2 = './fixtures/matching.ext2' | ||
require(filename2) | ||
assert.ok(callback.calledWith(filename2)) | ||
}) | ||
assert.equal(configFile, obj); | ||
}); | ||
it('require should return an original file if no override is specified', () => { | ||
const original = require('./test'); | ||
assert.deepEqual(original,{foo:"bar"}); | ||
}); | ||
}); | ||
it('should modify the return value to eql the return from callback', ()=> { | ||
const filename = './fixtures/matching.ext' | ||
const actual = require(filename) | ||
assert.deepEqual(actual, callback()) | ||
}) | ||
}) | ||
}) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
69
9934
3
10
5
1