Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
interpret
Advanced tools
Package description
The interpret npm package is designed to be a dictionary of require extensions and associated file extensions. It allows developers to automatically register the appropriate require extension for files based on their file extensions. This is particularly useful when working with different types of files that need to be required in Node.js applications, such as .coffee, .ts, or .jsx files.
Registering require extensions
This code retrieves the require extension for TypeScript files, allowing Node.js to understand how to process and import .ts files.
require('interpret').extensions['.ts']
Associating file extensions with custom handlers
This code demonstrates how to associate a custom file extension (.custom) with a custom compiler or handler to be used when requiring files with that extension.
var interpret = require('interpret');
var extensions = interpret.extensions;
extensions['.custom'] = require('my-custom-compiler');
Rechoir is a package that allows you to automatically register the appropriate require hooks based on a file's extension. It is similar to interpret but goes a step further by actually attempting to require the necessary module to handle the file extension.
Liftoff is a CLI framework that builds on top of interpret. It enables applications to specify which interpreters they support for configuration files, and it will automatically require the necessary dependencies.
This package is used to hook into the require function to add support for transpiling files on the fly. It is similar to interpret in that it deals with require extensions, but it focuses more on the runtime aspect of transpiling or processing files.
Readme
A dictionary of file extensions and associated module loaders.
This is used by Liftoff to automatically require dependencies for configuration files, and by rechoir for registering module loaders.
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'
}
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' // old versions of coffee-script didn't have the `register` module
}
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();
}
}
These configuration options should be passed into any register
function with the same key.
// configurations
{
'node-jsx': {
extension: '.jsx',
harmony: true
}
}
// register
{
'node-jsx': function (module, config) {
module.install(config);
}
}
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'
}
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 works.
FAQs
A dictionary of file extensions and associated module loaders.
The npm package interpret receives a total of 15,560,563 weekly downloads. As such, interpret popularity was classified as popular.
We found that interpret demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.