rewrite-imports
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.
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';
const { rewrite } = require('rewrite-imports');
rewrite(`import foo from '../bar'`);
rewrite(`import { foo } from 'bar'`);
rewrite(`import * as path from 'path';`);
rewrite(`import { foo as bar, baz as bat, lol } from 'quz';`);
rewrite(`import foobar, { foo as FOO, bar } from '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