All Projects → lukeed → rewrite-imports

lukeed / rewrite-imports

Licence: MIT License
Rewrite `import` statements as `require()`s; via RegExp

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rewrite-imports

escapin
Escapin is a JS/TS transpiler for escaping from complicated usage of cloud services and APIs
Stars: ✭ 20 (-35.48%)
Mutual labels:  transpiler
Pawn.Regex
🔎 Plugin that adds support for regular expressions in Pawn
Stars: ✭ 34 (+9.68%)
Mutual labels:  regex
String.prototype.matchAll
Spec-compliant polyfill for String.prototype.matchAll, in ES2020
Stars: ✭ 14 (-54.84%)
Mutual labels:  regex
Headache
Programming Language that compiles to 8 Bit Brainfuck
Stars: ✭ 59 (+90.32%)
Mutual labels:  transpiler
rust-regex-playground
Web tool to evaluate rust regular expressions
Stars: ✭ 13 (-58.06%)
Mutual labels:  regex
bck2brwsr
Bck2Brwsr VM to transpile Java bytecode to JavaScript
Stars: ✭ 93 (+200%)
Mutual labels:  transpiler
retrie
Efficient Trie-based regex unions for blacklist/whitelist filtering and one-pass mapping-based string replacing
Stars: ✭ 35 (+12.9%)
Mutual labels:  regex
sqlglot
Python SQL Parser and Transpiler
Stars: ✭ 310 (+900%)
Mutual labels:  transpiler
crystular
Crystal regex tester http://www.crystular.org/
Stars: ✭ 31 (+0%)
Mutual labels:  regex
qaffeine
Decaffeinate your JS-powered CSS stylesheets
Stars: ✭ 22 (-29.03%)
Mutual labels:  transpiler
i3blocks-modules
Custom modules for i3blocks status bar
Stars: ✭ 36 (+16.13%)
Mutual labels:  regex
regex-cache
Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in dramatic speed improvements.
Stars: ✭ 39 (+25.81%)
Mutual labels:  regex
regex
A set of ready-made regex helper methods for use in your Laravel application.
Stars: ✭ 226 (+629.03%)
Mutual labels:  regex
flake8-type-checking
Flake8 plugin for managing type-checking imports & forward references.
Stars: ✭ 38 (+22.58%)
Mutual labels:  imports
genex
Genex package for Go
Stars: ✭ 64 (+106.45%)
Mutual labels:  regex
learn js regexp
Example based guide to mastering JavaScript regexp
Stars: ✭ 85 (+174.19%)
Mutual labels:  regex
js-slang
Implementations of the Source languages, which are small sublanguages of JavaScript designed for SICP JS
Stars: ✭ 41 (+32.26%)
Mutual labels:  transpiler
grizzly
A Python-to-SQL transpiler as replacement for Python Pandas
Stars: ✭ 27 (-12.9%)
Mutual labels:  transpiler
extract-email-address
Extracts email address from an arbitrary text input.
Stars: ✭ 45 (+45.16%)
Mutual labels:  regex
hex-color-regex
Regular expression for matching hex color values from string.
Stars: ✭ 29 (-6.45%)
Mutual labels:  regex

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.

💡 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].