interpret
A dictionary of file extensions and associated module loaders.
What is it
This is used by Liftoff to automatically require dependencies for configuration files, and by rechoir for registering module loaders.
API
extensions
Map file types to modules which provide a require.extensions loader.
{
'.babel.js': 'babel/register',
'.cirru': 'cirru-script/lib/register',
'.cjsx': 'node-cjsx/register',
'.co': 'coco',
'.coffee': 'coffee-script/register',
'.coffee.md': 'coffee-script/register',
'.csv': 'require-csv',
'.iced': 'iced-coffee-script/register',
'.iced.md': 'iced-coffee-script/register',
'.ini': 'require-ini',
'.js': null,
'.json': null,
'.json5': 'json5/lib/require',
'.jsx': 'node-jsx',
'.litcoffee': 'coffee-script/register',
'.liticed': 'iced-coffee-script/register',
'.ls': 'LiveScript',
'.toml': 'toml-require',
'.ts': 'typescript-register',
'.wisp': 'wisp/engine/node',
'.xml': 'require-xml',
'.yaml': 'require-yaml',
'.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.
{
'.coffee': 'coffee-script'
}
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.
{
'toml-require': function (module) {
module.install();
}
}
configurations
These configuration options should be passed into any register
function with the same key.
{
'node-jsx': {
extension: '.jsx',
harmony: true
}
}
{
'node-jsx': function (module, config) {
module.install(config);
}
}
jsVariants
Extensions which are javascript variants.
{
'.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'
}
Example Usage
const interpret = require('interpret');
const path = require('path');
const resolve = require('resolve');
function register(filepath, cwd) {
var ext = path.extname(filepath);
if (Object.keys(require.extensions).indexOf(ext) !== -1) {
return;
}
if (!cwd) {
cwd = path.dirname(path.resolve(filepath));
}
var moduleName = interpret.extensions[ext];
if (moduleName) {
var module = resolve.sync(moduleName, {basedir: cwd});
var compiler = require(module);
var register = interpret.register[moduleName];
var config = interpret.configurations[moduleName];
if (register) {
register(compiler, config);
}
}
}
Note: this is more or less exactly how rechoir works.