Comparing version 0.5.2 to 0.6.0
88
index.js
@@ -1,11 +0,20 @@ | ||
var extensions = { | ||
'.babel.js': 'babel/register', | ||
const extensions = { | ||
'.babel.js': { | ||
module: 'babel/register', | ||
register: function (module) { | ||
module({ | ||
// register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353 | ||
// which only captures the final extension (.babel.js -> .js) | ||
extensions: '.js' | ||
}) | ||
} | ||
}, | ||
'.cirru': 'cirru-script/lib/register', | ||
'.cjsx': 'node-cjsx/register', | ||
'.co': 'coco', | ||
'.coffee': 'coffee-script/register', | ||
'.coffee.md': 'coffee-script/register', | ||
'.coffee': ['coffee-script/register', 'coffee-script'], | ||
'.coffee.md': ['coffee-script/register', 'coffee-script'], | ||
'.csv': 'require-csv', | ||
'.iced': 'iced-coffee-script/register', | ||
'.iced.md': 'iced-coffee-script/register', | ||
'.iced': ['iced-coffee-script/register', 'iced-coffee-script'], | ||
'.iced.md': ['iced-coffee-script/register', 'iced-coffee-script'], | ||
'.ini': 'require-ini', | ||
@@ -15,9 +24,27 @@ '.js': null, | ||
'.json5': 'json5/lib/require', | ||
'.jsx': 'node-jsx', | ||
'.litcoffee': 'coffee-script/register', | ||
'.liticed': 'iced-coffee-script/register', | ||
'.ls': 'LiveScript', | ||
'.jsx': [ | ||
{ | ||
module: 'babel/register', | ||
register: function (module) { | ||
module({ | ||
extensions: '.jsx' | ||
}); | ||
}, | ||
}, | ||
{ | ||
module: 'node-jsx', | ||
register: function (module) { | ||
module.install({ | ||
extension: '.jsx', | ||
harmony: true | ||
}); | ||
} | ||
} | ||
], | ||
'.litcoffee': ['coffee-script/register', 'coffee-script'], | ||
'.liticed': ['iced-coffee-script/register', 'iced-coffee-script'], | ||
'.ls': ['livescript', 'LiveScript'], | ||
'.node': null, | ||
'.toml': 'toml-require', | ||
'.ts': 'typescript-register', | ||
'.ts': ['typescript-register', 'typescript-require'], | ||
'.wisp': 'wisp/engine/node', | ||
@@ -29,37 +56,3 @@ '.xml': 'require-xml', | ||
var register = { | ||
'babel/register': function (module, config) { | ||
module(config); | ||
}, | ||
'node-jsx': function (module, config) { | ||
module.install(config); | ||
}, | ||
'toml-require': function (module) { | ||
module.install(); | ||
} | ||
}; | ||
var legacyModules = { | ||
'.coffee': 'coffee-script', | ||
'.coffee.md': 'coffee-script', | ||
'.iced': 'iced-coffee-script', | ||
// .iced.md and .liticed weren't available before the register module | ||
'.litcoffee': 'coffee-script', | ||
// typescript-require is for versions of TypeScript before 1.4 | ||
'.ts': 'typescript-require' | ||
}; | ||
var configurations = { | ||
'babel/register': { | ||
// register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353 | ||
// which only captures the final extension (.babel.js -> .js) | ||
extensions: '.js' | ||
}, | ||
'node-jsx': { | ||
extension: '.jsx', | ||
harmony: true | ||
} | ||
}; | ||
var jsVariantExtensions = [ | ||
const jsVariantExtensions = [ | ||
'.js', | ||
@@ -84,5 +77,2 @@ '.babel.js', | ||
extensions: extensions, | ||
legacy: legacyModules, | ||
configurations: configurations, | ||
register: register, | ||
jsVariants: jsVariantExtensions.reduce(function (result, ext) { | ||
@@ -89,0 +79,0 @@ result[ext] = extensions[ext]; |
{ | ||
"name": "interpret", | ||
"description": "A dictionary of file extensions and associated module loaders.", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"homepage": "https://github.com/tkellen/node-interpret", | ||
@@ -6,0 +6,0 @@ "author": { |
156
README.md
@@ -13,13 +13,23 @@ # interpret | ||
Map file types to modules which provide a [require.extensions] loader. | ||
```js | ||
{ | ||
'.babel.js': 'babel/register', | ||
'.babel.js': { | ||
module: 'babel/register', | ||
register: function (module) { | ||
module({ | ||
// register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353 | ||
// which only captures the final extension (.babel.js -> .js) | ||
extensions: '.js' | ||
}) | ||
} | ||
}, | ||
'.cirru': 'cirru-script/lib/register', | ||
'.cjsx': 'node-cjsx/register', | ||
'.co': 'coco', | ||
'.coffee': 'coffee-script/register', | ||
'.coffee.md': 'coffee-script/register', | ||
'.coffee': ['coffee-script/register', 'coffee-script'], | ||
'.coffee.md': ['coffee-script/register', 'coffee-script'], | ||
'.csv': 'require-csv', | ||
'.iced': 'iced-coffee-script/register', | ||
'.iced.md': 'iced-coffee-script/register', | ||
'.iced': ['iced-coffee-script/register', 'iced-coffee-script'], | ||
'.iced.md': ['iced-coffee-script/register', 'iced-coffee-script'], | ||
'.ini': 'require-ini', | ||
@@ -29,8 +39,27 @@ '.js': null, | ||
'.json5': 'json5/lib/require', | ||
'.jsx': 'node-jsx', | ||
'.litcoffee': 'coffee-script/register', | ||
'.liticed': 'iced-coffee-script/register', | ||
'.ls': 'LiveScript', | ||
'.jsx': [ | ||
{ | ||
module: 'babel/register', | ||
function (module) { | ||
module({ | ||
extensions: '.jsx' | ||
}); | ||
}, | ||
}, | ||
{ | ||
module: 'node-jsx', | ||
register: function (module) { | ||
module.install({ | ||
extension: '.jsx', | ||
harmony: true | ||
}); | ||
} | ||
} | ||
], | ||
'.litcoffee': ['coffee-script/register', 'coffee-script'], | ||
'.liticed': ['iced-coffee-script/register', 'iced-coffee-script'], | ||
'.ls': ['livescript', 'LiveScript'], | ||
'.node': null, | ||
'.toml': 'toml-require', | ||
'.ts': 'typescript-register', | ||
'.ts': ['typescript-register', 'typescript-require'], | ||
'.wisp': 'wisp/engine/node', | ||
@@ -40,107 +69,20 @@ '.xml': 'require-xml', | ||
'.yml': 'require-yaml' | ||
} | ||
}; | ||
``` | ||
### legacy | ||
Check here to see if a legacy module should be loaded upon failure to load the main module. If a legacy module is available | ||
it is recommended to use `try/catch` around the `require`s to avoid crashing the process upon failure to load the main module. | ||
```js | ||
{ | ||
'.coffee': 'coffee-script' // old versions of coffee-script didn't have the `register` module | ||
} | ||
``` | ||
### jsVariants | ||
Same as above, but only include the extensions which are javascript variants. | ||
### register | ||
Check here to see if setup is needed for the module register itself with [require.extensions]. If a method is returned, call it with the module. | ||
```js | ||
{ | ||
'toml-require': function (module) { | ||
module.install(); | ||
} | ||
} | ||
``` | ||
## How to use it | ||
### configurations | ||
These configuration options should be passed into any `register` function with the same key. | ||
```js | ||
// configurations | ||
{ | ||
'node-jsx': { | ||
extension: '.jsx', | ||
harmony: true | ||
} | ||
} | ||
// register | ||
{ | ||
'node-jsx': function (module, config) { | ||
module.install(config); | ||
} | ||
} | ||
``` | ||
Consumers should use the exported `extensions` or `jsVariants` object to determine which module should be loaded for a given extension. If a matching extension is found, consumers should do the following: | ||
### jsVariants | ||
Extensions which are javascript variants. | ||
1. If the value is null, do nothing. | ||
```js | ||
{ | ||
'.js': null, | ||
'.babel.js': 'babel/register', | ||
'.cirru': 'cirru-script/lib/register', | ||
'.cjsx': 'node-cjsx/register', | ||
'.co': 'coco', | ||
'.coffee': 'coffee-script/register', | ||
'.coffee.md': 'coffee-script/register', | ||
'.iced': 'iced-coffee-script/register', | ||
'.iced.md': 'iced-coffee-script/register', | ||
'.jsx': 'node-jsx', | ||
'.litcoffee': 'coffee-script/register', | ||
'.liticed': 'iced-coffee-script/register', | ||
'.ls': 'LiveScript', | ||
'.ts': 'typescript-register', | ||
'.wisp': 'wisp/engine/node' | ||
} | ||
``` | ||
2. If the value is a string, try to require it. | ||
[require.extensions]: http://nodejs.org/api/globals.html#globals_require_extensions | ||
3. If the value is an object, try to require the `module` property. If successful, the `register` property (a function) should be called with the module passed as the first argument. | ||
4. If the value is an array, iterate over it, attempting step #2 or #3 until one of the attempts does not throw. | ||
### Example Usage | ||
```js | ||
const interpret = require('interpret'); | ||
const path = require('path'); | ||
const resolve = require('resolve'); | ||
// register support for a defined extension | ||
function register(filepath, cwd) { | ||
// find the extension of the requested filename | ||
var ext = path.extname(filepath); | ||
// see if this extension is already supported | ||
if (Object.keys(require.extensions).indexOf(ext) !== -1) { | ||
return; | ||
} | ||
// if no cwd is specified, assume we want to use the | ||
// directory the requested file exists in | ||
if (!cwd) { | ||
cwd = path.dirname(path.resolve(filepath)); | ||
} | ||
// find out which module is needed to read this extension | ||
var moduleName = interpret.extensions[ext]; | ||
// if a module exists for this extension, make it usable | ||
if (moduleName) { | ||
// find the module relative to cwd that can add support for this extension | ||
// optionally deal with legacy modules here | ||
var module = resolve.sync(moduleName, {basedir: cwd}); | ||
// require it | ||
var compiler = require(module); | ||
// see if there is a method needed beyond requiring to enable support | ||
var register = interpret.register[moduleName]; | ||
var config = interpret.configurations[moduleName]; | ||
// if there is, run it | ||
if (register) { | ||
register(compiler, config); | ||
} | ||
} | ||
} | ||
``` | ||
Note: this is more or less exactly how [rechoir](http://github.com/tkellen/node-rechoir) works. | ||
[require.extensions]: http://nodejs.org/api/globals.html#globals_require_extensions |
Sorry, the diff of this file is not supported yet
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
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
0
8339
77
86