Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-hook-filename

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-hook-filename - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

.eslintrc

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())
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc