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

rewrite-imports

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rewrite-imports

A tiny (349B) utility to rewrite `import` statements as `require()`s; via RegExp

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

rewrite-imports CI

A tiny (349B) utility to transform various import statements into require() calls, using regular expressions.

Looking for something more backwards compatible?
Check out v1.4.0 which does not rely on destructured assignment!

Caveats

This module returns a string and does not provide a runtime nor does it evaluate the output.

:bulb: For this behavior, use rewrite-module or check out @taskr/esnext for an example.

The output requires a JavaScript runtime that supports require calls and destructuring assignments with Objects.

  • At least Node 6.x is required

  • Or, for browsers:

    • A require shim is always needed – see fn
    • Ensure your target browsers support destructuring – see chart

If you have false positives, you may want to use an AST to find actual import statements before transformation.

Check out an example implementation.

Install

$ npm install --save rewrite-imports

Usage

import { rewrite } from 'rewrite-imports';
// or
const { rewrite } = require('rewrite-imports');

rewrite(`import foo from '../bar'`);
//=> const foo = require('../bar');

rewrite(`import { foo } from 'bar'`);
//=> const { foo } = require('bar');

rewrite(`import * as path from 'path';`);
//=> const path = require('path');

rewrite(`import { foo as bar, baz as bat, lol } from 'quz';`);
//=> const { foo:bar, baz:bat, lol } = require('quz');

rewrite(`import foobar, { foo as FOO, bar } from 'foobar';`);
//=> const foobar = require('foobar');
//=> const { foo:FOO, bar } = foobar;

API

rewrite(input, fn)

input

Type: String

The import statement(s) or the code containing import statement(s).

See MDN for valid import statement syntax.

fn

Type: String
Default: 'require'

The require-like function name to use. Defaults to require but you may choose to pass the name of a custom shim function; for example, __webpack_require__ may work for webpack in the browser.

License

MIT © Luke Edwards

Keywords

FAQs

Package last updated on 03 Jun 2021

Did you know?

Socket

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.

Install

Related posts

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