https://github.com/lukeed/rewrite-imports
Rewrite `import` statements as `require()`s; via RegExp
https://github.com/lukeed/rewrite-imports
imports javascript regex transpiler
Last synced: 9 months ago
JSON representation
Rewrite `import` statements as `require()`s; via RegExp
- Host: GitHub
- URL: https://github.com/lukeed/rewrite-imports
- Owner: lukeed
- License: mit
- Created: 2017-07-25T22:17:29.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-03T23:48:56.000Z (about 5 years ago)
- Last Synced: 2024-08-10T11:07:31.663Z (almost 2 years ago)
- Topics: imports, javascript, regex, transpiler
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 31
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license
Awesome Lists containing this project
README
# rewrite-imports [](https://github.com/lukeed/rewrite-imports/actions/workflows/ci.yml)
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`](https://github.com/lukeed/rewrite-imports/tree/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`](https://github.com/lukeed/rewrite-module) or check out [`@taskr/esnext`](https://github.com/lukeed/taskr/tree/master/packages/esnext) for an example.
The output requires a JavaScript runtime that supports `require` calls and [destructuring assignments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring) with Objects.
* At least `Node 6.x` is required
* Or, for browsers:
* A `require` shim is always needed – see [`fn`](#fn)
* Ensure your target browsers support destructuring – see [chart](https://kangax.github.io/compat-table/es6/#test-destructuring,_assignment)
If you have [false positives](https://github.com/lukeed/rewrite-imports/issues/8), you may want to use an AST to find actual `import` statements before transformation.
> Check out an [example implementation](https://github.com/styleguidist/react-styleguidist/blob/82f22d217044dee6215e60696c39791ee168fc14/src/client/utils/transpileImports.js).
## Install
```
$ npm install --save rewrite-imports
```
## Usage
```js
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) 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](https://lukeed.com)