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

require-extension-hooks

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-extension-hooks - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

src/permaCache.js

3

changelog.md
# Change Log
## 0.3.1
- Include should be either-y.
## 0.3.0

@@ -4,0 +7,0 @@ - Fixed a bug where the plugin option did not pass configuration options [5](https://github.com/jackmellis/require-extension-hooks/issues/5)

6

package.json
{
"name": "require-extension-hooks",
"version": "0.3.0",
"version": "0.3.1",
"description": "Add hooks for js extension types",

@@ -26,7 +26,9 @@ "main": "src/index.js",

"minimatch": "^3.0.4",
"mkdirp": "^0.5.1",
"source-map": "^0.5.6"
},
"devDependencies": {
"ava": "^0.20.0"
"ava": "^0.20.0",
"inspect-process": "^0.5.0"
}
}

@@ -28,5 +28,11 @@ # require-extension-hooks

### hook.shift(fn)
### hook.unshift(fn)
Inserts a function at the start of the queue of hooks for this extension.
### hook.pop()
Removes the latest hook from the current extension.
### hook.shift()
Removes the first hook from the current extension.
### hook.splice(index, remove, fn1, fn2, ...)

@@ -39,7 +45,10 @@ Acts like [].splice() for inserting and removing functions.

### hook.plugin(name | fn)
Loads a plugin. The plugin can either be a partial name (i.e. for *require-extension-hooks-vue* you can just type `hook.plugin('vue')`), the full name of a plugin (i.e. `hook.plugin('require-extension-hooks-vue')`) or a direct function (i.e. `hook.plugin(function(config){}`).
Loads a plugin.
The plugin can either be a partial name (i.e. for *require-extension-hooks-vue* you can just type `hook.plugin('vue')`), the full name of a plugin (i.e. `hook.plugin('require-extension-hooks-vue')`) or a direct function (i.e. `hook.plugin(function(config){}`).
The plugin is automatically added to the hook queue. You can move it to the start of the queue by calling `hook.plugin('xxx').unshift()`.
### include(pattern | fn)
Restricts the hook to only run based on the provided pattern. The argument can either a function (that takes the same configuration options as the hook itself), or a **glob** pattern that is matched against the filename.
Restricts the hook to only run based on the provided pattern. The argument can be either a function (that takes the same configuration options as the hook itself), or a **glob** pattern that is matched against the filename.
```js

@@ -52,3 +61,3 @@ // these 2 examples will both only run for files in the node_modules folder

### exclude(pattern | fn)
Restricts the hook to skip files that do not match the provided pattern. The argument can either a function (that takes the same configuration options as the hook itself), or a **glob** pattern that is matched against the filename.
Restricts the hook to skip files that do not match the provided pattern. The argument can be either a function (that takes the same configuration options as the hook itself), or a **glob** pattern that is matched against the filename.
```js

@@ -63,2 +72,7 @@ // these 2 examples will both EXCLUDE any files from the node_modules folder

### config
```js
hooks('js').push(function ({filename, content, stop, cancel, sourceMap, hook}) {
...
})
```
A hook function takes a config object as its only argument. This object contains the following options:

@@ -81,5 +95,5 @@ #### filename

hooks('.custom').push(function ({content, hook}) {
let {javascriptPart, typescriptPart} = extractStuffFromContent(content);
let {javascriptPart, typescriptPart} = extractStuff(content);
let transpiledTypescriptPart = hook('.ts', typescriptPart);
return `${javascriptPart}\n${typescriptPart}`;
return `${javascriptPart}\n${transpiledTypescriptPart}`;
})

@@ -89,4 +103,4 @@ ```

- `String` - assumed to be the content you want to parse.
- `{content : String}` - same as passing content directly
- `{content : String, filename : String}` - passes the content to the hook but with a custom filename
- `{content : String}` - same as passing content directly.
- `{content : String, filename : String}` - passes the content to the hook but with a custom filename.
- `{filename : String}` - pass in a custom filename and it will read in and transpile that file's content.

@@ -93,0 +107,0 @@

@@ -7,3 +7,4 @@ const minimatch = require('minimatch');

this._plugin = null;
this._filter = [];
this._include = [];
this._exclude = [];
}

@@ -16,6 +17,10 @@ push(fn){

if (fn && typeof fn === 'function'){
if (this._filter.length){
if (this._include.length){
let fn2 = fn;
fn = config => this._filter.every(fn => fn(config)) && fn2(config);
fn = config => this._include.some(fn => fn(config)) && fn2(config);
}
if (this._exclude.length){
let fn2 = fn;
fn = config => this._exclude.every(fn => !fn(config)) && fn2(config);
}
this._extensions.forEach(ext => ext.push(fn));

@@ -87,6 +92,6 @@ }

pattern = new minimatch.Minimatch(pattern);
this._filter.push(({filename}) => pattern.match(filename));
this._include.push(({filename}) => pattern.match(filename));
break;
case 'function':
this._filter.push(pattern);
this._include.push(pattern);
break;

@@ -100,6 +105,6 @@ }

pattern = new minimatch.Minimatch(pattern);
this._filter.push(({filename}) => !pattern.match(filename));
this._exclude.push(({filename}) => pattern.match(filename));
break;
case 'function':
this._filter.push(config => !pattern(config));
this._exclude.push(pattern);
break;

@@ -106,0 +111,0 @@ }

const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const cache = require('./cache');
const permaCache = require('./permaCache');
const cwd = path.resolve('.');
const requireCache = {};

@@ -11,7 +15,19 @@ function requireIf(path){

const cache = require('./cache');
function hook(hooks, module, filename){
const useCache = permaCache.enabled && permaCache.match(filename);
if (useCache){
const cached = getCachedFile(filename);
if (cached !== false){
compile(module, cached, filename);
return;
}
}
function hook(hooks, module, filename){
const content = fs.readFileSync(filename, 'utf8');
const transpiled = transpile(hooks, filename, content);
if (useCache){
setCachedFile(filename, transpiled);
}
compile(module, transpiled, filename);

@@ -169,2 +185,31 @@ }

function getCachedFilepath(filepath){
return path.join(
permaCache.path,
filepath.replace(permaCache.cwd, ''),
);
}
function getCachedFile(filepath){
const tmpFilepath = getCachedFilepath(filepath);
let tmpLastUpdated = 0;
try{
tmpLastUpdated = fs.statSync(tmpFilepath).mtimeMs;
}catch(e){
return false;
}
const lastUpdated = fs.statSync(filepath).mtimeMs;
if (lastUpdated >= tmpLastUpdated){
return false;
}
return fs.readFileSync(tmpFilepath, 'utf8');
}
function setCachedFile(filepath, content){
const tmpFilepath = getCachedFilepath(filepath);
const tmpDirname = path.dirname(tmpFilepath);
mkdirp.sync(tmpDirname);
fs.writeFileSync(tmpFilepath, content, 'utf8');
}
module.exports = hook;
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